57

When I call -description on an NSData object, I see a pretty Hex string of the NSData object's bytes like:

<f6e7cd28 0fc5b5d4 88f8394b af216506 bc1bba86 4d5b483d>

I'd like to get this representation of the data (minus the lt/gt quotes) into an in-memory NSString so I can work with it.. I'd prefer not to call -[NSData description] and then just trim the lt/gt quotes (because I assume that is not a guaranteed aspect of NSData's public interface and is subject change in the future).

What's the simplest way to get this representation of an NSData object into an NSString object (other than calling -description)?

Todd Ditchendorf
  • 11,217
  • 14
  • 69
  • 123
  • 1
    So, why not description? (Really curious. Suspect I'll learn something.) – Steven Fisher Sep 22 '11 at 19:57
  • 1
    @Steven: `description`'s output may change across system versions. For example, `-[NSDate description]` was, pre-Lion, documented as returning a string with exactly the same format as was required for `-[NSDate initWithString:]`; that guarantee is no longer made. Brief discussion here: http://stackoverflow.com/questions/5837777/nsdates-initwithstring-is-returning-nil/5837852 – jscs Sep 22 '11 at 20:12
  • 3
    `description` should be used for debugging purposes only, because you have no guaranty that the value returned by the `description` method won't change in future releases (even if it is unlikely). For example nothing tells your that Apple won't decide some day that if `[NSData length]>200`, then the string returned by `description` will be clipped in the middle... or anything similar. So this is why its may not be a good practice to rely on what is returned by `description` for anything else than debugging/logging. – AliSoftware Sep 22 '11 at 20:17
  • Makes sense. Thanks. I have to admit, I've provided custom descriptions for my own debugging purposes. – Steven Fisher Sep 23 '11 at 04:05
  • When debugging: Fast way to inspect/view the hex as a readable string (when applicable): http://stackoverflow.com/a/38225931/1054573 – Leonard Pauli Jul 06 '16 at 14:09
  • Possible duplicate of [Best way to serialize an NSData into a hexadeximal string](https://stackoverflow.com/questions/1305225/best-way-to-serialize-an-nsdata-into-a-hexadeximal-string) – Cœur Sep 29 '17 at 07:53
  • I wrote an NSData to hexadecimal NSString category you can find here: https://stackoverflow.com/a/9084784/153040 It's similar to **AliSoftware**'s answer. – Dave Jan 31 '12 at 18:27

9 Answers9

93

Keep in mind that any String(format: ...) solution will be terribly slow (for large data)

NSData *data = ...;
NSUInteger capacity = data.length * 2;
NSMutableString *sbuf = [NSMutableString stringWithCapacity:capacity];
const unsigned char *buf = data.bytes;
NSInteger i;
for (i=0; i<data.length; ++i) {
  [sbuf appendFormat:@"%02X", (NSUInteger)buf[i]];
}

If you need something more performant try this:

static inline char itoh(int i) {
    if (i > 9) return 'A' + (i - 10);
    return '0' + i;
}

NSString * NSDataToHex(NSData *data) {
    NSUInteger i, len;
    unsigned char *buf, *bytes;
    
    len = data.length;
    bytes = (unsigned char*)data.bytes;
    buf = malloc(len*2);
    
    for (i=0; i<len; i++) {
        buf[i*2] = itoh((bytes[i] >> 4) & 0xF);
        buf[i*2+1] = itoh(bytes[i] & 0xF);
    }
    
    return [[NSString alloc] initWithBytesNoCopy:buf
                                          length:len*2
                                        encoding:NSASCIIStringEncoding
                                    freeWhenDone:YES];
}

Swift version


private extension Data {
    var hexadecimalString: String {
        let charA: UInt8 = 0x61
        let char0: UInt8 = 0x30
        func byteToChar(_ b: UInt8) -> Character {
            Character(UnicodeScalar(b > 9 ? charA + b - 10 : char0 + b))
        }
        let hexChars = flatMap {[
            byteToChar(($0 >> 4) & 0xF),
            byteToChar($0 & 0xF)
        ]}
        return String(hexChars)
    }
}
Makyen
  • 31,849
  • 12
  • 86
  • 121
30

I agree on the solution not to call description which is to be reserved for debugging, so good point and good question :)

The easiest solution is to loop thru the bytes of the NSData and construct the NSString from it. Use [yourData bytes] to access the bytes, and build the string into an NSMutableString.

Here is an example by implementing this using a category of NSData

@interface NSData(Hex)
-(NSString*)hexRepresentationWithSpaces_AS:(BOOL)spaces;
@end

@implementation NSData(Hex)
-(NSString*)hexRepresentationWithSpaces_AS:(BOOL)spaces
{
    const unsigned char* bytes = (const unsigned char*)[self bytes];
    NSUInteger nbBytes = [self length];
    //If spaces is true, insert a space every this many input bytes (twice this many output characters).
    static const NSUInteger spaceEveryThisManyBytes = 4UL;
    //If spaces is true, insert a line-break instead of a space every this many spaces.
    static const NSUInteger lineBreakEveryThisManySpaces = 4UL;
    const NSUInteger lineBreakEveryThisManyBytes = spaceEveryThisManyBytes * lineBreakEveryThisManySpaces;
    NSUInteger strLen = 2*nbBytes + (spaces ? nbBytes/spaceEveryThisManyBytes : 0);

    NSMutableString* hex = [[NSMutableString alloc] initWithCapacity:strLen];
    for(NSUInteger i=0; i<nbBytes; ) {
        [hex appendFormat:@"%02X", bytes[i]];
        //We need to increment here so that the every-n-bytes computations are right.
        ++i;

        if (spaces) {
            if (i % lineBreakEveryThisManyBytes == 0) [hex appendString:@"\n"];
            else if (i % spaceEveryThisManyBytes == 0) [hex appendString:@" "];
        }
    }
    return [hex autorelease];
}
@end

Usage:

NSData* data = ...
NSString* hex = [data hexRepresentationWithSpaces_AS:YES];
Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
AliSoftware
  • 32,623
  • 6
  • 82
  • 77
  • 1
    Nitpick: If you want to match description you'd want "%02x", rather than "%02X". (The difference is lowercase vs. uppercase.) – Steven Fisher Sep 23 '11 at 04:09
  • 1
    Nice snippet. I've made it available as gist with minor modifications here: https://gist.github.com/1443455 – Johan Kool Dec 07 '11 at 16:35
  • When I use this solution hex value 90 comes out as ffffff90. What's up with that? – Daniel T. Aug 05 '14 at 14:46
  • your 0x90 input value is a signed integer maybe, instead of unsigned? Explaining the sign bit propagation, and the representation you got, which looks like 2's-complement hex form? What is your exact code? – AliSoftware Aug 05 '14 at 16:03
26

Just wanted to add that @PassKits's method can be written very elegantly using Swift 3 since Data now is a collection.

extension Data { 
    var hex: String {
        var hexString = ""
        for byte in self {
            hexString += String(format: "%02X", byte)
        }

        return hexString
    }
}

Or ...

extension Data {
    var hex: String {
        return self.map { b in String(format: "%02X", b) }.joined()
    }
}

Or even ...

extension Data {
    var hex: String {
        return self.reduce("") { string, byte in
            string + String(format: "%02X", byte)
        }
    }
}
Johannes Lund
  • 1,897
  • 2
  • 19
  • 32
22

I liked @Erik_Aigner's answer the best. I just refactored it a bit:

NSData *data = [NSMutableData dataWithBytes:"acani" length:5];
NSUInteger dataLength = [data length];
NSMutableString *string = [NSMutableString stringWithCapacity:dataLength*2];
const unsigned char *dataBytes = [data bytes];
for (NSInteger idx = 0; idx < dataLength; ++idx) {
    [string appendFormat:@"%02x", dataBytes[idx]];
}
ma11hew28
  • 121,420
  • 116
  • 450
  • 651
8

In Swift you can create an extension.

extension NSData {

    func toHexString() -> String {

        var hexString: String = ""
        let dataBytes =  UnsafePointer<CUnsignedChar>(self.bytes)

        for (var i: Int=0; i<self.length; ++i) {
            hexString +=  String(format: "%02X", dataBytes[i])
        }

        return hexString
    }
}

Then you can simply use:

let keyData: NSData = NSData(bytes: [0x00, 0xFF], length: 2)

let hexString = keyData.toHexString()
println("\(hexString)") // Outputs 00FF
PassKit
  • 12,231
  • 5
  • 57
  • 75
3

Sadly there's no built-in way to produce hex from an NSData, but it's pretty easy to do yourself. The simple way is to just pass successive bytes into sprintf("%02x") and accumulate those into an NSMutableString. A faster way would be to build a lookup table that maps 4 bits into a hex character, and then pass successive nybbles into that table.

Lily Ballard
  • 182,031
  • 33
  • 381
  • 347
1

Seeing there is a Swift 1.2 snippet in the comments, here's the Swift 2 version since C style for loops are deprecated now. Gist with MIT license and two simple unit tests if you care.

Here's the code for your convenience:

import Foundation

extension NSData {
  var hexString: String {
    let pointer = UnsafePointer<UInt8>(bytes)
    let array = getByteArray(pointer)

    return array.reduce("") { (result, byte) -> String in
      result.stringByAppendingString(String(format: "%02x", byte))
    }
  }

  private func getByteArray(pointer: UnsafePointer<UInt8>) -> [UInt8] {
    let buffer = UnsafeBufferPointer<UInt8>(start: pointer, count: length)

    return [UInt8](buffer)
  }
}
pheuberger
  • 15
  • 4
1

While it may not be the most efficient way to do it, if you're doing this for debugging, SSCrypto has a category on NSData which contains two methods to do this (one for creating an NSString of the raw byte values, and one which shows a prettier representation of it).

http://www.septicus.com/SSCrypto/trunk/SSCrypto.m

Steve Streza
  • 253
  • 2
  • 5
0

Assuming you have already set:

NSData *myData = ...;

Simple solution:

NSString *strData = [[NSString alloc]initWithData:myData encoding:NSUTF8StringEncoding];
NSLog(@"%@",strData);
Cyborg
  • 1,437
  • 19
  • 40