快速将字节数组转换为字符串 [英] Swift converting Byte Array into String
本文介绍了快速将字节数组转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我无法将此字节数组下面的内容迅速转换为String.
I can't convert this below byte array into String in swift.
let chars: [UInt8] = [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0]
let datastring = NSString(data: chars, encoding: NSUTF8StringEncoding)
但是在android中,它可以正常工作,我不知道swift到底出了什么问题.
But in android it just works fine I don't know whats wrong in swift.
推荐答案
[UInt8]
不是 NSData
,因此您不能使用 NSString(数据...
初始化程序
[UInt8]
is not NSData
, so you can't use the NSString(data...
initializer
您可以使用
let chars: [UInt8] = [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0]
let count = chars.count / sizeof(UInt8)
let datastring = NSString(bytes: chars, length: count, encoding: NSASCIIStringEncoding)
这篇关于快速将字节数组转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文