Skip to content

Instantly share code, notes, and snippets.

@igorgatis
Created March 15, 2016 16:42
Show Gist options
  • Save igorgatis/d294fe714a4f523ac3a3 to your computer and use it in GitHub Desktop.
Save igorgatis/d294fe714a4f523ac3a3 to your computer and use it in GitHub Desktop.
Simple hexdump in Javascript
function hexdump(buffer, blockSize) {
blockSize = blockSize || 16;
var lines = [];
var hex = "0123456789ABCDEF";
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 " " + 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);
}
return lines.join("\n");
}
@username1565
Copy link

username1565 commented Nov 28, 2019

Example usage:

var buffer = 'very very long string; very very long string; very very long string; ';
console.log( hexdump( buffer , 16 ) ) ;

As you can see, buffer, this is not ArrayBuffer, just string.

So if anyone want to use ArrayBuffer here, as buffer, you can use the following modification:

function hexdump(buffer, blockSize) {
	
	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";
    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 " " + 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);
    }
    return lines.join("\n");
}

//tests:
var buffer = 'very very long string; very very long string; very very long string;';		//long string	- working
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( hexdump( buffer , 16 ) ) ;

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( 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( hexdump( buffer , 16 ) ) ;

@username1565
Copy link

username1565 commented Nov 28, 2019

Just created fork, here, including modification from @mkropat.

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