【问题标题】:Convert a String of hex to an Int [duplicate]将十六进制字符串转换为 Int [重复]
【发布时间】:2017-05-29 09:40:02
【问题描述】:

我在 Swift 中工作,我有这个字符串作为输入:"0x13123ac"
我想要一个Int 作为输出:0x13123ac

我该怎么做?

【问题讨论】:

标签: swift numbers int hex


【解决方案1】:

你的问题毫无意义。计算机以二进制而不是十六进制存储数字。也许您想要的是将十六进制字符串转换为整数并将其转换回十六进制以进行显示:

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))")
}

【讨论】:

    猜你喜欢
    • 2023-03-08
    • 2014-04-05
    • 1970-01-01
    • 1970-01-01
    • 2017-02-18
    • 2018-07-03
    • 2020-12-09
    • 2020-07-18
    相关资源
    最近更新 更多