NSData to NSString returns Null

I have searched. But still couldnt get it.

I’m converting NSdata to NSString.
When I do [data description];
it returns me <00000000 31323334 35363738>
Yes, Im receiving my string @”12345678″.

  • How can I format a date in Objective-C similar to the jquery.timeago library?
  • Reading long characteristic values using CoreBluetooth
  • Converting a gregorian date string to Islamic date gives correct & incorrect results
  • When to access property with self and when not to?
  • Auto Layout keeps stretching UIImageView
  • Learning iPhone SDK (Objective-C)
  • How do I convert it to NSString appropriately?
    I tried

    NSString *b = [NSString stringWithUTF8String:[data bytes]];
    NSString *a = [[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSUTF8StringEncoding];
    

    Both returns me null.
    Any idea?
    Thanks

    Hi all,
    Thanks for all suggestion.
    It appears to be constant whereby theres a null character infront always.
    So whenever I receive something, i just remove the first <00000000>, then its working fine already

    3 Solutions Collect From Internet About “NSData to NSString returns Null”

    This happens if the encoding is incorrect.

    Try using ASCII to test out. ASCII almost certainly work to retrive somekind of string. If it’s only numbers it will probably work.

    NSString *a = [[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSASCIIStringEncoding];
    

    Most common except UTF-8 enconding is:

    NSASCIIStringEncoding
    NSUnicodeStringEncoding
    NSISOLatin1StringEncoding
    NSISOLatin2StringEncoding
    NSSymbolStringEncoding
    

    try them out and see if they work.

    I’m converting NSdata to NSString. When I do [data description]; it
    returns me <00000000 31323334 35363738> Yes, Im receiving my string
    @”12345678″.

    No — you aren’t receiving that string. You are receiving a byte sequence that starts with a bunch of 0x00 values and is followed by a series of bytes that happen to correspond to the ASCII sequence “12345678”.

    I.e. you have raw data and are trying to convert it to a constrained type, but can’t because the constrained type cannot represent the raw data.

    You could try using the “lossy conversion” APIs on NSString, but that might not work and would be fragile anyway.

    Best bet?

    Only convert the bytes in the NSData that actually represent the string to an instance of NSString. That can be done with -initWithBytes:length:encoding:; you’ll need to do the calculations to find the correct offset and length.

    This may be because the first bytes of your data is 00. The character 0 is the end of string character. When creating a string from ASCII (from an array of chars or an array of bytes as you are doing), when the character 0 is encountered at the beginning, it produces an empty string.

    I would however expect it to return an instance of NSString with 0 characters, and not null.