Jump to content
  • Advertisement
Sign in to follow this  
DrTwox

28bits from 4 bytes?

This topic is 3609 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello all. Once again I'm stuck and pulling out hair trying to work out how to process this number from an mp3 file header:
Quote:
From id3v2.3.0 Numbers preceded with $ are hexadecimal. The most significant bit (MSB) of a byte is called 'bit 7'. ... The ID3v2 tag size is encoded with four bytes where the most significant bit (bit 7) is set to zero in every byte, making a total of 28 bits. The zeroed bits are ignored, so a 257 bytes long tag is represented as $00 00 02 01
I read those four bytes into an unsigned int right? Then some kind of shift operation? The number is stored big endian; do I 'flip' it before or after processing? (My machine is little endian) I've tried to work it out on paper but I end up with either 192 or 258! Please save my hair! How do I do it?

Share this post


Link to post
Share on other sites
Advertisement
#include <iostream>

unsigned decode(unsigned char *s, unsigned n_bytes) {
unsigned result = 0;

for(unsigned i=0; i<n_bytes; ++i)
result = (result<<7) + s;

return result;
}

int main() {
unsigned char s[4]={0,0,2,1};
std::cout << decode(s,4) << std::endl;
}



The code above will work on machines of any endianness.

Share this post


Link to post
Share on other sites
You also might want to do a data-integrity check, making sure that the MSB of each byte is indeed 0.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!