将字节转换为字符串不起作用

时间:2016-12-16 10:23:57

标签: ios swift macos cocoa encoding

我收到带字节的数组:

var requestPtr = [3, 64, 0, 73, 0, 110, 0, 116, 0, 101, 0, 114, 0, 110, 0, 97, 0, 108, 0, 32, 0, 70, 0, 108, 0, 97, 0, 115, 0, 104, 0, 32, 0, 32, 0, 47, 0, 48, 0, 120, 0, 48, 0, 56, 0, 48, 0, 48, 0, 48, 0, 48, 0, 48, 0, 48, 0, 47, 0, 48, 0, 52, 0, 42, 0, 48, 0, 49, 0, 54, 0, 75, 0, 103, 0, 44, 0, 48, 0, 49, 0, 42, 0, 48, 0, 54, 0, 52, 0, 75, 0, 103, 0, 44, 0, 48, 0, 55, 0, 42, 0, 49, 0, 50, 0, 56, 0, 75, 0, 103, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

然后删除第一个字节并使用以下代码将数组转换为字符串:

requestPtr.remove(at: 0)

let nsdata = NSData(bytes: requestPtr, length: requestPtr.count)
var str = String(data: nsdata as Data, encoding: String.Encoding.utf8)!

如果我在控制台中打印它会返回字符串: enter image description here 但是当我尝试复制消息时,我得到了奇怪的编码。

enter image description here

str.hasPrefix("@Internal Flash")等功能无效。

var tmp1 = description.components(separatedBy: ["/"])返回:

["\u{03}@\0I\0n\0t\0e\0r\0n\0a\0l\0 \0F\0l\0a\0s\0h\0 \0 \0", "\00\0x\00\08\00\00\00\00\00\00\0", "\00\04\0*\00\01\06\0K\0g\0,\00\01\0*\00\06\04\0K\0g\0,\00\07\0*\01\02\08\0K\0g\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"]

3 个答案:

答案 0 :(得分:1)

似乎数据以UTF-16LE(小端UTF-16)编码,而不是UTF-8编码。

(代码更新以澄清,我显示的结果是什么数据。)

       var loggBoken = new List<string[]>();
        string[] post = new string[2];
        post[0] = "a";
        post[1] = "b";
        loggBoken.Add(post); //don't need to specifie ToArray()

        // use join to create total string of every item in current array
        // split by new line
        foreach (string[] item in loggBoken)
        {                
                MessageBox.Show(String.Join("\n", item));                
        }

(实际数据称为var requestPtr = [3, 64, 0, 73, 0, 110, 0, 116, 0, 101, 0, 114, 0, 110, 0, 97, 0, 108, 0, 32, 0, 70, 0, 108, 0, 97, 0, 115, 0, 104, 0, 32, 0, 32, 0, 47, 0, 48, 0, 120, 0, 48, 0, 56, 0, 48, 0, 48, 0, 48, 0, 48, 0, 48, 0, 48, 0, 47, 0, 48, 0, 52, 0, 42, 0, 48, 0, 49, 0, 54, 0, 75, 0, 103, 0, 44, 0, 48, 0, 49, 0, 42, 0, 48, 0, 54, 0, 52, 0, 75, 0, 103, 0, 44, 0, 48, 0, 55, 0, 42, 0, 49, 0, 50, 0, 56, 0, 75, 0, 103, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] requestPtr.remove(at: 0) let data = Data(bytes: requestPtr.map{UInt8($0)}) var str = String(data: data, encoding: String.Encoding.utf16LittleEndian)!.trimmingCharacters(in: CharacterSet(charactersIn: "\0")) debugPrint(str) //->"@Internal Flash /0x08000000/04*016Kg,01*064Kg,07*128Kg" ,但[UInt8]正在制作。)

答案 1 :(得分:1)

需要定义类型[UInt8]的字节数组。下面提到的将工作并返回True

var requestPtr:[UInt8] = [3, 73, 110, 116, 101, 114, 110, 97, 108, 32, 70, 108, 97, 115, 104] requestPtr.remove(at: 0) print(requestPtr.count) print(requestPtr) let nsdata = NSData(bytes: requestPtr as [UInt8], length: requestPtr.count) var str = String(data: nsdata as Data, encoding: String.Encoding.utf8)! str.hasPrefix("Internal Flash")

答案 2 :(得分:0)

您接收带字节的数组,但只有每隔一个字节正确

试试这个:

let test = String(requestPtr.enumerated().filter{ index, element in
    return index % 2 == 1
}.map { index, element in
    return Character(UnicodeScalar(element)! )
})

他们可能是更好的解决方案,但没有想到