converting int to hex BYTE
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: converting int to hex BYTE

  1. #1
    Join Date
    Feb 2006
    Location
    Croatia - Zagreb
    Posts
    459

    converting int to hex BYTE

    Hi
    How to convert integer to hex value BYTE in style like this:
    BYTE val = 0x04e; witch stands for 78 decimal but now I have 78 decimal and need to write value of 78 hex to BYTE val. How to do that?
    You just divided by zero, didn't you?

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,315

    Re: converting int to hex BYTE

    BYTE val = 0x04e; witch stands for 78 decimal but now I have 78 decimal and need to write value of 78 hex to BYTE val.
    Um, 78 in decimal is 0x4e in hex, so it doesnt really matter which one you have, does it? At least so long as it fits into a BYTE.

  3. #3
    Join Date
    Feb 2006
    Location
    Croatia - Zagreb
    Posts
    459

    Re: converting int to hex BYTE

    not that I need to convert char array hex number to BYTE but nevermind i found it this is how it is done:
    Code:
    char lo[5];
    strcpy(lo,"0x04e");
    BYTE val;
    sscanf(lo, "%x", &val);
    printf("%x",val);
    ;
    You just divided by zero, didn't you?

  4. #4
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,716

    Re: converting int to hex BYTE

    or Either by this way.
    Code:
    char *lo="0x04e";
    BYTE val;
    sscanf(lo, "%x", &val);
    or by this

    Code:
    char lo[6]="0x04e";
    BYTE val;
    sscanf(lo, "%x", &val);

  5. #5
    Join Date
    Oct 2005
    Posts
    199

    Re: converting int to hex BYTE

    Quote Originally Posted by Odiee
    not that I need to convert char array hex number to BYTE but nevermind i found it this is how it is done:
    Code:
    char lo[5];
    strcpy(lo,"0x04e");
    BYTE val;
    sscanf(lo, "%x", &val);
    printf("%x",val);
    ;
    Please note that char buffer of size 5 is too small for "0x04e" !
    'You help me, and I, in turn, am helped by you!'
    -H.S.

  6. #6
    Join Date
    Feb 2006
    Location
    Croatia - Zagreb
    Posts
    459

    Re: converting int to hex BYTE

    Quote Originally Posted by eero_p
    Please note that char buffer of size 5 is too small for "0x04e" !
    not if you count 0
    You just divided by zero, didn't you?

  7. #7
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588

    Re: converting int to hex BYTE

    lo[0] = '0'
    lo[1] = 'x'
    lo[2] = '0'
    lo[3] = '4'
    lo[4] = 'e'
    lo[5] = '\0' (terminating null)
    And declaring char lo[5] will only let you access lo[0] to lo[4] (5 characters), whereas you really need 6 because of the terminating null.
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  8. #8

    Re: converting int to hex BYTE

    Code:
    char szBuffer[5] = "0x4E";
    unsigned char ucValue(0),ucTempValue(0);
    if (szBuffer[2] >= '0' || szBuffer[2] <= '9')
    {
        ucTempValue = static_cast<unsigned char>(szBuffer[2] - '0');
        ucValue |=  (ucValue << 4); // shift over as high bits
    }
    else if (szBuffer[2] >= 'A' && szBuffer[2] <= 'F')
    {
        ucTempValue = static_cast<unsigned char>(szBuffer[2] - 'A') + 10;
        ucValue |= (ucValue << 4); // shift over as high bits
    }
    
    if (szBuffer[3] >= '0' || szBuffer[3] <= '9')
    {
        ucTempValue = static_cast<unsigned char>(szBuffer[3] - '0');
        ucValue |=  ucValue// don't shift over, low bits
    }
    else if (szBuffer[3] >= 'A' && szBuffer[3] <= 'F')
    {
        ucTempValue = static_cast<unsigned char>(szBuffer[3] - 'A') + 10;
        ucValue |= ucValue; // don't shift over, low bits
    }
    Last edited by JamesSchumacher; June 8th, 2006 at 07:17 PM.

  9. #9
    Join Date
    Feb 2006
    Location
    Croatia - Zagreb
    Posts
    459

    Re: converting int to hex BYTE

    Quote Originally Posted by Yves M
    lo[0] = '0'
    lo[1] = 'x'
    lo[2] = '0'
    lo[3] = '4'
    lo[4] = 'e'
    lo[5] = '\0' (terminating null)
    And declaring char lo[5] will only let you access lo[0] to lo[4] (5 characters), whereas you really need 6 because of the terminating null.
    Yes of course. let this be note to you guys, 'c' is not 'c++'
    You just divided by zero, didn't you?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center