Swift中NSData转String的问题

5
我在Swift中将NSData转换成NSString时遇到了问题。我使用的是我认为正确的命令和格式:NSString(data: , encoding: ),但无论我做什么都会得到一个空值。我正在运行最新的Xcode beta,所以我不确定是否相关,但我希望这只是一个简单易错的错误。
我附上了playground代码和屏幕截图。

Xcode 6.3 Beta 2 Build(6D532l)Playground Code

import Foundation

//: # NSData to String Conversion Playground
//: ### Step 1
//: The first step is to take an array of bytes and conver them into a NSData object.  The bytes are as follows:

var testBytes : [UInt8] = [0x14, 0x00, 0xAB, 0x45, 0x49, 0x1F, 0xEF, 0x15, 0xA8, 0x89, 0x78, 0x0F, 0x09, 0xA9, 0x07, 0xB0, 0x01, 0x20, 0x01, 0x4E, 0x38, 0x32, 0x35, 0x56, 0x20, 0x20, 0x20, 0x00]

//: ### Step 2
//: Convert the byte array into an **NSData** Object

var immutableData = NSData(bytes: testBytes, length: testBytes.count)

//: ### Step 3
//: Attempt to convert the **NSData** object into a string so it can be sent around as ascii.  This for some reason seems to be failing, however.

var convertedString = NSString(data: immutableData, encoding: NSUTF8StringEncoding)

println("String = \(convertedString)")

Playground的结果

这里是图片描述

(注:Playground指的是一种编程学习工具)

官方的Swift也显示了相同的结果。你从哪里获取这些十六进制代码的? - qwerty_so
1
你的字符串数据看起来像垃圾。因此,解码过程将拒绝接受它。如果输入一些合法的ASCII码,它就可以正常工作了。例如尝试var testBytes: [UInt8] = [0x68, 0x65, 0x6c, 0x6c, 0x6f]. - qwerty_so
1
我只想要一个十六进制值的字符串。我猜这是试图解码为实际的ASCII码!傻啊!! - Jeef
使用格式化打印来获取十六进制表示。 - qwerty_so
是的,里面有一些合法的字符,但大多数都是垃圾。任何大于0x7F或小于0x20的字符都是垃圾。 - Hot Licks
4个回答

8
let testBytes : [UInt8] = [0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64]


func bytes2String(array:[UInt8]) -> String {
    return String(data: NSData(bytes: array, length: array.count), encoding: NSUTF8StringEncoding) ?? ""
}

Xcode 8.2 • Swift 3.0.2

func bytes2String(_ array: [UInt8]) -> String {
    return String(data: Data(bytes: array, count: array.count), encoding: .utf8) ?? ""
}

测试:

bytes2String(testBytes)  // "Hello World"

6

请使用有效的UTF8字符!

// Playground - noun: a place where people can play
import UIKit
var str = "Hello, playground"
import Foundation
//: # NSData to String Conversion Playground
//: ### Step 1
//: The first step is to take an array of bytes and conver them into a NSData object.  The bytes are as follows:

// Hello World
var testBytes : [UInt8] = [0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64]

//: ### Step 2
//: Convert the byte array into an **NSData** Object

var immutableData = NSData(bytes: testBytes, length: testBytes.count)

//: ### Step 3
//: Attempt to convert the **NSData** object into a string so it can be sent around as ascii.  This for some reason seems to be failing, however.

var convertedString = NSString(data: immutableData, encoding: NSUTF8StringEncoding)

println("String = \(convertedString)")

您的输出将是:"String = 可选(你好世界)"

3
我会使用String(data: immutableData, encoding: NSUTF8StringEncoding)来转换不可变数据为字符串。 - Julian

4

如果你只想要字符串中的十六进制值:

我只想要一个包含十六进制值的字符串。我猜这是尝试解码为实际的ASCII!傻!! - Jeef Mar 4 at 23:29

最简单的方法就是使用Swift内置的字符串插值。

 let myHexString = "\(myNSDataObject)"

这将为您提供一个十六进制字符串,每两个字符之间有空格,并用方括号括起来。就像这样:
 <a0 ff 21 4a>

您可以使用内置字符串方法进行格式化:
 myHexString.stringByTrimmingCharactersInSet(NSCharacterSet(charactersInString: "<>")).stringByReplacingOccurrencesOfString(" ", withString: "")

您将会得到一个包含字符串:a0ff214a。

4

在Swift 3.0中使用UTF8

 let testBytes : [UInt8] = [0x14, 0x00, 0xAB, 0x45, 0x49, 0x1F, 0xEF, 0x15, 0xA8, 0x89, 0x78, 0x0F, 0x09, 0xA9, 0x07, 0xB0, 0x01, 0x20, 0x01, 0x4E, 0x38, 0x32, 0x35, 0x56, 0x20, 0x20, 0x20, 0x00]

 let immutableData = NSData(bytes: testBytes, length: testBytes.count)

 let convertedString = NSString(data: (immutableData as NSData) as Data, encoding:String.Encoding.utf8.rawValue)

 print("String = \(convertedString)")

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接