Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在Swift3中将ASCII转换为十六进制?_Ios_Swift - Fatal编程技术网

Ios 如何在Swift3中将ASCII转换为十六进制?

Ios 如何在Swift3中将ASCII转换为十六进制?,ios,swift,Ios,Swift,我使用下面的代码将字符串“UK”转换为十六进制。如何将其转换为十六进制 let str = auxText let bytes = str.utf8 var buffer = [UInt8](bytes) buffer[0] = buffer[0] + UInt8(1) print("ascii value is",buffer) 在Swift 4中,您可以使用 string.data(using: .ascii) 要将数据转换为十六进制或十进制,可以

我使用下面的代码将字符串“UK”转换为十六进制。如何将其转换为十六进制

    let str = auxText
    let bytes = str.utf8
    var buffer = [UInt8](bytes)
    buffer[0] = buffer[0] + UInt8(1)
    print("ascii value is",buffer)

在Swift 4中,您可以使用

string.data(using: .ascii)
要将数据转换为十六进制或十进制,可以使用以下扩展名:

extension Data {

    var hexString: String {
        return map { String(format: "%02hhx", $0) }.joined()
    }

    var decimalString: String {
        return map { String(format: "%d", $0) }.joined()
    }

}
您的测试字符串“UK”

评估

let hexString = string.data(using: .ascii)!.hexString

let decimalString = string.data(using: .ascii)!.decimalString


这两个结果都是正确的,您可以在任何ASCII表格中查找它们。

可以在和研究中找到可能重复的类似问题,请查看并尝试。然后你可以避免被标记为重复。不工作。我的ascii值是[85,75],我没有得到所需的值。你期望什么十六进制形式?
554b
let decimalString = string.data(using: .ascii)!.decimalString
8575