在Swift 5中拆分十六进制字符串

时间:2019-04-15 10:48:12

标签: swift

我需要迅速将十六进制字符串拆分成对,即:

ff5405ff 54 05

我实际上是从javascript翻译过来的,但是相比之下,我发现的唯一解决方案似乎真的很复杂。

let tcStrt = data.index(data.startIndex, offsetBy:tcOffst)//index for selection
let tcEnd = data.index(tcStrt, offsetBy: 8)//index for end of selection
let tcRng = tcStrt..<tcEnd//complete range of selection
let subStr = data[tcRng]//value of selection
let tcData = String(subStr)//convert to string
var tcArr = [String]()//array for each hex pair

for (index, _) in tcData.enumerated(){
    if index%2 == 0 && index != 1{
    tcArr.append((String(tcData[tcData.index(tcData.startIndex, offsetBy: index)])+String(tcData[tcData.index(tcData.startIndex, offsetBy: index+1)])))
        print (tcArr)
    }

有更好的方法吗?

谢谢

0 个答案:

没有答案