Number to Character for LCD Display

Thread Starter

Dalaran

Joined Dec 3, 2009
168
Hello,

I am trying to find an easy way to change an integer to a character for display on an LCD.

For instance int x = 123 and I want "123" to be displayed. I understand that simply making this a character would display "{".

Right now what I am doing is dividing down (123/100) = 1 and adding 48 to display a "1". Then I subtract 1*100 and continue (23/10), etc, etc. This then becomes more complicated when doing decimals.

There must be an easier way to display an integer. Any one with a better thought or link is greatly appreciated.
 

hgmjr

Joined Jan 28, 2005
9,027
The primitive approach is to create an array and fill it with the digits by dividing by 10 and then performing a modulo % operation on the result.


For example:

Rich (BB code):
char buffer[3];
 
value = 123;
 
buffer[0] = value % 10;
buffer[1] = value/10 % 10;
buffer[2] = value/100 % 10;
 
// Now the array buffer contains each of the digits.

You can now output each of the three digits from the array buffer.

hgmjr
 

Thread Starter

Dalaran

Joined Dec 3, 2009
168
Thanks very much for your response.

Much better than my method. Had a problem initially telling me I could not use this operator on that operand. Turned out that this operation cannot be done on a floating number. Put it to an integer of correct size and it works flawlessly.

Thanks again.
 

THE_RB

Joined Feb 11, 2008
5,438
I would almost guarantee your PIC C compiler has a built-in function to convert a byte integer into a 3 char decimal string.

Look in the help file for "ByteToStr" or "IntToStr", "CharToStr" or similar conversion functions. :)
 
Top