CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

array of ascii number to integer number?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
chingB



Joined: 29 Dec 2003
Posts: 81

View user's profile Send private message

array of ascii number to integer number?
PostPosted: Mon Jan 19, 2004 10:11 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Jan 20, 2004 1:05 am     Reply with quote

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

View user's profile Send private message

Look in the manual!
PostPosted: Tue Jan 20, 2004 3:27 am     Reply with quote

Exclamation 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

View user's profile Send private message Visit poster's website

PostPosted: Tue Jan 20, 2004 2:30 pm     Reply with quote

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);
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group