Skip to content

Instantly share code, notes, and snippets.

@raym
Last active February 27, 2024 07:40
Show Gist options
  • Save raym/7b8cb7b838c94cada0b7 to your computer and use it in GitHub Desktop.
Save raym/7b8cb7b838c94cada0b7 to your computer and use it in GitHub Desktop.
synchsafe conversion functions
/*
Synchsafe integers are used in ID3 tags
http://id3.org/id3v2.4.0-structure (6.2)
https://en.wikipedia.org/wiki/Synchsafe
The why of synchsafe:
http://stackoverflow.com/a/5652842
Example:
255 (%11111111) encoded as a 16 bit synchsafe integer is 383 (%00000001 01111111).
*/
function synchsafe(input) {
var out, mask = 0x7F;
while (mask ^ 0x7FFFFFFF) {
out = input & ~mask;
out = out << 1;
out = out | (input & mask);
mask = ((mask + 1) << 8) - 1;
input = out;
}
return out;
}
function unsynchsafe(input) {
var out = 0, mask = 0x7F000000;
while (mask) {
out = out >> 1;
out = out | (input & mask)
mask = mask >> 8;
}
return out;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment