I hacked up toString from iago's Buffer.java to work with tmp's packet buffer.. which I also hacked up, heavily.
2009-12-11 23:39:27.201 CocoaBot[28627:a0f] Buffer contents:
ff 25 44 00 01 00 00 00 02 00 00 00 03 00 00 00 .%D.............
04 00 00 00 05 00 00 00 06 00 00 00 07 00 00 00 ................
08 00 00 00 01 00 00 00 02 00 00 00 03 00 00 00 ................
04 00 00 00 05 00 00 00 06 00 00 00 07 00 00 00 ................
08 00 00 00 ....
Length: 68 (0x44)
PacketBuilder* b = [[PacketBuilder alloc] init];
[b setId:SID_PING];
[b addInt:1];
[b addInt:2];
[b addInt:3];
[b addInt:4];
[b addInt:5];
[b addInt:6];
[b addInt:7];
[b addInt:8];
[b addInt:1];
[b addInt:2];
[b addInt:3];
[b addInt:4];
[b addInt:5];
[b addInt:6];
[b addInt:7];
[b addInt:8];
[b dumphex];
-(void) dumphex {
// fill in header
packetbuf[0] = 0xff;
packetbuf[1] = packetId;
memcpy(packetbuf + 2, &position, 2);
NSMutableString* returnString = [[NSMutableString alloc] init];
[returnString appendString:@"Buffer contents:\n"];
for (int i = 0; i < position; i++) {
// If we're at the end of a line, write out the plaintext
if ((i != 0) && (i % 16 == 0)) {
[returnString appendString:@"\t"];
for (int j = i - 16; j < i; j++) {
// If the character is 'unsafe', use a period
if (packetbuf[j] < 0x20 || packetbuf[j] > 0x7F) {
[returnString appendString:@"."];
} else {
[returnString appendFormat:@"%c", (char)packetbuf[j]];
}
}
[returnString appendString:@"\n"];
}
[returnString appendFormat:@"%02x ", packetbuf[i] & 0xFF];
}
// Add padding spaces if it's not a multiple of 16
if (position != 0 && position % 16 != 0) {
for (int i = 0; i < (16 - position % 16) * 3; i++) {
[returnString appendString:@" "];
}
}
[returnString appendString:@"\t"];
// Print the last line
int i;
if (position > 0 && (position % 16) == 0) {
i = position - 16;
} else {
i = (position - (position % 16));
}
for (; i < position; i++) {
if (packetbuf[i] < 0x20 || packetbuf[i] > 0x7F) {
[returnString appendString:@"."];
} else {
[returnString appendFormat:@"%c", (char)packetbuf[i]];
}
}
[returnString appendFormat:@"\n Length: %d (0x%02x)\n", position, position];
// NSLog it
NSLog(@"%@", returnString);
// release the NSMutableString
[returnString release];
}