-
August 30th, 2005, 06:02 AM
#1
Getting value form hex (example: 0x45 -> 45)
Hi,
I have following problem: from hex value, let's say 0x45 i want to get unsigned char which it this case will should be equal 45. I don't have to consider case where there will be letters in hex value.
I've found a macro which does something like that but in opposite direction, char to hex:
Code:
#define packem(hi, lo) (((unsigned char)(hi) << 4) | ((unsigned char)(lo)))
unsigned char Val = packem(p_v/10, p_v%10);
I know I could do this throught string but I'm looking for something more sophisticated
thanks in advance
-
August 30th, 2005, 06:14 AM
#2
Re: Getting value form hex (example: 0x45 -> 45)
somehting like that:
Code:
int HexVal =0x45;
char szHex[128]={0};
sprintf(szHex,"%x",HexVal);
unsigned char val = ::atoi(szHex);
just note that with hex letter it wont work.
Cheers
-
August 30th, 2005, 06:41 AM
#3
Re: Getting value form hex (example: 0x45 -> 45)
Originally Posted by lechoo
Hi,
I have following problem: from hex value, let's say 0x45 i want to get unsigned char which it this case will should be equal 45. I don't have to consider case where there will be letters in hex value.
I've found a macro which does something like that but in opposite direction, char to hex:
Code:
#define packem(hi, lo) (((unsigned char)(hi) << 4) | ((unsigned char)(lo)))
unsigned char Val = packem(p_v/10, p_v%10);
I know I could do this throught string but I'm looking for something more sophisticated
thanks in advance
What you're looking for is a function that will convert from BCD (Binary-Coded Decimal) format to a normal integer.
Code:
unsigned char bcdValue = 0x45;
unsigned char normalValue = (bcdValue & 0x0F) + ((bcdValue >> 4) * 10);
-
August 30th, 2005, 11:32 AM
#4
Re: Getting value form hex (example: 0x45 -> 45)
Thanks for Your help and BCD tip, now some things make sense.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|