Pages: [1]   Go Down
Author Topic: Help with Char to Hex Conversion.  (Read 602 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 25
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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]smiley-cry A, B, C, D, 1, 2, 3);
and I want to convert it to
int value[7]smiley-cry 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 smiley-grin
Logged

0
Offline Offline
Shannon Member
****
Karma: 215
Posts: 12446
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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

[ I won't respond to messages, use the forum please ]

Offline Offline
Newbie
*
Karma: 0
Posts: 25
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the reply MarkT!

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 Offline
Shannon Member
****
Karma: 215
Posts: 12446
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the reply MarkT!

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

[ I won't respond to messages, use the forum please ]

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can anyone give me a sample code for the conversion of char to Hex value?.
for example I have a
char result[7]smiley-cry 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

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Newbie
*
Karma: 0
Posts: 25
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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!  smiley smiley smiley smiley smiley
Logged

Pages: [1]   Go Up
Jump to: