Pages: [1]   Go Down
 Author Topic: Help with Char to Hex Conversion.  (Read 390 times) 0 Members and 1 Guest are viewing this topic.
Offline
Newbie
Karma: 0
Posts: 25
 « on: December 28, 2012, 12:26:02 pm » Bigger Smaller Reset

Hello Good morning everyone!
Can anyone give me a sample code for the conversion of char to Hex value?.
for example I have a
char result[7] A, B, C, D, 1, 2, 3);
and I want to convert it to
int value[7] 0xA, 0xB, 0xC, 0xD, 0x1, 0x2, 0x3);

because later I want to convert it to something like this
result= 0xABCD123;

all i know is that for me to combine the hex values is to move bits like
result=value;
result<<4   (4 since one HEX is 4 bits? is it correct?)

but i keep having troubles on converting that char to an integer.
I tried using strtol but to no vain.

Thank you to anyone that will help. Any kind of suggestions are welcomed and sorry for my bad english
 Logged

0
Offline
Tesla Member
Karma: 76
Posts: 6849
Arduino rocks
 « Reply #1 on: December 28, 2012, 12:33:14 pm » Bigger Smaller Reset

Code:
char result [7] = { 'A','B','C','D','1','2','3' } ;

int value [7] ;

for (byte i = 0 ; i < 7 ; i++)
value [i] = hex2bin (result[i]) ;

long res = 0L ;
for (byte i = 0 ; i < 7 ; i++)
{
res <<= 4 ;
res += value [i] ;
}

// or combine the two
for (byte i = 0 ; i < 7 ; i++)
{
res <<= 4 ;
res += hex2bin (result [i]) ;
}

// and you'll need an implementation of hex2bin, something like
int nex2bin (char c)
{
if (c >= '0' && c <= '9')
return c - '0' ;
if (c >= 'A' && c << 'F')
return c - 'A' + 10 ;
if (c >= 'a' && c << 'f')
return c - 'f' + 10 ;
}

// but that isn't complete of course, no error checks.

 Logged

Offline
Newbie
Karma: 0
Posts: 25
 « Reply #2 on: December 28, 2012, 12:55:01 pm » Bigger Smaller Reset

So you mean I have to call a seperate function that converts whatever 'char' from 0-F to its HEX equivalent? right?
hahaha Why didn't i think of that! thanks!

But still I have a few questions...
what does res =0L; means? what is 0L?

also what does this mean..

if (c >= '0' && c <= '9')
return c - '0' ;
if (c >= 'A' && c << 'F')
return c - 'A' + 10 ;
if (c >= 'a' && c << 'f')
return c - 'f' + 10 ;

why does it test if the newly declared char c >= '0'?
isn't it that since c is a char it cannot be compared by greater or equal to? (Correct me if 'm wrong...I'm kinda new to this)
so basically what im asking is what does the condition if (c >= '0' && c <= '9') test?

also what does  return c - 'f' + 10 ; means? what value does it actually returns?

Thank you again and sorry for my ignorance...
 Logged

0
Offline
Tesla Member
Karma: 76
Posts: 6849
Arduino rocks
 « Reply #3 on: December 28, 2012, 01:16:24 pm » Bigger Smaller Reset

So you mean I have to call a seperate function that converts whatever 'char' from 0-F to its HEX equivalent? right?
hahaha Why didn't i think of that! thanks!

No you don't have to, but its much easier to read well-structured code.
Quote

But still I have a few questions...
what does res =0L; means? what is 0L?
long integer constants should have an 'L' at the end to tell the compiler you mean a long constant,
often it doesn't matter, but its a good habit to get into if you don't want to be surprised later on.
Quote

also what does this mean..

if (c >= '0' && c <= '9')
return c - '0' ;
if (c >= 'A' && c << 'F')
return c - 'A' + 10 ;
if (c >= 'a' && c << 'f')
return c - 'f' + 10 ;

why does it test if the newly declared char c >= '0'?
isn't it that since c is a char it cannot be compared by greater or equal to? (Correct me if 'm wrong...I'm kinda new to this)
so basically what im asking is what does the condition if (c >= '0' && c <= '9') test?
chars are just a kind of integer, namely 8 bit signed integer.  The character coding is ASCII (for the first 128 codes at least),
so you can rely on 0..9, A..F and a..f being contiguous code ranges.
Quote

also what does  return c - 'f' + 10 ; means? what value does it actually returns?
subtract the code for 'f' from the char c, then add ten.  I got that wrong BTW, it should be 'a', not 'f'.
Quote

Thank you again and sorry for my ignorance...

 Logged

UK
Offline
Tesla Member
Karma: 100
Posts: 6784
-
 « Reply #4 on: December 28, 2012, 01:19:02 pm » Bigger Smaller Reset

Can anyone give me a sample code for the conversion of char to Hex value?.
for example I have a
char result[7] A, B, C, D, 1, 2, 3);
(snip)
because later I want to convert it to something like this
result= 0xABCD123;

If your array is populated with ASCII characters then you could just make sure it was null-terminated and parse it directly to an integer using strtol() or strtoul().
 Logged

Offline
Newbie
Karma: 0
Posts: 25
 « Reply #5 on: December 28, 2012, 01:27:33 pm » Bigger Smaller Reset

Wow...thanks...now I completely understand it...thanks a lot for your help and patience.
Will try this now on my project. Thanks Again!
 Logged

 Pages: [1]   Go Up