Last active
December 18, 2021 12:18
-
-
Save kapil1024/8a9ad5846f256b77daf9f492828d69ec to your computer and use it in GitHub Desktop.
Hexdump in javascript
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
// Modified version of https://gist.github.com/igorgatis/d294fe714a4f523ac3a3 | |
function hexdump(buffer, blockSize, maxLength) { | |
if(typeof buffer === 'string'){ | |
console.log("buffer is string"); | |
//do nothing | |
}else if(buffer instanceof ArrayBuffer && buffer.byteLength !== undefined){ | |
console.log("buffer is ArrayBuffer"); | |
var tlen = buffer.byteLength; | |
if (tlen > 65000) tlen = 65000; | |
buffer = String.fromCharCode.apply(String, [].slice.call(new Uint8Array(buffer, 0, tlen))); | |
}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"; | |
// do not show more than maxLength bytes | |
maxLength = maxLength || buffer.length; | |
var length = buffer.length; | |
var truncated = false; | |
if (length > maxLength ) { | |
length = maxLength; | |
truncated = true; | |
} | |
for (var b = 0; b < length; b += blockSize) { | |
var block = buffer.slice(b, Math.min(b + blockSize, 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); | |
} | |
if (truncated) { | |
lines.push("Truncated from " + buffer.length + " to " + maxLength); | |
} | |
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 ) ) ; | |
console.log( hexdump( buffer , 16 , 32 ) ) ; // only show 32 bytes at max | |
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 ) ) ; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment