所以在Swift 3中,我有这个函数将表示Hexa数据的String转换为Data type:
extension String {
public func fromHexStringtoData() -> Data? {
func convertToUInt8(u: UInt16) -> UInt8? {
switch(u) {
case 0x30 ... 0x39:
return UInt8(u - 0x30)
case 0x41 ... 0x46:
return UInt8(u - 0x41 + 10)
case 0x61 ... 0x66:
return UInt8(u - 0x61 + 10)
default:
return nil
}
}
let utf16 = self.utf16
guard let data = NSMutableData(capacity: utf16.count/2) else { return nil }
var i = utf16.startIndex
while i != utf16.endIndex {
guard let hi = convertToUInt8(u: utf16[i]) else { return nil }
//Need to convert following line to Swift 4
guard let lo = convertToUInt8(u: utf16[i.advanced(by: 1)]) else { return nil }
var value = hi << 4 + lo
data.append(&value, length: 1)
//Need to convert following line to Swift 4
i = i.advanced(by: 2)
}
return data as Data
}
}
如何以最佳方式转换advanced(by: n)
?
答案 0 :(得分:0)
你必须这样使用:
extension String {
public func fromHexStringtoData() -> Data? {
func convertToUInt8(u: UInt16) -> UInt8? {
switch(u) {
case 0x30 ... 0x39:
return UInt8(u - 0x30)
case 0x41 ... 0x46:
return UInt8(u - 0x41 + 10)
case 0x61 ... 0x66:
return UInt8(u - 0x61 + 10)
default:
return nil
}
}
let utf16 = self.utf16
guard let data = NSMutableData(capacity: utf16.count/2) else { return nil }
var i = utf16.startIndex
while i != utf16.endIndex {
guard let hi = convertToUInt8(u: utf16[i]) else { return nil }
//changed to Swift 4
guard let lo = convertToUInt8(u: utf16[utf16.index(i, offsetBy: 1)]) else { return nil }
var value = hi << 4 + lo
data.append(&value, length: 1)
//changed to Swift 4
i = utf16.index(i, offsetBy: 2)
}
return data as Data
}
}
答案 1 :(得分:0)
这不是您问题的直接答案,但您的代码会从头到尾扫描输入字符串。在这种情况下,您可以在不使用索引的情况下重写代码:
extension String {
public func fromHexStringToData() -> Data? {
func convertToUInt8(u: UInt16) -> UInt8? {
//...
}
var utf16Iterator = utf16.makeIterator()
var data = Data(capacity: utf16.count/2)
while let hiChar = utf16Iterator.next() {
guard
let hi = convertToUInt8(u: hiChar),
let loChar = utf16Iterator.next(),
let lo = convertToUInt8(u: loChar)
else { return nil }
let value = hi << 4 + lo
data.append(value)
}
return data
}
}
适用于Swift 3和Swift 4。