How do you read a value of size 3 bytes as an integer in C ++?

I read in the id3 tag, where the size of each frame is specified in 3 bytes. How can I use this value as an int?

+6
source share
2 answers

Read each byte and then concatenate them into your int :

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

Make sure you consider the statement.

+9
source

Read the bytes separately and put them in the right places in the int:

 int value = 0; unsigned char byte1 = fgetc(ID3file); unsigned char byte2 = fgetc(ID3file); unsigned char byte3 = fgetc(ID3file); value = (byte1 << 16) | (byte2 << 8) | byte3; 

Edit: It looks like ID3 uses a network (byte) byte order - a modified code to match.

+5
source

Source: https://habr.com/ru/post/911792/


All Articles