CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: converting int to hex BYTE

1. Member
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?

2. Elite Member Power Poster
Join Date
Jan 2006
Location
Singapore
Posts
6,738

## 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. Member
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);```
;

4. Elite Member
Join Date
May 2005
Location
Oregon
Posts
3,725

## 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. Member
Join Date
Oct 2005
Posts
199

## Re: converting int to hex BYTE

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" !

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

## Re: converting int to hex BYTE

Originally Posted by eero_p
Please note that char buffer of size 5 is too small for "0x04e" !
not if you count 0

7. ## 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.

8. Member +
Join Date
Aug 2002
Posts
623

## 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. Member
Join Date
Feb 2006
Location
Croatia - Zagreb
Posts
459

## Re: converting int to hex BYTE

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++'

#### Posting Permissions

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