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 CCS Technical Support

string Hex into int

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








string Hex into int
PostPosted: Wed Nov 16, 2005 6:08 am     Reply with quote

Hi,

I'm trying to translate the following ASCII string: "RD 2B0 A3 58"
into: RD, 0x2b0, 0xA3, 0x58

What is the simples why to do it ?
Foppie



Joined: 16 Sep 2005
Posts: 138
Location: The Netherlands

View user's profile Send private message Send e-mail Visit poster's website MSN Messenger

PostPosted: Wed Nov 16, 2005 7:39 am     Reply with quote

I would write something like this:
Code:
#define NUMBER_OF_VALUES 4
int i, k, value;
int16 values[NUMBER_OF_VALUES] = {0,0,0,0};
k = 0;

for (i = 0; i < length(ASCIIstring); i++)
{
   if (i == ' ')
   {
      k++;
      value = 0;
      if (k == NUMBER_OF_VALUES)
      {
         i = 255;//to avoid array-overflow
      }
   }
   else
   {
      value = ASCIIstring[i] - '0';
      if (value > 9)
      {
         value = (value + '0') - 'A';
      }
      values[k] = (values[k] * 16) + value;
   }
}


this code might contain syntax errors and so you should check it first.

Off course, there are more ways to do it, and this is probably not the best...
Guest








PostPosted: Thu Nov 17, 2005 2:01 am     Reply with quote

Thanks,

This example was waht I'm looking for ..
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