且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

十六进制字符串到文本的转换 - swift 3

更新时间:2023-02-03 08:33:24

你可能可以这样使用:

func hexToStr(text: String) -> String {

    let regex = try! NSRegularExpression(pattern: "(0x)?([0-9A-Fa-f]{2})", options: .caseInsensitive)
    let textNS = text as NSString
    let matchesArray = regex.matches(in: textNS as String, options: [], range: NSMakeRange(0, textNS.length))
    let characters = matchesArray.map {
        Character(UnicodeScalar(UInt32(textNS.substring(with: $0.rangeAt(2)), radix: 16)!)!)
    }

    return String(characters)
}