title: Best way to serialize an NSData into a hexadeximal string
body: <p>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.</p>
  
  <p>I have the following implementation, but I am thinking there must be some shorter and nicer way to do it.</p>
  
  <pre><code>+ (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 &lt; length; i++)
      {
          [str appendFormat:@"%02.2hhX", bytes[i]];
      }
      free(bytes);
  
      return str;
  }
  </code></pre>