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
Swift 将十六进制字符串转换为整数_Swift_Numbers_Int_Hex - Fatal编程技术网

Swift 将十六进制字符串转换为整数

Swift 将十六进制字符串转换为整数,swift,numbers,int,hex,Swift,Numbers,Int,Hex,我在Swift中工作,输入以下字符串:“0x13123ac” 我想要一个Int作为输出:0x13123ac 我该怎么做?你的问题毫无意义。计算机以二进制而不是十六进制存储数字。您可能希望将十六进制字符串转换为整数,并将其转换回十六进制以用于显示: let input = "0x13123ac" // Int(_, radix: ) can't deal with the '0x' prefix. NSScanner can handle hex // with or without the '

我在Swift中工作,输入以下字符串:
“0x13123ac”

我想要一个
Int
作为输出:
0x13123ac


我该怎么做?你的问题毫无意义。计算机以二进制而不是十六进制存储数字。您可能希望将十六进制字符串转换为整数,并将其转换回十六进制以用于显示:

let input = "0x13123ac"

// Int(_, radix: ) can't deal with the '0x' prefix. NSScanner can handle hex
// with or without the '0x' prefix
let scanner = Scanner(string: input)
var value: UInt64 = 0

if scanner.scanHexInt64(&value) {
    print("Decimal: \(value)")
    print("Hex: 0x\(String(value, radix: 16))")
}