Convert Unsigned 8 bit integer to a string ?

#3630064
If you use C:
1
  unsigned char number_txt[4], hundred=0, ten=0;
2
  while(1)
3
  {
4
    if(number>=100)
5
    {
6
      number=number-100;
7
      hundred++;
8
    }  
9
    else
10
      break;
11
  }
12
  while(1)
13
  {
14
    if(number>=10)
15
    {
16
      number=number-10;
17
      ten++;
18
    }  
19
    else
20
      break;
21
  }
22
  number_txt[0]=hundred+'0';
23
  number_txt[1]=ten+'0';
24
  number_txt[2]=number+'0';
25
  number_txt[3]=0x00;
Guest #3630151
Why this construct with while(1)/if?

This is simpler:
1
  unsigned char number_txt[4], hundred=0, ten=0;
2
  while (number >= 100)
3
  {
4
    number = number-100;
5
    hundred++;
6
  }
7
  while (number >= 10)
8
  {
9
    number = number-10;
10
    ten++;
11
  }
12
  number_txt[0] = hundred+'0';
13
  number_txt[1] = ten+'0';
14
  number_txt[2] = number+'0';
15
  number_txt[3] = 0;

or still shorter:
1
  unsigned char number_txt[4], hundred=0, ten=0;
2
  while (number >= 100)
3
  {
4
    number -= 100;
5
    hundred++;
6
  }
7
  while (number >= 10)
8
  {
9
    number -= 10;
10
    ten++;
11
  }
12
  number_txt[0] = hundred+'0';
13
  number_txt[1] = ten+'0';
14
  number_txt[2] = number+'0';
15
  number_txt[3] = 0;
OP #3631336
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..

Reply

A question of your own? This thread is over 12 years old. Ask it in a thread of its own – more people read along there, and it gets answered more often and sooner.

Start a new thread

A fix, an addition or feedback on this thread? Then reply here.

Formatting
  • [c]C-Code[/c]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]
  • [[Title]] – link to an article
  • Link to another post: right-click its post ID (#8078628), copy the address, paste it into the text

More about formatting

Post long source code as an attachment, not in the text.

Already registered? Log in

Not shown to anyone. We send you a link; your post appears once you confirm it there.

By submitting you create a free account and accept the terms of use.