(char) { var value = char.charCodeAt(0); return ((value < 16 ? '0' : '') + value.toString(16)).toUpperCase(); }).join(' '); } function hex2str (hex) { return hex.split(' ').map(function (string) { return String.fromCharCode(parseInt(string, 16)); }).join(''); } /** * PackBits unpack function * * @param {String} data * @return {String} */ function unpackBits (data) { var output = '', i = 0; while (i < data.length) { var hex = data.charCodeAt(i); if (hex == 128) { // Do nothing, nop } else if (hex > 128) { // This is a repeated byte hex = 256 - hex; for (var j = 0; j <= hex; ++j) { output += data.charAt(i + 1); } ++i; } else { // These are literal bytes for (var j = 0; j <= hex; ++j) { output += data.charAt(i + j + 1); } i += j; } ++i; } return output; } var original = 'FE AA 02 80 00 2A FD AA 03 80 00 2A 22 F7 AA', data = unpackBits(hex2str(original)); // Output is: AA AA AA 80 00 2A AA AA AA AA 80 00 2A 22 AA AA AA AA AA AA AA AA AA AA console.log(str2hex(data)); External links Apple webpage describing the PackBits format The TIFF PackBits Algorithm taken from the https://www.fileformat.info site with permission from Corion.net PACKBITS Compression or Why We Support Lossless TIFF Compression Method? the article on site https://www.universal-document-converter.com also describes the algorithm. Lossless compression algorithms