Go Down

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

EVP

#15
Jun 08, 2012, 12:31 am Last Edit: Jun 08, 2012, 12:38 am by EVP Reason: 1
Code: [Select]
int convertFromHex(int ascii){
 if(ascii > 0x39) ascii -= 7; // adjust for hex letters upper or lower case
 return(ascii & 0xf);
}


This receives an int and returns a int. Is this a joke. What is it's propose?

ohh it adjusts for upper lower. What does   return(ascii & 0xf); do.   & means the address of something. So what does ascii & 0xf mean....

majenko

An int is just a bigger storage area than a byte.  A char is a byte.  You can store a char in an int, but you waste space.

You can replace the "int"s with "char"s.
Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

EVP

Ahh yep get that. but still don't quite understand what the return actually means. Cheers

majenko

& has different meanings in different situations.

"&variablename" is the address of variablename.

"variable & variable" is a logical and between two variables or literals.

"ascii & 0xf" is the value in ascii anded with 0xf.

If ascii contains 0x8B, that is a binary value 10001010.  0xf is 00001111.

And the two together and you get 00001010.

Or, 0xB.  A quirk (or a design) of the ASCII character set places certain character groups on aligned boundaries which can be isolated with simple mathematical operations like this.  Ascii '3' is 0x33.  And that with 0xf (which is the same as 0x0f) and you get 0x03 (or 3 in decimal).
Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

EVP

#19
Jun 08, 2012, 12:49 am Last Edit: Jun 08, 2012, 12:52 am by EVP Reason: 1
Ahh nice one majenko. That answers my question exactly. Thanks.




not sure why i started the last two pasts with 'Ahh' other than i have a very literal phonetic way of typing.

Go Up