[英]Swift convert HexString to Integer
I have a data source in String example 我在String示例中有一个数据源
HexString = "72AE"
and i would like to convert it into byte and store into byte array 我想将其转换为字节并存储到字节数组中
bytearray = [72, AE] //UInt8
i know i can do this by 我知道我可以做到这一点
let hexaString = "72AE"
let resultArray = hexaString.characters.map{Int(strtoul(( String($0)), nil, 16))}
print(resultArray) // "[7, 2, 10, 14]"
but it is not returning the value i want. 但它没有返回我想要的价值。 I have also tried to chop it into hexaString1 = "72" hexaString2 = "AE" but still i can't manage to get the correct value.
我也尝试将其切成hexaString1 =“72”hexaString2 =“AE”,但仍然无法获得正确的值。
Hope this will help you 希望这个能对您有所帮助
let hexaString = "72AE"
var byteArray = [UInt8]()
byteArray += hexaString.utf8 // Convert into byte array
// Retain the orginal string from byte array
let stringFromByteArray = NSString(bytes: byteArray, length: byteArray.count, encoding: NSUTF8StringEncoding)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.