EmbDev.net

Forum: µC & Digital Electronics Convert Unsigned 8 bit integer to a string ?


Author: John Mayer (215)
Posted on:

Rate this post
0 useful
not useful
How shall i convert a unsigned 8 bit integer to a string without using 
any libaries.... Only self written functions.

Author: Max H. (hartl192)
Posted on:

Rate this post
0 useful
not useful
If you use C:
  unsigned char number_txt[4], hundred=0, ten=0;
  while(1)
  {
    if(number>=100)
    {
      number=number-100;
      hundred++;
    }  
    else
      break;
  }
  while(1)
  {
    if(number>=10)
    {
      number=number-10;
      ten++;
    }  
    else
      break;
  }
  number_txt[0]=hundred+'0';
  number_txt[1]=ten+'0';
  number_txt[2]=number+'0';
  number_txt[3]=0x00;

: Edited by User
Author: T_T (Guest)
Posted on:

Rate this post
0 useful
not useful
Why this construct with while(1)/if?

This is simpler:
  unsigned char number_txt[4], hundred=0, ten=0;
  while (number >= 100)
  {
    number = number-100;
    hundred++;
  }
  while (number >= 10)
  {
    number = number-10;
    ten++;
  }
  number_txt[0] = hundred+'0';
  number_txt[1] = ten+'0';
  number_txt[2] = number+'0';
  number_txt[3] = 0;

or still shorter:
  unsigned char number_txt[4], hundred=0, ten=0;
  while (number >= 100)
  {
    number -= 100;
    hundred++;
  }
  while (number >= 10)
  {
    number -= 10;
    ten++;
  }
  number_txt[0] = hundred+'0';
  number_txt[1] = ten+'0';
  number_txt[2] = number+'0';
  number_txt[3] = 0;

Author: John Mayer (215)
Posted on:

Rate this post
0 useful
not useful
I am trying to read it to an LCD display, but I am getting random 
character on the display...

Author: John Mayer (215)
Posted on:

Rate this post
0 useful
not useful
When i number = 100, is the character i get on my LCD

11111
11010
11111
11111
11111
11111
11111
11111

Author: T_T (Guest)
Posted on:

Rate this post
0 useful
not useful
And what characters are on your lcd if you write out the string "100" 
direct?

Author: John Mayer (215)
Posted on:

Rate this post
0 useful
not useful
when i write 100 the char.  ] and
1111
1110
1100
1111
1101
1111
1111
1110


and if I write "100", 100 appears on the LCD.

oh.. i might have found the solution.. is it possible to get beyond the 
limitation of the size of number..

: Edited by User

Reply

Entering an e-mail address is optional. If you want to receive reply notifications by e-mail, please log in.

Rules — please read before posting

  • Post long source code as attachment, not in the text
  • Posting advertisements is forbidden.

Formatting options

  • [c]C code[/c]
  • [avrasm]AVR assembler code[/avrasm]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]




Bild automatisch verkleinern, falls nötig
Note: the original post is older than 6 months. Please don't ask any new questions in this thread, but start a new one.