Two hexdump methods for low-level debugging that have been extremely useful to me this evening.

git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@5076 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Par Winzell
2008-05-11 05:37:50 +00:00
parent 7bbd9baf28
commit 898c62a449
+36
View File
@@ -461,6 +461,42 @@ public class StringUtil
return data;
}
/**
* Return a hexadecimal representation of an unsigned int, potentially left-padded with
* zeroes to arrive at of precisely the requested width, e.g.
* toHex(131, 4) -> "0083"
*/
public static function toHex (n :uint, width :uint) :String
{
var result :String = "";
while (width > 0) {
result = HEX[n & 0x0f] + result;
n >>>= 4;
width --;
}
return result;
}
/**
* Create line-by-line hexadecimal output with a counter, much like the
* 'hexdump' Unix utility. For debugging purposes.
*/
public static function hexdump (bytes :ByteArray) :String
{
var str :String = "";
for (var lineIx :int = 0; lineIx < bytes.length; lineIx += 16) {
str += toHex(lineIx, 4);
for (var byteIx :int = 0; byteIx < 16 && lineIx + byteIx < bytes.length; byteIx ++) {
var b :uint = bytes[lineIx + byteIx];
str += " " + HEX[b >> 4] + HEX[b & 0x0f];
}
str += "\n";
}
return str;
}
/**
* Internal helper function for parseNumber.
*/