How do you read in a 3 byte size value as an integer in c++?

carboncomputed picture carboncomputed · Mar 27, 2012 · Viewed 13.9k times · Source

I'm reading in an id3 tag where the size of each frame is specified in 3 bytes. How would I be able to utilize this value as an int?

Answer

Carl Norum picture Carl Norum · Mar 27, 2012

Read each byte and then put them together into your int:

int id3 = byte0 + (byte1 << 8) + (byte2 << 16);

Make sure to take endianness into account.