Skip to content

Instantly share code, notes, and snippets.

@MarkLavrynenko
Created March 27, 2015 19:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MarkLavrynenko/bcbaf7c6a0ebffae8e99 to your computer and use it in GitHub Desktop.
Save MarkLavrynenko/bcbaf7c6a0ebffae8e99 to your computer and use it in GitHub Desktop.
NRZI & MLT-3 decoders
var code1 = "0010110111011111001001000010110011011100111001101"
var code2 = "1102201100200102001002011102220011020111000220110"
function decodeMessage(message) {
if (message.length != 49){
throw new Error("Bad length");
}
var ans = [];
for (var i = 1; i < message.length; ++i) {
ans[ans.length] = (message[i] != message[i-1] ? 1 : 0);
}
return ans;
}
function fromBitsToAscii(bits) {
if (bits.length % 8){
throw new Error("Bad length");
}
var ans = "";
while (bits.length) {
var code = 0;
var part = bits.splice(0, 8);
for (var j = 0; j < 8; j++) {
code += part[j] ? (1 << (7 - j)) : 0;
}
ans += String.fromCharCode(code);
}
return ans;
}
var decoded1 = fromBitsToAscii(decodeMessage(code1));
var decoded2 = fromBitsToAscii(decodeMessage(code2));
console.log(decoded1);
console.log(decoded2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment