View previous topic :: View next topic |
Author |
Message |
TL
Joined: 15 Sep 2003 Posts: 75
|
Long integer to string |
Posted: Fri Feb 03, 2006 5:31 am |
|
|
I would like to convert a long integer v=2345 into a string called str=�2.345�.
I know how to strip v into single digits like integers v[0]=2, v[1]=3, v[2]=4, v[3]=5 by using division and modulus operators.
Could someone please tell me how I can convert the above v[] array of integers into str=�2.345�
I prefer a small function as opposed to standard library functions to save ROM space. |
|
|
Benjamin
Joined: 11 Jan 2006 Posts: 21 Location: Quebec (Canada)
|
|
Posted: Fri Feb 03, 2006 9:12 am |
|
|
Well the easy way would have been using sprintf:
Code: |
sprintf(string,"%lu",long);
// or in your case
sprintf(str,"%f",v/1000);
|
But if you really want to do this manually:
you have:
v[0]=2, v[1]=3, v[2]=4, v[3]=5
Since a string is in ASCII format (you can find the ASCII table on the internet), you want this:
str[0] = 50, str[1] = '.', str[2] = 51, str[3] = 52, str[4] = 53
All you have to do is add 48 to an interger to get the ASCII code.
You should try both methods to see if this really takes up less ROM. |
|
|
Benjamin
Joined: 11 Jan 2006 Posts: 21 Location: Quebec (Canada)
|
|
Posted: Fri Feb 03, 2006 9:15 am |
|
|
P.S. D'ont forget that a string should normally finish with a NUL caracter (0), so str[5] = 0. This might depend on how you will use the string. Also make sure your table is large enough to receive this extra caracter. |
|
|
TL
Joined: 15 Sep 2003 Posts: 75
|
|
Posted: Fri Feb 03, 2006 2:55 pm |
|
|
Thanks Benjamin for your help. I'll try your suggestions. |
|
|
|