Synchsafe Integer

S

SDS

I'm writing code that writes to an ID3v2 tag for an MP3 file. The tag
header contains 4 bytes that represents the total length of the tag.
It is stored as a 32-bit synchsafe integer. Definition of a 7-bit
synchsafe integer is:

Synchsafe integers are integers that keep its highest bit (bit 7)
zeroed, making seven bits out of eight available. Thus a 32 bit
synchsafe integer can store 28 bits of information.

Example:
255 (%11111111) encoded as a 16 bit synchsafe integer is 383
(%00000001 01111111).

What I need help with is how to convert a 32-bit integer to its 28-bit
equivelent. How would one go about doing this?

Thanks!
 
J

Jon Skeet [C# MVP]

SDS said:
I'm writing code that writes to an ID3v2 tag for an MP3 file. The tag
header contains 4 bytes that represents the total length of the tag.
It is stored as a 32-bit synchsafe integer. Definition of a 7-bit
synchsafe integer is:

Synchsafe integers are integers that keep its highest bit (bit 7)
zeroed, making seven bits out of eight available. Thus a 32 bit
synchsafe integer can store 28 bits of information.

Example:
255 (%11111111) encoded as a 16 bit synchsafe integer is 383
(%00000001 01111111).

What I need help with is how to convert a 32-bit integer to its 28-bit
equivelent. How would one go about doing this?

Something like this:

public static int SynchsafeToInt(int synchsafe)
{
return (synchsafe & 0x7f) |
(synchsafe & 0x7f00) >> 1 |
(synchsafe & 0x7f0000) >> 2 |
(synchsafe & 0x7f000000) >> 3;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top