This discussion is archived
4 Replies Latest reply: Oct 29, 2008 11:58 AM by 807589

Currently Being Moderated
I am working with a file that has a header with the size of the file encoded into 4 bytes like:

>
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 have found someone's code that puts this together into the integer value, but i don't understand why they do each step. Is there anyone here who can explain this code to me?
``````//Read in the bytes (why do they read char[] instead of byte[]?)
char[] tagSize = br.ReadChars(4);    // I use this to read the bytes in from the file

//Store the shifted bytes (why is it int[], not byte[]?)
int[] bytes = new int[4];      // for bit shifting

int size = 0;    // for the final number

/*
* Why are they combining these bytes in this way if they're
* going to again combine them below (in the line setting "size")?
*/

//how do they know they only care about the rightmost bit on the 3rd byte?
//how do they know to shift it 7 to the left?
bytes[3] =  tagSize[3] | ((tagSize[2] & 1) << 7) ;

//Why do they use 63 here (I know it's 111111)?
//how do they know they only want the 3 rightmost of byte 2nd byte?
//And how know to shift it 6 to the left?
bytes[2] = ((tagSize[2] >> 1) & 63) | ((tagSize[1] & 3) << 6) ;
bytes[1] = ((tagSize[1] >> 2) & 31) | ((tagSize[0] & 7) << 5) ;
bytes[0] = ((tagSize[0] >> 3) & 15) ;

//how do they know to shift these bytes the amount that they do to the left?
size  = ((UInt64)bytes[3] | ((UInt64)bytes[2] << 8)  | ((UInt64)bytes[1] << 16) | ((UInt64)bytes[0] << 24)) ;``````
Currently Being Moderated
You've been a member at this forum and at JavaRanch for quite some time. Long enough to know that people answering questions here (and there) don't like to help people who cross post their questions all over the place.

[http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=34&t=010203]
Currently Being Moderated
You've GOT to be kidding! I posted only ONCE on THIS forum. Javaranch is owned by a different company and does NOT have all the same members that this web forum has. Posting the same question on completely different websites' forums is NOT cross-posting. Cross-posting is posting the same thing in different boards of ONE website's forums.
Currently Being Moderated
6tr6tr wrote:
You've GOT to be kidding! I posted only ONCE on THIS forum. Javaranch is owned by a different company and does NOT have all the same members that this web forum has. Posting the same question on completely different websites' forums is NOT cross-posting. Cross-posting is posting the same thing in different boards of ONE website's forums.
Absolute rubbish. Here a couple of links for you:

Wikipedia:
[Crossposting is the act of posting the same message to multiple forums, mailing lists, or newsgroups.|http://en.wikipedia.org/wiki/Crossposting]

JavaRanch's FAQ:
[There might be times when you are inclined to post a question not only at JavaRanch, but also at some other site. When you do so, keep in mind that this is not generally in the interests of the posters who might be responding to your posts - those folks may find they've wasted their time. You certainly don't want to annoy those you want to help you, so you might want to make the experience as painless as possible.|http://faq.javaranch.com/java/BeForthrightWhenCrossPostingToOtherSites]

Sun's FAQ, section C2:
[Post once and in the right area: Multiple postings are discouraged, because they make the category lists longer and create more email traffic for developers who have placed watches on multiple categories. Because of this, duplicate posts are considered a waste of time and an annoyance to many community members, that is, the people who might help you.|http://wikis.sun.com/display/SunForums/Forums.sun.com+FAQ]
Currently Being Moderated
I got the answer on another forum:

The original format uses 7 bits out of each byte. To make that into a 32 bit (actually 32-bit) number, it is first converted to a set of 8-bit bytes.

Bytes[3] is the lowest byte, so it holds 7 bits from tagsize[3] and 1 bit from tagsize[2].
Bytes[2] is the second lowest byte, so it holds the remaining 6 bits form tagsize[2], and 2 bits from tagsize[1].
Bytes[1] is the reamining bits of tagsize[1] and part of tagsize[0]
Bytes[0] is the last bits of the tagsize[0].

The number of bits correspond to the masks used in the & operation, for example 1 bit -> & 1, 2 bits -> & 3 and 6 bits -> & 63

Once we have the bytes values, we can then shuffle it all into a 32-bit integer. As each byte is 8 bits, we need to shift by 0, 8, 16 and 24 bits to form the 32-bit number.