View previous topic :: View next topic |
Author |
Message |
kmp84
Joined: 02 Feb 2010 Posts: 345
|
Convert ASCII-HEX to int32 hex |
Posted: Sun Jan 20, 2013 3:09 am |
|
|
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
|
|
Posted: Sun Jan 20, 2013 3:35 am |
|
|
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: 19503
|
|
Posted: Sun Jan 20, 2013 3:36 am |
|
|
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 |
|
|
|