Thread: Puzzling issue... Is typecasting the answer?

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    2

    Puzzling issue... Is typecasting the answer?

    Hey all,

    Thanks so much for taking the time to read this. I'll be brief with my issue.

    I am also an EE major so I am not very smooth with C code.

    Problem: Converting an integer value into a character string. I need to use integers to calculate a temperature conversion, and make it into a char* to send to an LCD function.

    Constraints: Cannot use standard library. Cannot use floats.

    Example code:

    Code:
    int cel=1000;                         //100.0 degrees
    int fah = (((9*cel)/5)+320);              //using 320 due to x10 of temperature
    print_to_LCD((char*) fah);
    // function only accepts char* as pass-in


    This clearly doesn't work because it is trying to convert the fah value of 2120 to an ACSII character. I want it to somehow generate the string "2120".

    Thanks in advance,

    Mike

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Check out sprintf().
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    No, for sure typecasting is not the answer. You can't use sprintf() without stdio.h, but you can convert each digit to it's ASCII value:

    ASCII Table / Extended ASCII Codes

    That will mean working thru the number with modulus.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Why not use the standard library? (At least the I/O parts should be there even in an embedded system, and that's where sprintf is.) (EDIT: I guess I was wrong -- stdio.h is not required in a freestanding implementation, although interestingly float.h is.)

    If you do have to roll your own for whatever reason, you'll have to go back to how you learned numbers. Hint: 3120 is three thousand, one hundred and twenty.
    Last edited by tabstop; 05-16-2010 at 07:25 PM.

  5. #5
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    I would like to know WTH floats have to do with converting an integer to a string. Is this some artificial constraint thought up by an instructor?

    In any event one way to deal with this is to take your number as in integer and start up a loop that peels off a digit at a time. If you cannot use sprintf() and you cannot use itoa() (which is the correct answer) you can do something like:
    1. Say your integer mainInteger is 1234;
    2. Set up a char buffer of say 10 characters, memset it to zero.
    2. while your integer is > 0:
    3. int curDigit = mainInteger % 10; dividing mainInteger by 10 is 123 with 4 left over. The mod operator will stick the 4 into curDigit.
    4. Add 48 (ascii code for '0') to curDigit and stick it into the first (right-hand) element of your char buffer.
    5. mainInteger = mainInteger / 10;
    6. goto 2

    If you walk this logic on paper you will see one way of doing this. Hope this helps.
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    2
    Thanks guys.

    I can't use standard library because the FPGA I am programming on can't handle floats or it will freeze the processor. Don't ask me why, haha. I guess they just like to make ........ difficult for us.

    I guess we'll have to break it down digit by digit with modulos. ick.

    Any additional feedback would be great.

    -Mike

  7. #7
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Well its either use the standard libraries or something by hand. I don't know what else you are going to do. Modulos are not 'ick' BTW and sometimes the only way to solve certain things...
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Boot issue; no floppy drive
    By CodeMonkey in forum Tech Board
    Replies: 6
    Last Post: 11-19-2002, 05:37 PM
  2. simple answer? test your IQ
    By Fountain in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 07-11-2002, 04:32 PM
  3. I still have some problems
    By Mike Jones in forum C Programming
    Replies: 2
    Last Post: 04-01-2002, 02:26 AM
  4. code help :)
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 02-28-2002, 01:12 PM
  5. Replies: 22
    Last Post: 11-08-2001, 11:01 PM