Skip to content

Instantly share code, notes, and snippets.

@jeremy-w
Created October 23, 2012 22:22
Show Gist options
  • Save jeremy-w/3942064 to your computer and use it in GitHub Desktop.
Save jeremy-w/3942064 to your computer and use it in GitHub Desktop.
Rewrite logged NSData to UTF-8 text
//clang -framework Foundation -fobjc-arc data-to-str.m -o data-to-str
#import <Foundation/Foundation.h>
#import <sysexits.h>
void
usage(void) {
fprintf(stderr, "usage: %s DATA\n"
" treats data as hex bytes, skips spaces,\n"
" writes UTF-8 text interpretation to stdout\n",
getprogname());
}
int
main(void)
{
@autoreleasepool {
NSProcessInfo *info = [NSProcessInfo processInfo];
NSArray *args = [info arguments];
if ([args count] < 2) {
fprintf(stderr, "error: no string argument supplied\n");
usage();
exit(EX_USAGE);
}
NSString *text = args[1];
const char *s = [text UTF8String];
char buf[3];
buf[2] = '\0';
size_t end = strlen(s);
char *t = malloc(end / 2 + 1);
size_t j = 0;
for (size_t i = 0; i < end; i += 2, j += 1) {
while (s[i] == ' ') i += 1;
if (!s[i]) break;
buf[0] = s[i];
buf[1] = s[i + 1];
long val = strtol(buf, NULL, 16);
t[j] = (char)val;
}
t[j] = '\0';
puts(t);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment