-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,423
  • 5
  • 63
  • 74

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
  • 128,090
  • 22
  • 218
  • 270
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523