2010-03-18 06:17

将 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?


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • csdnceshi72 谁还没个明天 11年前

    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:))
    点赞 20 评论 复制链接分享
  • weixin_41568131 10.24 6年前

    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.

    点赞 4 评论 复制链接分享
  • csdnceshi50 三生石@ 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:


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


    let signatureString = signature.base64EncodedStringWithOptions(nil)
    点赞 4 评论 复制链接分享
  • csdnceshi79 python小菜 8年前

    I humbly submit a category to make this less annoying:

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


    @implementation NSData (EasyUTF8)
    - (NSString *)asUTF8String {
        return [[NSString alloc] initWithData:self encoding:NSUTF8StringEncoding];    

    (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];
    点赞 3 评论 复制链接分享
  • csdnceshi74 7*4 11年前

    You could call this method

    +(id)stringWithUTF8String:(const char *)bytes.
    点赞 2 评论 复制链接分享
  • weixin_41568196 撒拉嘿哟木头 6年前

    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)


    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"
    点赞 1 评论 复制链接分享