billywhizz
10/14/2010 - 7:22 PM

buffer.pprint.js

Buffer.prototype.pprint = function() {
	var line = "";
	if(this.length > 0) {
		for(var i=0; i<this.length; i++) {
			if(i%8==0) {
				sys.print(" ");
			}
			if(i%16==0) {
				sys.print(line);
				line = "";			
				sys.print("\n");
				var ss = "00000000" + i.toString(10);
				sys.print(ss.slice(ss.length - 8) + ": ");
			}
			if(this[i] > 15) {
				sys.print(this[i].toString(16) + " ");
			}
			else {
				sys.print("0" + this[i].toString(16) + " ");
			}
			if(this[i] >= 0x20 && this[i] <= 0x7e) {
				line += String.fromCharCode(this[i]);
			}
			else {
				line += ".";
			}
		}
		sys.print("\n");
	}
}