Convert Utf-8 Encoded Nsdata to Nsstring

Convert UTF-8 encoded NSData to NSString

If the data is not null-terminated, you should use -initWithData:encoding:

NSString* newStr = [[NSString alloc] initWithData:theData encoding:NSUTF8StringEncoding];

If the data is null-terminated, you should instead use -stringWithUTF8String: to avoid the extra \0 at the end.

NSString* newStr = [NSString stringWithUTF8String:[theData bytes]];

(Note that if the input is not properly UTF-8-encoded, you will get nil.)



Swift variant:

let newStr = String(data: data, encoding: .utf8)
// note that `newStr` is a `String?`, not a `String`.

If the data is null-terminated, you could go though the safe way which is remove the that null character, or the unsafe way similar to the Objective-C version above.

// safe way, provided data is \0-terminated
let newStr1 = String(data: data.subdata(in: 0 ..< data.count - 1), encoding: .utf8)
// unsafe way, provided data is \0-terminated
let newStr2 = data.withUnsafeBytes(String.init(utf8String:))

How to convert NSData to NSString?

NSString provides an initializer for this purpose.

// NSData *data = [NSData data];
NSString *string = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];

Can't convert NSData to NSString when using UIImageJPEGRepresentation()?

Your mistake is thinking that the JPEG representation of an image is a UTF-8-encoded string. It's not. It's arbitrary binary data. UTF-8 strings aren't arbitrary sequences of bytes; there are rules about what bytes can follow other bytes.

You want to encode your JPEG data as a string suitable for use in an HTTP form submission. One way is to use base64 encoding, like this:

    [body appendString:@"Content-Type: image/*\r\n"];
[body appendString:@"Content-Transfer-Encoding: base64\r\n\r\n"];
[body appendString:[data base64EncodedStringWithOptions: NSDataBase64Encoding76CharacterLineLength]];

On the other hand, you could use the binary encoding and include the raw JPEG data directly. In that case, you would need to change the type of body to NSMutableData and build it up with different methods, as you cannot append arbitrary binary data to an NSMutableString.

UPDATE

In Swift, with an NSMutableString:

let data = UIImageJPEGRepresentation(image, 0.5)!
let body = NSMutableString()
body.append("Content-Type: image/*\r\n")
body.append("Content-Transfer-Encoding: base64\r\n\r\n")
body.append(data.base64EncodedString(options: .lineLength76Characters))

In Swift, with a String:

let data = UIImageJPEGRepresentation(image, 0.5)!
var body = ""
body += "Content-Type: image/*\r\n"
body += "Content-Transfer-Encoding: base64\r\n\r\n"
body += data.base64EncodedString(options: .lineLength76Characters)

How to Convert NSData to NSString

I am posting this for records sake because I found a duplicate and voting to close this down.

Actually what I am receiving is a stream of bytes represented as hex, and all the answers indicated do not work. Only [NSData description] gave me true data, which is something I can't use because it is intended for debugging.

Finally I tried the solution given here, and I get what I want.
Thanks to all for trying to help out.

Converting NSData that contains UTF-8 and null bytes to string

The documentation for stringWithUTF8String describes its first parameter as:

A NULL-terminated C array of bytes in UTF8 encoding.

Which is why your conversion stops at the first null byte.

What you appear to have is a collection of C strings packed into a single NSData. You can convert each one individually. Use the NSData methods bytes and length to obtain a pointer to the bytes/first C string and the total number of bytes respectively. The standard C function strlen() will give you the length in bytes of an individual string. Combine these and some simple pointer arithmetic and you can write a loop which converts each string and, for example, stores them all into an array or concatenates them.

If you get stuck implementing the solution ask a new question, show your code, and explain the issue. Someone will undoubtedly help you with the next step.

HTH



Related Topics



Leave a reply



Submit