Go Down

Topic: hex/byte to int conversion (Read 2 times) previous topic - next topic

Arrch

Quote

You don't need two chars to hold two hex digits, unless they're stored as their character representation

I misinterpreted what he was doing, I thought he was talking about the hex value being in a single char.

mrboni

Thanks guys, a stunning response!

Yes, "they're stored as their character representation" because they've just been read as characters from a text file

the ascii example works great.

However majenko's switch code to convert from hex gives me the following error -"invalid conversion from char* to unsigned char*"    Know what's up?


majenko

I copied it from another one of my projects and modified it to suit on the fly.  Experiment with it.  It'll be the passing of the char * variable.  I had it working with two plain char variables originally.

Just remove the "unsigned" bits from the function definitions.  Those were left over from my other project.
Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Grumpy_Mike

majenko - I thought you were having a joke with that code. But if you were serious then this is much better than using case statements.

Code: [Select]
int convertFromHex(int ascii){
  if(ascii > 0x39) ascii -= 7; // adjust for hex letters upper or lower case
  return(ascii & 0xf);
}

EVP

Mike why is that last method better than using casting? Is their a difference in how it complies?

Go Up