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

Convert ASCII-HEX to int32 hex

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



Joined: 02 Feb 2010
Posts: 345

View user's profile Send private message

Convert ASCII-HEX to int32 hex
PostPosted: Sun Jan 20, 2013 3:09 am     Reply with quote

Hi Friends!
Any idea how to convert ascii string ,which represent hex value to hex value.
i.e. : string 0x35,0x38,0x41,0x43,0x42,0x44,0x36,0x32 ("58ACBD62") to 0x58ACBD62 int32 value.
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Sun Jan 20, 2013 3:35 am     Reply with quote

Prefix the string with "0x" and pass it to atoi32(). Or write your own decode function that converts the characters nibble-wise.
Ttelmah



Joined: 11 Mar 2010
Posts: 19326

View user's profile Send private message

PostPosted: Sun Jan 20, 2013 3:36 am     Reply with quote

There are a couple of ways you can do this. First, the atoi32 function can do this, but would require you to add the 'hex' header to the text ("0x").
Easiest though is probably just to go manual. So something like:
Code:

//returns numeric value of a hex digit.
int8 hextoval(char chr) { //handles both UC & LC hex
   if (chr<':') return (chr-'0');
   if (chr>='a') return (chr-87);
   return (chr-55);
}

int32 to_int32(char * data_string) {
   int8 ctr;
   int32 val=0;
   for (ctr=0;ctr<8;ctr++) {
      val*=16; //compiler will optimise this to a shift
      val+=hextoval(data_string[ctr]);
   }
   return val;
}


Depending on whether the string might ever be shorter, you could add testing for the characters being hex digits, and terminate early if not (isxdigit).

Best Wishes
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