Skip to content

Instantly share code, notes, and snippets.

@DarrenSem
Last active October 31, 2022 13:52
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 DarrenSem/ef2ec1326eedfcdceb498db77ce38929 to your computer and use it in GitHub Desktop.
Save DarrenSem/ef2ec1326eedfcdceb498db77ce38929 to your computer and use it in GitHub Desktop.
bufferToHexString.js (and stringsToBuffer and stringFromCharCodeArray)
// bufferToHexString and stringsToBuffer and stringFromCharCodeArray.js
const bufferToHexString = (arraybuffer, delim = "\t", z = "0123456789abcdef") => [...new Uint8Array(arraybuffer)].map(v => z[v >> 4] + z[v & 15]).join(delim);
const bufferToHex_clearest = (arraybuffer, delim = "\t") => [...new Uint8Array(arraybuffer)].map(v => v.toString(16).padStart(2, "0")).join(delim); // .toString(16).padStart = more clear even if slower than using pre-computed HEX[0..255] https://stackoverflow.com/questions/40031688/javascript-arraybuffer-to-hex/55200387#55200387
const bufferToHex_reduce_instead_of_map = arraybuffer => [...new Uint8Array(arraybuffer)].reduce((acc, v) => acc + v.toString(16).padStart(2, "0"), ""); // reduce instead of map because "map is reimplemented for typed arrays to return a typed array for each element, instead of a Uint8" // https://stackoverflow.com/questions/40031688/javascript-arraybuffer-to-hex/70790307#70790307
const HEX = new Array(0xff); for (let i = 0; i <= 0xff; i++)HEX[i] = i.toString(16).padStart(2, "0");
const bufferToHex_precomputed_hex = (arraybuffer, delim = "\t") => [...new Uint8Array(arraybuffer)].map(v => HEX[v]).join(delim);
// "fastest" demonstrated via this JS benchmark website that replaces the old jsperf.com: http://jsben.ch/Vjx2V = https://archive.ph/W4iSF
const bufferToHex_fastest0 = (arraybuffer, z = "0123456789abcdef", y = "") => (new Uint8Array(arraybuffer).forEach((v) => y += z[v >> 4] + z[v & 15]), y); // via https://stackoverflow.com/questions/40031688/javascript-arraybuffer-to-hex/53307879#53307879
const bufferToHex_fastest1 = (arraybuffer, delim = "\t", z = "0123456789abcdef", y = new Array(arraybuffer.length)) => (new Uint8Array(arraybuffer).forEach((v, i) => y[i] = z[v >> 4] + z[v & 15]), y.join(delim));
const bufferToHex_fastest2 = (arraybuffer, delim = "\t", z = "0123456789abcdef") => new Uint8Array(arraybuffer).reduce((acc, v, i) => (acc[i] = z[v >> 4] + z[v & 15], acc), new Array(arraybuffer.length)).join(delim);
// "WINNER!"
const bufferToHex_clearest_fast_enough = (arraybuffer, delim = "\t", z = "0123456789abcdef") => [...new Uint8Array(arraybuffer)].map(v => z[v >> 4] + z[v & 15]).join(delim);
const stringsToBuffer = (...args) => args.flat(Infinity).flatMap(arg => arg == null ? [0] : [...arg].map(c => c.charCodeAt(0)));
console.assert(JSON.stringify(stringsToBuffer('ABC')) === '[65,66,67]', "JSON.stringify(stringsToBuffer('ABC')) === '[65,66,67]'");
console.assert(JSON.stringify(stringsToBuffer(null, 'A', ['', , [, 'BC']])) === '[0,65,66,67]', "JSON.stringify(stringsToBuffer(null, 'A', ['', , [, 'BC']])) === '[0,65,66,67]'");
console.assert(bufferToHex(stringsToBuffer(null, 'A', ['', , [, 'BC']]), ';') === '00;41;42;43', "bufferToHex(stringsToBuffer(null, 'A', ['', , [, 'BC']]), ';') === '00;41;42;43'");
// interesting idea to use this , but apparently it is WAY SLOWER so DO NOT USE in bufferToHex! based on: https://stackoverflow.com/questions/40031688/javascript-arraybuffer-to-hex/70790307#comment97119473_54099484
const stringFromCharCodeArray = charCodeArray => String.fromCharCode(...charCodeArray);
console.assert(stringFromCharCodeArray([13, 0xD, 015]) === "\r\r\r", "stringFromCharCodeArray([single, 3-element, array]) === <CR><CR><CR>");
console.assert(String.fromCharCode(13, 0xD, 015) === "\r\r\r", "String.fromCharCode(3, separate, args) === <CR><CR><CR>");
// bufferToHexString ^ TOP version ^ = bufferToHex_clearest_fast_enough; // "WINNER!"
"bufferToHexString = " + bufferToHexString;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment