Skip to content

Instantly share code, notes, and snippets.

@username1565
Forked from mkropat/hexdump.js
Last active January 13, 2023 10:31
Show Gist options
  • Save username1565/18878422a72ef0e7f05edf72536b6ed9 to your computer and use it in GitHub Desktop.
Save username1565/18878422a72ef0e7f05edf72536b6ed9 to your computer and use it in GitHub Desktop.
Simple hexdump in Javascript
function hexdump(buffer, blockSize) {
//determine the type of variable "buffer", and convert this to "string".
if(typeof buffer === 'string'){
//console.log("buffer is string");
//do nothing
}else if(buffer instanceof ArrayBuffer && buffer.byteLength !== undefined){
//console.log("buffer is ArrayBuffer");
buffer = String.fromCharCode.apply(String, [].slice.call(new Uint8Array(buffer)));
}else if(Array.isArray(buffer)){
//console.log("buffer is Array");
buffer = String.fromCharCode.apply(String, buffer);
}else if (buffer.constructor === Uint8Array) {
//console.log("buffer is Uint8Array");
buffer = String.fromCharCode.apply(String, [].slice.call(buffer));
}else{
//console.log("Error: buffer is unknown...");
return false;
}
blockSize = blockSize || 16;
var lines = [];
var hex = "0123456789ABCDEF";
lines.push('var byte_array = [', "//hex, //ASCII, //start offset. (block-size = "+blockSize+" bytes/per line)"); //now this like variable, and can be used in script.
for (var b = 0; b < buffer.length; b += blockSize) {
var block = buffer.slice(b, Math.min(b + blockSize, buffer.length));
var addr = ("0000" + b.toString(16)).slice(-4);
var codes = block.split('').map(function (ch) {
var code = ch.charCodeAt(0);
return ' ' + '"0x'+hex[(0xF0 & code) >> 4] + hex[0x0F & code]+'",';
}).join("");
codes += " ".repeat(blockSize - block.length);
var chars = block.replace(/[\x00-\x1F\x20]/g, '.');
chars += " ".repeat(blockSize - block.length);
// lines.push(addr + " " + codes + " " + chars); //old code
lines.push(codes + " //" + chars +" 0x"+addr); //new code
}
lines.push('];');
return lines.join("\n");
}
function decode(base64) { //decode base64
base64 = base64 || '';
return atob(base64.replace(/_/g, '/').replace(/-/g, '+'));
}
function d(base64_encoded_string) {//use base64 encoded string, on input
console.log(hexdump(decode(base64_encoded_string)));
}
function run_tests(){
//tests:
var buffer = 'very very long string; very very long string; very very long string;'; //long string - working
console.log("buffer is string");
console.log( hexdump( buffer , 16 ) ) ;
var buffer = new Uint8Array([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]).buffer; //arrayBuffer - working
console.log("buffer is ArrayBuffer");
console.log( hexdump( buffer , 16 ) ) ;
var buffer = [41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60]; //bytearray - working
console.log("buffer is Array");
console.log( hexdump( buffer , 8 ) ) ;
var buffer = new Uint8Array([21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40]); //Uint8Array - working
console.log("buffer is Uint8Array");
console.log( hexdump( buffer , 4 ) ) ;
console.log("base64-encoded string:");
d('dmVyeSB2ZXJ5IGxvbmcgc3RyaW5nOyB2ZXJ5IHZlcnkgbG9uZyBzdHJpbmc7IHZlcnkgdmVyeSBsb25nIHN0cmluZzsg'); //string base64 - working
console.log("base64-encoded string - from string");
d(window.btoa('very very long string; very very long string; very very long string;')); //convert string to base64 - working
//try to get string, after some time.
setTimeout(
function(){
var buffer = 'blah-blah-blah-blah-very-very-long-blah-blah...';
eval(
hexdump( buffer , 16 )+ //this function return the variable "var byte_array", as text, so we can define this, using eval.
'console.log("\\n\\nbuffer = ", buffer,\n\
"\\n\\nhexdump( buffer , 16 ):\\n"+hexdump( buffer , 16 ),\n\
"\\n\\nbytearray = ", byte_array,\n\
"\\n\\nstring from bytearray = ", byte_array.map(x => String.fromCharCode(x)).join("")\n\
);' //this is a text for second command, to get string from already defined "var byte_array".
)//all this two commands are a string, and running as javascript code, using eval.
}
,
1000 //run this string, after 1000 milliseconds.
);
}
run_tests(); //run tests
@username1565
Copy link
Author

Now, this hex-dump, is an array.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment