Number to Character for LCD Display

Discussion in 'Embedded Systems and Microcontrollers' started by Dalaran, May 14, 2011.

  1. Dalaran

    Thread Starter Active Member

    Dec 3, 2009
    168
    0
    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.
     
  2. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    For the record, what programming language are you using?

    hgmjr
     
  3. Dalaran

    Thread Starter Active Member

    Dec 3, 2009
    168
    0
    Very good point thanks. I'm programming PIC uC using C language.
     
  4. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    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:

    Code ( (Unknown Language)):
    1.  
    2. char buffer[3];
    3.  
    4. value = 123;
    5.  
    6. buffer[0] = value % 10;
    7. buffer[1] = value/10 % 10;
    8. buffer[2] = value/100 % 10;
    9.  
    10. // Now the array buffer contains each of the digits.
    11.  
    12.  

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

    hgmjr
     
  5. Dalaran

    Thread Starter Active Member

    Dec 3, 2009
    168
    0
    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.
     
  6. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    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. :)
     
Loading...