简体   繁体   English

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

[英]Convert UTF-8 encoded NSData to NSString

I have UTF-8 encoded NSData from windows server and I want to convert it to NSString for iPhone. 我从Windows服务器有UTF-8编码的NSData ,我想将它转换为iPhone的NSString Since data contains characters (like a degree symbol) which have different values on both platforms, how do I convert data to string? 由于数据包含在两个平台上具有不同值的字符(如度数符号), 如何将数据转换为字符串?

If the data is not null-terminated, you should use -initWithData:encoding: 如果数据不是以null结尾,则应使用-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. 如果数据以空值终止,则应使用-stringWithUTF8String:以避免结尾处的额外\\0

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

(Note that if the input is not properly UTF-8-encoded, you will get nil .) (注意,如果输入没有正确的UTF-8编码,你将得到nil 。)


Swift variant: Swift变种:

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. 如果数据以空值终止,您可以通过删除该空字符的安全方式或类似于上述Objective-C版本的不安全方式。

// 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:))

你可以调用这个方法

+(id)stringWithUTF8String:(const char *)bytes.

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.) (请注意,如果您不使用ARC,则需要在那里进行autorelease 。)

Now instead of the appallingly verbose: 而不是令人震惊的冗长:

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

You can do: 你可以做:

NSData *data = ...
[data asUTF8String];

The Swift version from String to Data and back to String: Swift版本从String到Data再返回String:

Xcode 10.1 • Swift 4.2.1 Xcode 10.1•Swift 4.2.1

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

extension StringProtocol {
    var data: Data {
        return Data(utf8)
    }
}

extension String {
    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 ?? "nil")                    // "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"

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. 在我的情况下,我使用我的RSA私钥生成签名,结果是NSData。 I found that this seems to work: 我发现这似乎有效:

Objective-C Objective-C的

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

Swift 迅速

let signatureString = signature.base64EncodedStringWithOptions(nil)

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. 我得到的字符串是不可预测的:大约70%它确实包含了预期的值,但是它常常导致Null甚至更糟:在字符串的末尾打扮。

After some digging I switched to 经过一番挖掘后,我转而去了

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

And got the expected result every time. 每次都得到预期的结果。

With Swift 5, you can use String 's init(data:encoding:) initializer in order to convert a Data instance into a String instance using UTF-8. 使用Swift 5,您可以使用Stringinit(data:encoding:)初始化程序,以便使用UTF-8将Data实例转换为String实例。 init(data:encoding:) has the following declaration: init(data:encoding:)具有以下声明:

init?(data: Data, encoding: String.Encoding)

Returns a String initialized by converting given data into Unicode characters using a given encoding. 返回通过使用给定编码将给定数据转换为Unicode字符而初始化的String

The following Playground code shows how to use it: 以下Playground代码显示了如何使用它:

import Foundation

let json = """
{
"firstName" : "John",
"lastName" : "Doe"
}
"""

let data = json.data(using: String.Encoding.utf8)!

let optionalString = String(data: data, encoding: String.Encoding.utf8)
print(String(describing: optionalString))

/*
 prints:
 Optional("{\n\"firstName\" : \"John\",\n\"lastName\" : \"Doe\"\n}")
*/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM