103

I am looking for a nice-cocoa way to serialize an NSData object into a hexadecimal string. The idea is to serialize the deviceToken used for notification before sending it to my server.

I have the following implementation, but I am thinking there must be some shorter and nicer way to do it.

+ (NSString*) serializeDeviceToken:(NSData*) deviceToken
{
    NSMutableString *str = [NSMutableString stringWithCapacity:64];
    int length = [deviceToken length];
    char *bytes = malloc(sizeof(char) * length);

    [deviceToken getBytes:bytes length:length];

    for (int i = 0; i < length; i++)
    {
        [str appendFormat:@"%02.2hhX", bytes[i]];
    }
    free(bytes);

    return str;
}
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
sarfata
  • 4,625
  • 4
  • 28
  • 36

15 Answers15

209

This is a category applied to NSData that I wrote. It returns a hexadecimal NSString representing the NSData, where the data can be any length. Returns an empty string if NSData is empty.

NSData+Conversion.h

#import <Foundation/Foundation.h>

@interface NSData (NSData_Conversion)

#pragma mark - String Conversion
- (NSString *)hexadecimalString;

@end

NSData+Conversion.m

#import "NSData+Conversion.h"

@implementation NSData (NSData_Conversion)

#pragma mark - String Conversion
- (NSString *)hexadecimalString {
    /* Returns hexadecimal string of NSData. Empty string if data is empty.   */

    const unsigned char *dataBuffer = (const unsigned char *)[self bytes];

    if (!dataBuffer)
        return [NSString string];

    NSUInteger          dataLength  = [self length];
    NSMutableString     *hexString  = [NSMutableString stringWithCapacity:(dataLength * 2)];

    for (int i = 0; i < dataLength; ++i)
        [hexString appendString:[NSString stringWithFormat:@"%02lx", (unsigned long)dataBuffer[i]]];

    return [NSString stringWithString:hexString];
}

@end

Usage:

NSData *someData = ...;
NSString *someDataHexadecimalString = [someData hexadecimalString];

This is "probably" better than calling [someData description] and then stripping the spaces, <'s, and >'s. Stripping characters just feels too "hacky". Plus you never know if Apple will change the formatting of NSData's -description in the future.

NOTE: I have had people reach out to me about licensing for the code in this answer. I hereby dedicate my copyright in the code I posted in this answer to the public domain.

