Created
June 17, 2021 09:34
-
-
Save CaledoniaProject/4a8682c9bf29bbb7bb0c4db418a3c385 to your computer and use it in GitHub Desktop.
Improved hexdump with uint8array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function _fillUp(value, count, fillWith) { | |
var l = count - value.length; | |
var ret = ""; | |
while (--l > -1) | |
ret += fillWith; | |
return ret + value; | |
} | |
function hexdump(arrayBuffer, offset, length) { | |
offset = offset || 0; | |
length = length || arrayBuffer.byteLength; | |
var out = _fillUp("Offset", 8, " ") + " 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\n"; | |
var row = ""; | |
for (var i = 0; i < length; i += 16) { | |
row += _fillUp(offset.toString(16).toUpperCase(), 8, "0") + " "; | |
var n = Math.min(16, length - offset); | |
var string = ""; | |
for (var j = 0; j < 16; ++j) { | |
if (j < n) { | |
var value = arrayBuffer[offset] | |
string += value >= 32 ? String.fromCharCode(value) : "."; | |
row += _fillUp(value.toString(16).toUpperCase(), 2, "0") + " "; | |
offset++; | |
} | |
else { | |
row += " "; | |
string += " "; | |
} | |
} | |
row += " " + string + "\n"; | |
} | |
out += row; | |
return out; | |
} | |
var input = '11223344' | |
var buffer = Uint8Array.from(input, x => x.charCodeAt(0)) | |
var hex = hexdump(buffer) | |
console.log(hex) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment