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

# Thread: Convert a 3-byte array into a single size integer?

1. Junior Member
Join Date
Nov 2005
Posts
3

## Convert a 3-byte array into a single size integer?

I have a file I am trying to decode and the size of the portion of the file I need is specified by 3 bytes in its header. How can I convert these 3 separate bytes into 1 single integer?

2. ## Re: Convert a 3-byte array into a single size integer?

What, exactly, does each of the bytes represent? Are they part of a single 24 bit integer?

Viggy

3. Junior Member
Join Date
Nov 2005
Posts
3

## Re: Convert a 3-byte array into a single size integer?

Originally Posted by MrViggy
What, exactly, does each of the bytes represent? Are they part of a single 24 bit integer?

Viggy
Yes they are part of a single 24 bit integer, but I can't figure out how to get the program to recognize them as a single integer, and not 3 different bytes in an array...

4. Senior Member
Join Date
Jun 2002
Posts
1,417

## Re: Convert a 3-byte array into a single size integer?

I don't know how to create a 24-bit integer, but if it were a 32-bit integer conversion would look something like this, where buffer contains the binary representation of a 4-byte integer. I assume a 24-bit integer would be similar. I know short = 16 bit integer, long = 32 bit. I don't know what is a 24 bit. Yes, I know those sizes are not guarenteed, but that's what they are on 16 and 32 bit operating systems such as MS-DOS, MS-Windows and *nix.
Code:
int n = *(int *)buffer;

5. Member +
Join Date
Jul 2005
Posts
767

## Re: Convert a 3-byte array into a single size integer?

Is this not a possible solution
Code:
BYTE arr[3] = {0x23, 0x45, 0x67};
UINT val = arr[2] << 16 | arr[1] << 8 | arr[0];
I have assumed that arr[2] is the MSB and arr[0] is LSB.

6. Junior Member
Join Date
Nov 2005
Posts
3

## Re: Convert a 3-byte array into a single size integer?

Thanks for the help it worked perfectly!

7. Junior Member
Join Date
Jan 2009
Location
AZ
Posts
18

## Re: Convert a 3-byte array into a single size integer?

anyone know how to do this in .net?

8. Senior Member
Join Date
Aug 2005
Location
San Diego, CA
Posts
1,046

## Re: Convert a 3-byte array into a single size integer?

Originally Posted by DaBoonDockSaint
anyone know how to do this in .net?
Yes, in the exact same way. What does using .Net have to do with anything if it is a c++ program? You still have the same problem and the same kind of solution.

#### Posting Permissions

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