Dave
  • 12,408
  • 12
  • 64
  • 67
  • 4
    Nice, but two suggestions: (1) I think appendFormat is more efficient for large data since it avoids creating an intermediate NSString and (2) %x represents an unsigned int rather than unsigned long, although the difference is harmless. – svachalek May 09 '12 at 23:28
  • Not to be a naysayer, since this is a good solution that is easy to use but [my solution](http://stackoverflow.com/a/9009321/1121655) on Jan 25 is far more efficient. If you are looking for a performance optimized answer, see that [answer](http://stackoverflow.com/a/9009321/1121655). Upvoting this answer as a nice, easy to understand solution. – NSProgrammer Sep 16 '12 at 03:44
  • 5
    I had to remove the (unsigned long) cast and use @"%02hhx" as the format string to make this work. – Anton Sep 25 '12 at 00:13
  • I, too, had to use @"%02hhx", but otherwise, great answer. – Jacob Nov 06 '12 at 19:47
  • 1
    Right, per https://developer.apple.com/library/ios/documentation/cocoa/conceptual/Strings/Articles/formatSpecifiers.html#//apple_ref/doc/uid/TP40004265-SW1 the format should be `"%02lx"` with that cast, or cast to `(unsigned int)`, or drop the cast and use `@"%02hhx"` :) – qix Sep 05 '13 at 07:17
  • Thanks guys, I altered the answer to use `%02lx` instead. – Dave Sep 23 '13 at 01:32
  • 1
    `[hexString appendFormat:@"%02x", (unsigned int)dataBuffer[i]];` is much better (smaller memory footprint) – Marek R Oct 31 '13 at 15:17
  • The @NSProgrammer solution is about 50 times faster ( Tested ). – Moose Nov 03 '15 at 09:41
  • Just added a method that is about 170 times faster - the fastest ( for now ) on this page. – Moose Nov 03 '15 at 14:11
  • iOS newbie: can anyone tell me where to put the files NSData+Conversion.h and NSData+Conversion.m and how to get them loaded into the app so that I can call "deviceToken hexadecimalString" in my didRegisterForRemoteNotificationsWithDeviceToken block? – stcorbett Nov 28 '15 at 22:18
32

Here's a highly optimized NSData category method for generating a hex string. While @Dave Gallagher's answer is sufficient for a relatively small size, memory and cpu performance deteriorate for large amounts of data. I profiled this with a 2MB file on my iPhone 5. Time comparison was 0.05 vs 12 seconds. Memory footprint is negligible with this method while the other method grew the heap to 70MBs!

- (NSString *) hexString
{
    NSUInteger bytesCount = self.length;
    if (bytesCount) {
        const char *hexChars = "0123456789ABCDEF";
        const unsigned char *dataBuffer = self.bytes;
        char *chars = malloc(sizeof(char) * (bytesCount * 2 + 1));       
        if (chars == NULL) {
            // malloc returns null if attempting to allocate more memory than the system can provide. Thanks Cœur
            [NSException raise:NSInternalInconsistencyException format:@"Failed to allocate more memory" arguments:nil];
            return nil;
        }
        char *s = chars;
        for (unsigned i = 0; i < bytesCount; ++i) {
            *s++ = hexChars[((*dataBuffer & 0xF0) >> 4)];
            *s++ = hexChars[(*dataBuffer & 0x0F)];
            dataBuffer++;
        }
        *s = '\0';
        NSString *hexString = [NSString stringWithUTF8String:chars];
        free(chars);
        return hexString;
    }
    return @"";
}
junjie
  • 7,946
  • 2
  • 26
  • 26
Peter
  • 2,005
  • 1
  • 20
  • 14
  • Nice one @Peter - There is however an even faster ( not much than your's ) solution - just below ;) – Moose Nov 03 '15 at 14:38
  • 2
    @Moose, please reference more precisely which answer you are talking about: votes and new answers may affect positioning of answers. [edit: oh, let me guess, you mean your own answer...] – Cœur Sep 29 '17 at 02:55
  • 1
    Added malloc null check. Thanks @Cœur. – Peter Mar 08 '18 at 19:03
17

Using the description property of NSData should not be considered an acceptable mechanism for HEX encoding the string. That property is for description only and can change at any time. As a note, pre-iOS, the NSData description property didn't even return it's data in hex form.

Sorry for harping on the solution but it's important to take the energy to serialize it without piggy-backing off an API that is meant for something else other than data serialization.

@implementation NSData (Hex)

- (NSString*)hexString
{
    NSUInteger length = self.length;
    unichar* hexChars = (unichar*)malloc(sizeof(unichar) * (length*2));
    unsigned char* bytes = (unsigned char*)self.bytes;
    for (NSUInteger i = 0; i < length; i++) {
        unichar c = bytes[i] / 16;
        if (c < 10) {
            c += '0';
        } else {
            c += 'A' - 10;
        }
        hexChars[i*2] = c;

        c = bytes[i] % 16;
        if (c < 10) {
            c += '0';
        } else {
            c += 'A' - 10;
        }
        hexChars[i*2+1] = c;
    }
    NSString* retVal = [[NSString alloc] initWithCharactersNoCopy:hexChars length:length*2 freeWhenDone:YES];
    return [retVal autorelease];
}

@end
NSProgrammer
  • 2,387
  • 1
  • 24
  • 27
  • however, you have to free(hexChars) before the return. – karim Nov 11 '14 at 11:15
  • 3
    @karim, that is incorrect. By using initWithCharactersNoCopy:length:freeWhenDone: and having freeWhenDone be YES, the NSString will take control of that byte buffer. Calling free(hexChars) will result in a crash. The benefit here is substantial since NSString won't have to make an expensive memcpy call. – NSProgrammer Nov 11 '14 at 17:15
  • @NSProgrammer thanks. I did not notice the NSSting initializer. – karim Nov 12 '14 at 09:12
  • The documentation states that `description` returns a hex encoded string, so that seems reasonable to me. – Uncommon Jul 17 '17 at 19:51
  • shouldn't we check for malloc returned value being potentially null? – Cœur Oct 13 '17 at 05:30
10

Here is a faster way to do the conversion:

BenchMark (mean time for a 1024 bytes data conversion repeated 100 times):

Dave Gallagher : ~8.070 ms
NSProgrammer : ~0.077 ms
Peter : ~0.031 ms
This One : ~0.017 ms

@implementation NSData (BytesExtras)

static char _NSData_BytesConversionString_[512] = "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff";

-(NSString*)bytesString
{
    UInt16*  mapping = (UInt16*)_NSData_BytesConversionString_;
    register UInt16 len = self.length;
    char*    hexChars = (char*)malloc( sizeof(char) * (len*2) );

    // --- Coeur's contribution - a safe way to check the allocation
    if (hexChars == NULL) {
    // we directly raise an exception instead of using NSAssert to make sure assertion is not disabled as this is irrecoverable
        [NSException raise:@"NSInternalInconsistencyException" format:@"failed malloc" arguments:nil];
        return nil;
    }
    // ---

    register UInt16* dst = ((UInt16*)hexChars) + len-1;
    register unsigned char* src = (unsigned char*)self.bytes + len-1;

    while (len--) *dst-- = mapping[*src--];

    NSString* retVal = [[NSString alloc] initWithBytesNoCopy:hexChars length:self.length*2 encoding:NSASCIIStringEncoding freeWhenDone:YES];
#if (!__has_feature(objc_arc))
   return [retVal autorelease];
#else
    return retVal;
#endif
}

@end
Moose
  • 2,607
  • 24
  • 23
  • 1
    You can see how I implemented the malloc check here (`_hexString` method): https://github.com/ZipArchive/ZipArchive/blob/master/SSZipArchive/SSZipArchive.m#L1044 – Cœur Nov 10 '17 at 12:57
  • Thanks for the reference - BTW I like the 'too lengthy' - That's true, but now I've typed it, anyone can copy/paste - I'm joking - I have generated it - You knew already :) You are right on the lengthy, I was just trying to hit everywhere I can win microseconds! It divide the loop iterations by 2. But I admit it lacks elegance. Bye – Moose Nov 15 '17 at 19:10
8

Functional Swift version

One liner:

let hexString = UnsafeBufferPointer<UInt8>(start: UnsafePointer(data.bytes),
count: data.length).map { String(format: "%02x", $0) }.joinWithSeparator("")

Here's in a reusable and self documenting extension form:

extension NSData {
    func base16EncodedString(uppercase uppercase: Bool = false) -> String {
        let buffer = UnsafeBufferPointer<UInt8>(start: UnsafePointer(self.bytes),
                                                count: self.length)
        let hexFormat = uppercase ? "X" : "x"
        let formatString = "%02\(hexFormat)"
        let bytesAsHexStrings = buffer.map {
            String(format: formatString, $0)
        }
        return bytesAsHexStrings.joinWithSeparator("")
    }
}

Alternatively, use reduce("", combine: +) instead of joinWithSeparator("") to be seen as a functional master by your peers.


Edit: I changed String($0, radix: 16) to String(format: "%02x", $0), because one digit numbers needed to having a padding zero

NiñoScript
  • 4,523
  • 2
  • 27
  • 33
7

Peter's answer ported to Swift

func hexString(data:NSData)->String{
    if data.length > 0 {
        let  hexChars = Array("0123456789abcdef".utf8) as [UInt8];
        let buf = UnsafeBufferPointer<UInt8>(start: UnsafePointer(data.bytes), count: data.length);
        var output = [UInt8](count: data.length*2 + 1, repeatedValue: 0);
        var ix:Int = 0;
        for b in buf {
            let hi  = Int((b & 0xf0) >> 4);
            let low = Int(b & 0x0f);
            output[ix++] = hexChars[ hi];
            output[ix++] = hexChars[low];
        }
        let result = String.fromCString(UnsafePointer(output))!;
        return result;
    }
    return "";
}

swift3

func hexString()->String{
    if count > 0 {
        let hexChars = Array("0123456789abcdef".utf8) as [UInt8];
        return withUnsafeBytes({ (bytes:UnsafePointer<UInt8>) -> String in
            let buf = UnsafeBufferPointer<UInt8>(start: bytes, count: self.count);
            var output = [UInt8](repeating: 0, count: self.count*2 + 1);
            var ix:Int = 0;
            for b in buf {
                let hi  = Int((b & 0xf0) >> 4);
                let low = Int(b & 0x0f);
                output[ix] = hexChars[ hi];
                ix += 1;
                output[ix] = hexChars[low];
                ix += 1;
            }
            return String(cString: UnsafePointer(output));
        })
    }
    return "";
}

Swift 5

func hexString()->String{
    if count > 0 {
        let hexChars = Array("0123456789abcdef".utf8) as [UInt8];
        return withUnsafeBytes{ bytes->String in
            var output = [UInt8](repeating: 0, count: bytes.count*2 + 1);
            var ix:Int = 0;
            for b in bytes {
                let hi  = Int((b & 0xf0) >> 4);
                let low = Int(b & 0x0f);
                output[ix] = hexChars[ hi];
                ix += 1;
                output[ix] = hexChars[low];
                ix += 1;
            }
            return String(cString: UnsafePointer(output));
        }
    }
    return "";
}
john07
  • 562
  • 6
  • 16
4

I needed to solve this problem and found the answers here very useful, but I worry about performance. Most of these answers involve copying the data in bulk out of NSData so I wrote the following to do the conversion with low overhead:

@interface NSData (HexString)
@end

@implementation NSData (HexString)

- (NSString *)hexString {
    NSMutableString *string = [NSMutableString stringWithCapacity:self.length * 3];
    [self enumerateByteRangesUsingBlock:^(const void *bytes, NSRange byteRange, BOOL *stop){
        for (NSUInteger offset = 0; offset < byteRange.length; ++offset) {
            uint8_t byte = ((const uint8_t *)bytes)[offset];
            if (string.length == 0)
                [string appendFormat:@"%02X", byte];
            else
                [string appendFormat:@" %02X", byte];
        }
    }];
    return string;
}

This pre-allocates space in the string for the entire result and avoids ever copying the NSData contents out by using enumerateByteRangesUsingBlock. Changing the X to an x in the format string will use lowercase hex digits. If you don't want a separator between the bytes you can reduce the statement

if (string.length == 0)
    [string appendFormat:@"%02X", byte];
else
    [string appendFormat:@" %02X", byte];

down to just

[string appendFormat:@"%02X", byte];
John Stephen
  • 7,625
  • 2
  • 31
  • 45
  • 2
    I believe the index to retrieve the byte value needs adjustment, because the `NSRange` indicates the range within the larger `NSData` representation, not within the smaller bytes buffer (that first parameter of the block supplied to `enumerateByteRangesUsingBlock`) that represents a single contiguous portion of the larger `NSData`. Thus, `byteRange.length` reflects the size of the bytes buffer, but the `byteRange.location` is the location within the larger `NSData`. Thus, you want to use simply `offset`, not `byteRange.location + offset`, to retrieve the byte. – Rob Aug 01 '14 at 20:16
  • 1
    @Rob Thanks, I see what you mean and have adjusted the code – John Stephen Aug 01 '14 at 22:30
  • 1
    If you modify the statement down to just use the single `appendFormat` you should probably also change the `self.length * 3` to `self.length * 2` – T. Colligan Jun 15 '16 at 19:24
1

Here is a solution using Swift 3

extension Data {

    public var hexadecimalString : String {
        var str = ""
        enumerateBytes { buffer, index, stop in
            for byte in buffer {
                str.append(String(format:"%02x",byte))
            }
        }
        return str
    }

}

extension NSData {

    public var hexadecimalString : String {
        return (self as Data).hexadecimalString
    }

}
Alex
  • 8,801
  • 5
  • 27
  • 31
1

I needed an answer that would work for variable length strings, so here's what I did:

+ (NSString *)stringWithHexFromData:(NSData *)data
{
    NSString *result = [[data description] stringByReplacingOccurrencesOfString:@" " withString:@""];
    result = [result substringWithRange:NSMakeRange(1, [result length] - 2)];
    return result;
}

Works great as an extension for the NSString class.

BadPirate
  • 25,802
  • 10
  • 92
  • 123
1

You can always use [yourString uppercaseString] to capitalize letters in data description

1

A better way to serialize/deserialize NSData into NSString is to use the Google Toolbox for Mac Base64 encoder/decoder. Just drag into your App Project the files GTMBase64.m, GTMBase64.h e GTMDefines.h from the package Foundation and the do something like

/**
 * Serialize NSData to Base64 encoded NSString
 */
-(void) serialize:(NSData*)data {

    self.encodedData = [GTMBase64 stringByEncodingData:data];

}

/**
 * Deserialize Base64 NSString to NSData
 */
-(NSData*) deserialize {

    return [GTMBase64 decodeString:self.encodedData];

}
loretoparisi
  • 15,724
  • 11
  • 102
  • 146
  • Looking at the [source code](http://google-toolbox-for-mac.googlecode.com/svn/trunk/Foundation/GTMStringEncoding.m) it seems that the class providing that is now GTMStringEncoding. I have not tried it but it looks like a great new solution to this question. – sarfata Jun 10 '11 at 17:49
  • 1
    As of Mac OS X 10.6 / iOS 4.0, NSData does Base-64 encoding. `string = [data base64EncodedStringWithOptions:(NSDataBase64EncodingOptions)0]` – jrc Sep 10 '15 at 20:53
  • @jrc that is true, but consider to encode real work strings in Base-64. You have to deal than with "web safe" encoding, that you do not have in iOS/MacOS, like in GTMBase64#webSafeEncodeData. Also you may need to add/remove Base64 "padding", so you have this option as well: GTMBase64#stringByWebSafeEncodingData:(NSData *)data padded:(BOOL)padded; – loretoparisi Sep 11 '15 at 09:35
0
@implementation NSData (Extn)

- (NSString *)description
{
    NSMutableString *str = [[NSMutableString alloc] init];
    const char *bytes = self.bytes;
    for (int i = 0; i < [self length]; i++) {
        [str appendFormat:@"%02hhX ", bytes[i]];
    }
    return [str autorelease];
}

@end

Now you can call NSLog(@"hex value: %@", data)
Ramesh
  • 1,703
  • 18
  • 13
0

Change %08x to %08X to get capital characters.

newfurniturey
  • 37,556
  • 9
  • 94
  • 102
Dan Reese
  • 456
  • 4
  • 10
0

Swift + Property.

I prefer to have hex representation as property (the same as bytes and description properties):

extension NSData {

    var hexString: String {

        let buffer = UnsafeBufferPointer<UInt8>(start: UnsafePointer(self.bytes), count: self.length)
        return buffer.map { String(format: "%02x", $0) }.joinWithSeparator("")
    }

    var heXString: String {

        let buffer = UnsafeBufferPointer<UInt8>(start: UnsafePointer(self.bytes), count: self.length)
        return buffer.map { String(format: "%02X", $0) }.joinWithSeparator("")
    }
}

Idea is borrowed from this answer

Community
  • 1
  • 1
Avt
  • 16,927
  • 4
  • 52
  • 72
-4
[deviceToken description]

You'll need to remove the spaces.

Personally I base64 encode the deviceToken, but it's a matter of taste.

Matthew Verstraete
  • 6,335
  • 22
  • 67
  • 123
Eddie
  • 135
  • 5
  • 10
  • This does not get the same result. description returns : <2cf56d5d 2fab0a47 ... 7738ce77 7e791759> While I am looking for: 2CF56D5D2FAB0A47....7738CE777E791759 – sarfata Aug 20 '09 at 19:02