CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2005
    Posts
    22

    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

  2. #2
    Join Date
    May 2005
    Posts
    4,954

    Re: Getting value form hex (example: 0x45 -> 45)

    somehting like that:
    Code:
      int HexVal =0x45;
      char szHex&#091;128&#093;={0};
      sprintf(szHex,"%x",HexVal);
      unsigned char val = ::atoi(szHex);
    just note that with hex letter it wont work.

    Cheers
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  3. #3
    Join Date
    Jan 2001
    Posts
    588

    Re: Getting value form hex (example: 0x45 -> 45)

    Quote 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);

  4. #4
    Join Date
    Mar 2005
    Posts
    22

    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
  •  





Click Here to Expand Forum to Full Width

Featured