Here’s a utility I whipped up quickly to save out a file from a hex string, as from [NSData description]
— kinda a reverse hexdump.
While doing some debugging, I realised I needed to visualise an intermediate UIImage from the iPhone’s camera. Not being able to use the simulator, and thus be able to write to a file easily, this was my solution: po UIImageJPEGRepresentation(photo, 0.8)
to print out the data as a hex string, then copied it to the clipboard, saved it as a text file, and used an NSScanner to scan in each int, fix the endianness and write it out as a file.
Insane? Maybe, but it did the trick. So, I turned it into a service that takes the selected hex string and writes it to the Desktop.
This is the service: SaveHexStringAsData.service.zip (unzip it to Library/Services
)
In the debugger, print an NSData, then select the hex string (not including the surrounding angle brackets), then select this service. The data will be saved to your Desktop – rename it to the appropriate file type to open it.
Here’s the source:
// Build me with gcc hex2data.m -framework Foundation -framework Cocoa -o hex2data #import <Cocoa/Cocoa.h> int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; NSFileHandle *input = [NSFileHandle fileHandleWithStandardInput]; NSData *inputData = [NSData dataWithData:[input readDataToEndOfFile]]; NSString *inputString = [[NSString alloc] initWithData:inputData encoding:NSUTF8StringEncoding]; NSScanner *scanner = [NSScanner scannerWithString:inputString]; while ( ![scanner isAtEnd] ) { int i; [scanner scanHexInt:&i]; i = ((i & 0xff000000) >> 24) | ((i & 0x00ff0000) >> 8) | ((i & 0x0000ff00) << 8) | ((i & 0xff) << 24); fwrite(&i, sizeof(i), 1, stdout); } [pool drain]; return 0; } |