Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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.</p> <p><strong>NSData+Conversion.h</strong></p> <pre><code>#import &lt;Foundation/Foundation.h&gt; @interface NSData (NSData_Conversion) #pragma mark - String Conversion - (NSString *)hexadecimalString; @end </code></pre> <p><strong>NSData+Conversion.m</strong></p> <pre><code>#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 &lt; dataLength; ++i) [hexString appendString:[NSString stringWithFormat:@"%02lx", (unsigned long)dataBuffer[i]]]; return [NSString stringWithString:hexString]; } @end </code></pre> <p>Usage:</p> <pre><code>NSData *someData = ...; NSString *someDataHexadecimalString = [someData hexadecimalString]; </code></pre> <p>This is "probably" better than calling <code>[someData description]</code> and then stripping the spaces, &lt;'s, and >'s. Stripping characters just feels too "hacky". Plus you never know if Apple will change the formatting of NSData's <code>-description</code> in the future.</p> <p><strong>NOTE:</strong> 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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload