View previous topic :: View next topic |
Author |
Message |
chingB
Joined: 29 Dec 2003 Posts: 81
|
array of ascii number to integer number? |
Posted: Mon Jan 19, 2004 10:11 pm |
|
|
Hi,
Anybody in the community have an idea on how to convert an array of ascii number to integer?
e.g.
array1[0]=2;
array1[1]=3;
array1[2]=5;
reult would be integer number 235.
Any help will do.
Thnx |
|
|
Paolino
Joined: 19 Jan 2004 Posts: 42
|
|
Posted: Tue Jan 20, 2004 1:05 am |
|
|
I think you can act as follow:
int result;
// your code here
result=array1[0]*100+array1[1]*10+array1[2];
// your code here
Obviously, this code works for unsigned int (see CCS manual for data types) and/or for 16/32 bit integer, not for signed ones...
Best regards,
Paolo. |
|
|
neil
Joined: 08 Sep 2003 Posts: 128
|
Look in the manual! |
Posted: Tue Jan 20, 2004 3:27 am |
|
|
There is a standard C function, not specific to CCS, but it is in the CCS manual.
Atoi() converts a string, eg. an array containing ASCII into an integer.
Code: | #include <stdlib.h>
char string[4];
int x;
strcpy(string,"123");
x = atoi(string); // x is now 123
|
Only difference to what you described is that this works with variable length strings which must be terminated with a zero (binary zero, not '0'). |
|
|
Kasper
Joined: 14 Jan 2004 Posts: 88 Location: Aurora, Ontario, Canada
|
|
Posted: Tue Jan 20, 2004 2:30 pm |
|
|
Paolino wrote: | I think you can act as follow:
int result;
// your code here
result=array1[0]*100+array1[1]*10+array1[2];
// your code here
Obviously, this code works for unsigned int (see CCS manual for data types) and/or for 16/32 bit integer, not for signed ones...
Best regards,
Paolo. |
since ascii 0 = 48dec, subtracting 48 from the value in the array before multiplying will yield the better result ;)
result=(array1[0]-48)*100+(array1[1]-48)*10+(array1[2]-48); |
|
|
|