How shall i convert a unsigned 8 bit integer to a string without using any libaries.... Only self written functions.
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; |
:
Edited by User
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; |
I am trying to read it to an LCD display, but I am getting random character on the display...
When i number = 100, is the character i get on my LCD 11111 11010 11111 11111 11111 11111 11111 11111
And what characters are on your lcd if you write out the string "100" direct?
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
Please log in before posting. Registration is free and takes only a minute.
Existing account
Do you have a Google/GoogleMail account? No registration required!
Log in with Google account
Log in with Google account
No account? Register here.