Skip to content

Instantly share code, notes, and snippets.

@ajcrites
Created November 26, 2018 19:49
Show Gist options
  • Select an option

  • Save ajcrites/1ce7309631b14ae93b2005c1e7939f1a to your computer and use it in GitHub Desktop.

Select an option

Save ajcrites/1ce7309631b14ae93b2005c1e7939f1a to your computer and use it in GitHub Desktop.
const str = 'cd0148035c000f01d20097002b0185030000000000000000000000000000000000000000000000008403dd0159022d033c0334036d0349038503000000000000000000000000000084034b03da00d40077008503000000008403480303003e009c0045002a016c016200860185030000000000000000000000000000000000000000000000000000000000008403b40055017e00c10185030000000000000000000000000000000000000000000000000000000084031c028901fe0185030000000000000000000000000000000000000000000000000000000000008403260026000d001d00850300000000000000000000000000000000000000000000000000000000840348034d038603b600400268029000e402850384005c02d7029502ff0101006e026f008001100307014701a90297003b0386008c005d008a00840341036c034500fd01850315017a002a01d101d100d2008002510353037c006e00df002c0239039b027c00f702d200e9016d01f0001e021303c201d000a800460206013c02de028d00de01b20119022902ca0002019400f00121020801ea00d4019f027c03d00074031a022e010d009c006d036802ed01cc008800d002ec0120017a018f00c0007100c50084036e034e035b036603fb026c038503f9000b013201a10197013d013a02d8003402de002b015e011a02230096012b01de019b02e1009802d3008e02a90149016901c900040259028e027502cf008f012e0332008e01ab01b902a8023a02780236006200af00430104025c00a600000341035501ac019f0037031e02ee0207017103f802d2007502f800d502c901d4002502f0008f003a0016039b01dd00ec01980125032b00410145012703ac01d800b9000c02f1004f00f701e6001803ee022c013302d100e200f202fc002502b200f802ee0274020a008600cc02a10083031701eb0068009c01a6026003d5005a030601b10154006600a40084030e0248036103490185031601f90261033b03d502d3009902ca01d3011d01dc00e8021a028601f3017b01d50069006c020a027e017802fa00dc02a4026e01bd011501a900b801d000c500d600c800a70084037f0350032d0333036c03b5018503f5007501a20283021a014301b701b3018802ef021901b900c102f7004302ab012e01ed02bf0147008d01b8007c020b00200148016e034f016a00d6017a011b02f20047032502ef001200ae01180208008f000e020c0229036c019b004f00ff02ce017b0032009000840384038403840384038403d5002d020a013e00bc0133013d02c60087011e03de004c03a60235033302700330009502a7027d03910389012d016f009a0063035c00b102170304030a006e020a0304033d0145000400d9020200af019803660292030e01fc017f002b03dc010002d201aa0256008e0001015c029800b3027102220124016b00990234028502';
let digs = [];
for (let i = 0; i < str.length / 2; i++) {
const value = parseInt(str.substr(i * 2, 2), 16);
digs.push(value);
}
console.log(digs.map(String.fromCharCode).join(''));
return;
let bitarray = [];
for (let i = 0; i < str.length / 2; i++) {
const value = parseInt(str.substr(i * 2, 2), 16);
bitarray[i * 8 + 0] = (value & 1) > 0;
bitarray[i * 8 + 1] = (value & 2) > 0;
bitarray[i * 8 + 2] = (value & 4) > 0;
bitarray[i * 8 + 3] = (value & 8) > 0;
bitarray[i * 8 + 4] = (value & 16) > 0;
bitarray[i * 8 + 5] = (value & 32) > 0;
bitarray[i * 8 + 6] = (value & 64) > 0;
bitarray[i * 8 + 7] = (value & 128) > 0;
}
const tables = {
upper: 1,
binary: 2,
digit: 3,
};
let latchTable = tables.upper;
let shiftTable = tables.upper;
let index = 0;
const endIndex = bitarray.length;
const result = [];
while (index < endIndex) {
if (shiftTable === tables.binary) {
if (endIndex - index < 5) {
break;
}
let length = readCode(bitarray, index, 5);
index += 5;
if (length == 0) {
if (endIndex - index < 11) {
break;
}
length = readCode(bitarray, index, 11) + 31;
index += 11;
}
for (let charCount = 0; charCount < length; charCount++) {
if (endIndex - index < 8) {
index = endIndex;
break;
}
let code = readCode(bitarray, index, 8);
result.push(code);
index += 8;
}
shiftTable = latchTable;
} else {
let size = shiftTable == tables.digit ? 4 : 5;
if (endIndex - index < size) {
break;
}
let code = readCode(bitarray, index, size);
result.push(code);
index += size;
latchTable = shiftTable;
shiftTable = tables.binary;
}
}
function readCode(bitArray, startIndex, length) {
let res = 0;
for (i = startIndex; i < startIndex + length; i++) {
res <<= 1;
if (bitArray[i]) {
res |= 0x01;
}
}
return res;
}
console.log(result.map(String.fromCharCode).join(''));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment