 | Discussion Forum |  |
|
|
Conversion of integer to ASCII for displayNext Thread | Thread List | Previous Thread Start a Thread | Settings | Details | Message |
|---|
Read-Only Author Tim Wood Posted 18-Jan-2004 10:33 GMT Toolset C51 |  Conversion of integer to ASCII for display Tim Wood I wish to display the contents of a variable, probably an unsigned integer, on an lcd display driven from the 8051. I have written software to accept an array of characters and write these, so the ideal would be to creat a similat array from the variable.
I have tried the basic ways I know of,
sprintf (char_array, "%d", integer_value);
(very low tech) but dosent work.
Any pointers would be greatfully recieved,
Thank you,
Tim | | Read-Only Author Dan Henry Posted 18-Jan-2004 16:45 GMT Toolset C51 |  RE: Conversion of integer to ASCII for display Dan Henry sprintf() really should work for you, but you have not provided enough context for us to determine why it doesn't. On the other hand, if you would only be using sprintf() for integer to ASCII conversion, you could do the conversion with less code overhead by using a function like:
void UlToStr(char *s, unsigned long bin, unsigned char n);
This is a function that stores a NUL-terminated string, in n + 1 (plus 1 for the NUL string terminator) successive elements of the array whose first element has the address s, by converting bin to n decimal characters. The function stores leading zeroes if necessary. If the leading zeroes are undesireable, you can replace them with spaces or start outputting at the first nonzero character.
void UlToStr(char *s, unsigned long bin, unsigned char n)
{
s += n;
*s = '\0';
while (n--)
{
*--s = (bin % 10) + '0';
bin /= 10;
}
}
| | Read-Only Author Tim Wood Posted 18-Jan-2004 17:04 GMT Toolset C51 |  RE: Conversion of integer to ASCII for display Tim Wood Dan,
Thank you very much for your help. Thats fantastic!
Tim | | Read-Only Author Graham Cole Posted 27-Jan-2004 11:01 GMT Toolset C51 |  RE: Conversion of integer to ASCII for display Graham Cole If you are interested in a faster radix conversion, see here:
http://www.programmersheaven.com/zone5/cat27/32144.htm
A general purpose radix conversion function is provided as a specific example. The algorithm used is successive division of the value by the radix - same as algorithm posted by Dan. However, this implementation should prove to be much faster than using just C. | | Read-Only Author Lekha NM Posted 23-Jan-2004 09:26 GMT Toolset C51 |  RE: Conversion of integer to ASCII for display Lekha NM If the number is an unsigned integer,you may have to use %u instead of %d in the sprintf function to get the correct string.
sprintf (char_array, "%d", integer_value); | | Read-Only Author Drew Davis Posted 23-Jan-2004 18:00 GMT Toolset C51 |  RE: Conversion of integer to ASCII for display Drew Davis Note that unless you enable ANSI integer promotion, a byte variable cannot be printed with "%d". Those format specifiers only work with integers, which are two bytes long. To print bytes, you need to use "%bd" (etc), in much the same way you need "%ld" for a 32-bit long. | |
Next Thread | Thread List | Previous Thread Start a Thread | Settings |
|