Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To encode a synchsafe integer: 1. Convert the decimal value to binary. 2. Split the binary value into 7-bit chunks, starting from the rightmost bit. 3. Prepend each chunk with a zero bit. 4. Put the chunks back together to form a new binary value. 5. Convert the new binary value back to decimal.

Example: To encode the decimal value 300, we first convert it to binary: 100101100. We split this binary value into three 7-bit chunks: 0010110 0, 0110010 0, and 0000001 1. We prepend each chunk with a zero bit to get: 00010110 00, 00110010 00, and 00000001 11. We put these chunks back together to get the binary value 00010110 00001100 00000001 11, which is equal to 7700687 in decimal.

To decode a synchsafe integer: 1. Convert the decimal value to binary. 2. Split the binary value into 8-bit chunks, starting from the leftmost bit. 3. Remove the leftmost zero bit from each chunk. 4. Put the chunks back together to form a new binary value. 5. Convert the new binary value back to decimal.

Example: Given the synchsafe integer 7700687, we convert it to binary: 11101000 01100110 00000010 11. We split this binary value into four 8-bit chunks: 11101000, 01100110, 00000010, and 11. We remove the leftmost zero bit from each chunk to get: 1110100, 1100110, 0000010, and 1. We put these chunks back together to get the binary value 11101001 10011000 00001001, which is equal to 300 in decimal.