NSData to NSString returns Null
I have searched. But still couldnt get it.
I’m converting NSdata to NSString.
When I do
it returns me <00000000 31323334 35363738>
Yes, Im receiving my string @”12345678″.
How do I convert it to NSString appropriately?
NSString *b = [NSString stringWithUTF8String:[data bytes]]; NSString *a = [[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSUTF8StringEncoding];
Both returns me null.
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
- SEGV_ACCERR calling removeObserver:self] in dealloc
- In iOS selected cell should move to top portion in UITableView
- NSRegularExpression to extract text between two XML tags
- NSManagedObjectContext save doesn't crash but breaks on objc_exception_throw
- Storyboard and autolayout: how make a circular image
- What are the most battery-consuming things you can do in an iPhone-app?
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
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.
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.
- How do I record a maker note or user comment in EXIF?
- How to make a Button continually call a function when held down (SpriteKit)
- How to rewrite Swift ++ operator in ?: ternary operator
- Push notifications not working properly when the app is not active (killed)
- how to print value of object in an array in swift, not its location
- Install ree-1.8.7 with RVM on Mountain Lion
- What exactly does @synthesize do?
- iOS – is Motion Activity Enabled in Settings > Privacy > Motion Activity
- Instantiating Modal view controller freezes the app – Probable issue with Xcode 7
- SKAction playSoundFileNamed stops background music
- Class PLBuildVersion is implemented in both/Applications
- PHImageManager.requestImageForAsset returns nil when creating thumbnail for video
- How to support multiple devices with non-renewing In-App Purchase?
- HTTPS request in iOS 9 : NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9802)
- Make a layer over an image and adjust its co-ordinates by touch