栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在Swift中将十六进制字符串转换为NSData

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在Swift中将十六进制字符串转换为NSData

这是我的

Data
例行十六进制字符串:

extension String {    /// Create `Data` from hexadecimal string representation    ///    /// This creates a `Data` object from hex string. Note, if the string has any spaces or non-hex characters (e.g. starts with '<' and with a '>'), those are ignored and only hex characters are processed.    ///    /// - returns: Data represented by this hexadecimal string.    var hexadecimal: Data? {        var data = Data(capacity: characters.count / 2)        let regex = try! NSRegularexpression(pattern: "[0-9a-f]{1,2}", options: .caseInsensitive)        regex.enumerateMatches(in: self, range: NSRange(startIndex..., in: self)) { match, _, _ in let byteString = (self as NSString).substring(with: match!.range) let num = UInt8(byteString, radix: 16)! data.append(num)        }        guard data.count > 0 else { return nil }        return data    }}

为了完整起见,这是我

Data
的十六进制字符串例程:

extension Data {    /// Hexadecimal string representation of `Data` object.    var hexadecimal: String {        return map { String(format: "%02x", $0) } .joined()    }}

请注意,如上所示,我通常只在十六进制表示形式和

NSData
实例之间进行转换(因为如果可以将信息表示为字符串,则可能一开始就不会创建十六进制表示形式)。但是您最初的问题是要在十六进制表示形式和
String
对象之间进行转换,因此可能看起来像这样:

extension String {    /// Create `String` representation of `Data` created from hexadecimal string representation    ///    /// This takes a hexadecimal representation and creates a String object from that. Note, if the string has any spaces, those are removed. Also if the string started with a `<` or ended with a `>`, those are removed, too.    ///    /// For example,    ///    ///     String(hexadecimal: "<666f6f>")    ///    /// is    ///    ///     Optional("foo")    ///    /// - returns: `String` represented by this hexadecimal string.    init?(hexadecimal string: String, encoding: String.Encoding = .utf8) {        guard let data = string.hexadecimal() else { return nil        }        self.init(data: data, encoding: encoding)    }    /// Create hexadecimal string representation of `String` object.    ///    /// For example,    ///    ///     "foo".hexadecimalString()    ///    /// is    ///    ///     Optional("666f6f")    ///    /// - parameter encoding: The `String.Encoding` that indicates how the string should be converted to `Data` before performing the hexadecimal conversion.    ///    /// - returns: `String` representation of this String object.    func hexadecimalString(encoding: String.Encoding = .utf8) -> String? {        return data(using: encoding)? .hexadecimal    }}

然后可以像上面这样使用上面的代码:

let hexString = "68656c6c 6f2c2077 6f726c64"print(String(hexadecimal: hexString))

要么,

let originalString = "hello, world"print(originalString.hexadecimalString())

有关上述对早期Swift版本的排列,请参阅此问题的修订历史记录。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/373807.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号