Re: convert int to string without using standard library (stdio.h)



On 17 Jul 2006 15:42:50 -0700, in comp.lang.c , "ceeques"
<cquestions@xxxxxxxxx> wrote:

Thanks for the repy:

I need to convert number (int, short) to a string but cannot use
sprintf as I am using an embedded system. I want to mix text and
number in a display function on the system's output (It doesn't support
printf) but it does have a text to screen function that accepts
strings. I would like to display counters with a message along with it.


Hope my question is clear now.

If you have a single-digit number, you can convert it into the
equivalent character by adding '0'
int i = 4;
char x = i + '0'; // x will be '4'

If you have more than one digit, you would have to apply this logic
iteratively over each digit in turn.

To handle floating point types, you'd need to convert to scaled
integers, and re-insert the decimal point as appropriate.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
.



Relevant Pages

  • Re: a question for sorting keys in Map
    ... VARIABLE0, VARIABLE1, VARIABLE2, etc. ... for (String var: m.keySet()) ... MyKey(String v, int n) ... // Split var names into digit and non digit substrings ...
    (comp.lang.java.programmer)
  • Re: convert int to char
    ... converting it to a string called intstring ... character of the string into a char variable ch. ... I was thinking that should give me the int value ... then you can find out what number that digit ...
    (comp.lang.c)
  • Re: char conversion problem
    ... not a string. ... int AsInt; ... a 5 digit string, eg. "78542", may overflow ... Note that the decrement of the weightingfactor is now enclosed ...
    (comp.lang.cpp)
  • Re: manipulating a string
    ... > what i'm trying to do is manipulate a string from a GUI textfield. ... You get these errors, because parseInt() requires a String as argument, ... that digit must ... bedeclared as an int, ...
    (comp.lang.java.help)
  • Re: Brian Kernighan, maybe Im not worthy, maybe Im scum
    ... conformant string. ... int repeats, reps; ... ref satisfierLength); ...
    (comp.programming)