Go Down

Topic: Help with Char to Hex Conversion. (Read 639 times) previous topic - next topic

masterraf

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 :D

MarkT

Code: [Select]

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.

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

masterraf

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...

MarkT


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...

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

PeterH


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().
I only provide help via the forum - please do not contact me for private consultancy.

masterraf

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

Go Up