-2

Is it possible to NSLog NSData in base 10. Basically to see byte array of NSData.

I would like to see output something like this: [51, -55, 55, -54, -110]

sash
  • 8,035
  • 4
  • 57
  • 71

1 Answers1

4

You can define a category on NSData to produce a string with decimal data representation, like this:

@interface NSData (DecimalOutput)
-(NSString*)asDecimalString;
@end
@implementation NSData (DecimalOutput)
-(NSString*)asDecimalString {
    NSMutableString *res = [NSMutableString string];
    [res appendString:@"["];
    // Construct an `NSString`, for example by appending decimal representations
    // of individual bytes to the output string
    const char *p = [self bytes];
    NSUInteger len = [self length];
    for (NSUInteger i = 0 ; i != len ; i++) {
        [res appendFormat:@"%i ", p[i]];
    }
    [res appendString:@"]"];
    return res;
}
@end

Now you can use this to NSLog strings in the new format:

NSLog("Data:%@", [myData asDecimalString]);
Sulthan
  • 123,697
  • 21
  • 207
  • 260
Sergey Kalinichenko
  • 697,062
  • 78
  • 1,055
  • 1,465