将 UTF-8编码的 NSData 转换为 NSString

I have UTF-8 encoded NSData from windows server and I want to convert it to NSString for iPhone. Since data contains characters (like a degree symbol) which have different values on both platforms, how do I convert data to string?

转载于:https://stackoverflow.com/questions/2467844/convert-utf-8-encoded-nsdata-to-nsstring

csdnceshi59
ℙℕℤℝ UTF-8 is UTF-8 everywhere. Once it's UTF-8, there's no different values for different platforms. That's the whole point of it.
6 年多之前 回复

6个回答

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:))
csdnceshi70
笑故挽风 No doubt you're right, but sometimes one has to row with the oars that one has. (Apologies for translating a Dutch saying)
2 年多之前 回复
csdnceshi66
必承其重 | 欲带皇冠 Thanks for the link. I'd argue that if the transmitted data could be randomly null-terminated or not the protocol is ill-defined.
2 年多之前 回复
csdnceshi70
笑故挽风 How do you know whether your NSData is null-terminated or not? See Tom Harrington's answer at: stackoverflow.com/questions/27935054/…. In my experience, one should not ever assume NSData is either null-terminated or not: it can differ from one transmission to the next, even from a known server.
2 年多之前 回复
csdnceshi66
必承其重 | 欲带皇冠 Please check update.
接近 4 年之前 回复
csdnceshi63
elliott.david Dumb question probably (just learning Swift, don't know Objective-C at all): You show two variants for Objective-C depending on whether the input is null-terminated or not, but only one variant for Swift?
接近 4 年之前 回复
csdnceshi69
YaoRaoLov I meet a crash: NSString jsonStr = [NSString stringWithUTF8String:[jsonData bytes]]; And NSString newStr = [[NSString alloc] initWithData:theData encoding:NSUTF8StringEncoding];
接近 4 年之前 回复
csdnceshi79
python小菜 You can also drop the NS if using swift 2 making it: let newStr = String(data: data, encoding: NSUTF8StringEncoding)
4 年多之前 回复
csdnceshi53
Lotus@ Both of the solutions returning nil for me.
大约 5 年之前 回复
csdnceshi75
衫裤跑路 This is retuning nullfor me :(
大约 6 年之前 回复
csdnceshi59
ℙℕℤℝ you saved me....
6 年多之前 回复
csdnceshi71
Memor.の MIND THIS: when using "stringWithUTF8String:" on a string that is not null-terminated, the result is unpredictable!
大约 8 年之前 回复
csdnceshi60
℡Wang Yan watch out!! if using stringWithUTF8String, don't pass it a NULL argument or it will throw an exception
8 年多之前 回复

The Swift version from String to Data and back to String:

Xcode 9 • Swift 4

extension Data {
    var string: String {
        return String(data: self, encoding: .utf8) ?? ""
    }
}
extension String {
    var data: Data {
        return Data(utf8)
    }
    var base64Decoded: Data? {
        return Data(base64Encoded: self)
    }
}

Playground

let string = "Hello World"                                  // "Hello World"
let stringData = string.data                                // 11 bytes
let base64EncodedString = stringData.base64EncodedString()  // "SGVsbG8gV29ybGQ="
let stringFromData = stringData.string                      // "Hello World"

let base64String = "SGVsbG8gV29ybGQ="
if let data = base64String.base64Decoded {
    print(data)                                    //  11 bytes
    print(data.base64EncodedString())              // "SGVsbG8gV29ybGQ="
    print(data.string)                             // "Hello World"
}

let stringWithAccent = "Olá Mundo"                          // "Olá Mundo"
print(stringWithAccent.count)                               // "9"
let stringWithAccentData = stringWithAccent.data            // "10 bytes" note: an extra byte for the acute accent
let stringWithAccentFromData = stringWithAccentData.string  // "Olá Mundo\n"

Just to summarize, here's a complete answer, that worked for me.

My problem was that when I used

[NSString stringWithUTF8String:(char *)data.bytes];

The string I got was unpredictable: Around 70% it did contain the expected value, but too often it resulted with Null or even worse: garbaged at the end of the string.

After some digging I switched to

[[NSString alloc] initWithBytes:(char *)data.bytes length:data.length encoding:NSUTF8StringEncoding];

And got the expected result every time.

csdnceshi72
谁还没个明天 Its important that you understand <i>why</i> you got 'garbage' results.
大约 4 年之前 回复

You could call this method

+(id)stringWithUTF8String:(const char *)bytes.
csdnceshi51
旧行李 stringWithUTF8Data, hence most of us create a NSString+Foo category and create the method.
大约 3 年之前 回复
csdnceshi67
bug^君 Of course. I wasn't clear. I meant, given that it's possible to go from a non-null-terminated NSData to an NSString (see KennyTM's answer), I'm surprised there isn't a +(id)stringWithUTF8Data:(NSData *)data which just works.
接近 7 年之前 回复
csdnceshi63
elliott.david you're not passing in an NSData object, you're passing it a (const char *) obtained with [data bytes], which is just a pointer, no size information. Hence the data block it points to must be null terminated. Check out the documentation, it says so explicitly.
接近 7 年之前 回复
csdnceshi67
bug^君 i don't know why on earth this would break on non-null-terminated strings seeing how the NSData knows how many bytes it has...
大约 7 年之前 回复
csdnceshi64
游.程 yes, this is absolutely NOT valid.
7 年多之前 回复
csdnceshi78
程序go Only if the data is null-terminated. Which it may not be (and, in fact, probably is not).
7 年多之前 回复

Sometimes, the methods in the other answers don't work. In my case, I'm generating a signature with my RSA private key and the result is NSData. I found that this seems to work:

Objective-C

NSData *signature;
NSString *signatureString = [signature base64EncodedStringWithOptions:0];

Swift

let signatureString = signature.base64EncodedStringWithOptions(nil)
csdnceshi79
python小菜 Objective-C: [[NSData alloc] initWithBase64EncodedString:signatureString options:0]; Swift: NSData(base64EncodedString: str options: nil)
5 年多之前 回复
csdnceshi77
狐狸.fox how to get that string to nsdata ?
5 年多之前 回复

I humbly submit a category to make this less annoying:

@interface NSData (EasyUTF8)

// Safely decode the bytes into a UTF8 string
- (NSString *)asUTF8String;

@end

and

@implementation NSData (EasyUTF8)

- (NSString *)asUTF8String {
    return [[NSString alloc] initWithData:self encoding:NSUTF8StringEncoding];    
}

@end

(Note that if you're not using ARC you'll need an autorelease there.)

Now instead of the appallingly verbose:

NSData *data = ...
[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

You can do:

NSData *data = ...
[data asUTF8String];
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