Convert NSData byte array to string? Convert NSData byte array to string? json json

Convert NSData byte array to string?


  1. The reason the String is being considered 'unreliable' in previous Stack posts is because they too were attempting to use NSData objects where the ending bytes aren't properly terminated with NULL :

    NSString *jsonString = [NSString stringWithUTF8String:[nsDataObj bytes]];// This is unreliable because it may result in NULL string values
  2. Whereas the example below should give you your desired results because the NSData byte string will terminate correctly:

    NSString *jsonString = [[NSString alloc]  initWithBytes:[nsDataObj bytes] length:[nsDataObj length] encoding: NSUTF8StringEncoding];

You were on the right track and hopefully this is able to help you solve your current problem. Best of luck!

~ EDIT ~

Make sure you are declaring your NSData Object from an image like so:

NSData *imageData = [[NSData alloc] init];imageData = UIImagePNGRepresentation(yourImage);


Have you tried using something like this:

@implementation NSData (Base64)- (NSString *)base64EncodedString{    return [self base64EncodedStringWithWrapWidth:0];}

This will turn your NSData in a base64 string, and on the other side you just need to decode it.

EDIT: @Lucas said you can do something like this:

NSString *myString = [[NSString alloc] initWithData:myData encoding:NSUTF8StringEncoding];

but i had some problem with this method because of some special characters, and because of that i started using base64 strings for communication.

EDIT3: Trys this method base64EncodedString

    @implementation NSData (Base64)    - (NSString *)base64EncodedString    {        return [self base64EncodedStringWithWrapWidth:0];    }    //Helper Method    - (NSString *)base64EncodedStringWithWrapWidth:(NSUInteger)wrapWidth    {        //ensure wrapWidth is a multiple of 4        wrapWidth = (wrapWidth / 4) * 4;        const char lookup[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";        long long inputLength = [self length];        const unsigned char *inputBytes = [self bytes];        long long maxOutputLength = (inputLength / 3 + 1) * 4;        maxOutputLength += wrapWidth? (maxOutputLength / wrapWidth) * 2: 0;        unsigned char *outputBytes = (unsigned char *)malloc((NSUInteger)maxOutputLength);        long long i;        long long outputLength = 0;        for (i = 0; i < inputLength - 2; i += 3)        {            outputBytes[outputLength++] = lookup[(inputBytes[i] & 0xFC) >> 2];            outputBytes[outputLength++] = lookup[((inputBytes[i] & 0x03) << 4) | ((inputBytes[i + 1] & 0xF0) >> 4)];            outputBytes[outputLength++] = lookup[((inputBytes[i + 1] & 0x0F) << 2) | ((inputBytes[i + 2] & 0xC0) >> 6)];            outputBytes[outputLength++] = lookup[inputBytes[i + 2] & 0x3F];            //add line break            if (wrapWidth && (outputLength + 2) % (wrapWidth + 2) == 0)            {                outputBytes[outputLength++] = '\r';                outputBytes[outputLength++] = '\n';            }        }        //handle left-over data        if (i == inputLength - 2)        {            // = terminator            outputBytes[outputLength++] = lookup[(inputBytes[i] & 0xFC) >> 2];            outputBytes[outputLength++] = lookup[((inputBytes[i] & 0x03) << 4) | ((inputBytes[i + 1] & 0xF0) >> 4)];            outputBytes[outputLength++] = lookup[(inputBytes[i + 1] & 0x0F) << 2];            outputBytes[outputLength++] =   '=';        }        else if (i == inputLength - 1)        {            // == terminator            outputBytes[outputLength++] = lookup[(inputBytes[i] & 0xFC) >> 2];            outputBytes[outputLength++] = lookup[(inputBytes[i] & 0x03) << 4];            outputBytes[outputLength++] = '=';            outputBytes[outputLength++] = '=';        }        if (outputLength >= 4)        {            //truncate data to match actual output length            outputBytes = realloc(outputBytes, (NSUInteger)outputLength);            return [[NSString alloc] initWithBytesNoCopy:outputBytes                                                  length:(NSUInteger)outputLength                                                encoding:NSASCIIStringEncoding                                            freeWhenDone:YES];        }        else if (outputBytes)        {            free(outputBytes);        }        return nil;    }


Null termination is not the only problem when converting from NSData to NSString.

NSString is not designed to hold arbitrary binary data. It expects an encoding.

If your NSData contains an invalid UTF-8 sequence, initializing the NSString will fail.

The documentation isn't completely clear on this point, but for initWithData it says:

Returns nil if the initialization fails for some reason (for example if data does not represent valid data for encoding).

Also: The JSON specification defines a string as a sequence of Unicode characters.

That means even if you're able to get your raw data into a JSON string, parsing could fail on the receiving end if the code performs UTF-8 validation.

If you don't want to use Base64, take a look at the answers here.