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

Converting ascii hex to decimal

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



Joined: 06 Nov 2007
Posts: 5

View user's profile Send private message

Converting ascii hex to decimal
PostPosted: Thu Nov 08, 2007 3:08 pm     Reply with quote

I found some example code that converts ascii to decimal and it works well. If someone could explain why the code below doesnt I would appreciate it

I wanted to convert a 3 digit number to decimal so i wrongfully used this code below.

Code:


int test;
//test=(string[0]-0x30)*100 + (string[1]-0x30)*10 + (string[2]-0x30);


Thanks for your help
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Nov 08, 2007 3:33 pm     Reply with quote

Assuming that your 3 digits could be from 000 to 999, the result is
greater than an 'int' can hold. In CCS, an 'int' is an unsigned 8-bit
variable and can only hold from 0-255. So you need to declare 'test'
as an int16 as shown in bold below.

Also, you need to force the compiler to do 16-bit math on the first
part of the expression below. You can do this by casting the '100'
to a 'long'. This is done by appending an 'L' on the end, as shown below:
Quote:

int16 test;

test=(string[0]-0x30)*100L + (string[1]-0x30)*10 + (string[2]-0x30);
Jomile



Joined: 06 Nov 2007
Posts: 5

View user's profile Send private message

PostPosted: Thu Nov 08, 2007 10:52 pm     Reply with quote

Thanks PCM programmer, that did it. I was not aware of the 'L' to do 16-bit math. Much appreciation
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