gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 01:26:51 24 4
gpt4 key购买 nike

我收到带字节的数组:

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"]

最佳答案

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

var requestPtr:[UInt8] = [3, 73, 110, 116, 101, 114, 110, 97, 108, 32, 70, 108, 97, 115, 104]
requestPtr.remove(在:0)
打印(requestPtr.count)
打印(requestPtr)
让 nsdata = NSData(字节:requestPtr 作为 [UInt8],长度:requestPtr.count)
var str = String(data: nsdata as Data, encoding: String.Encoding.utf8)!
str.hasPrefix("内部闪存")

关于ios - 将字节转换为字符串不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41182197/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com