新濠天地 > E编程 > 新濠天地        // 拆分字符串 转成10进制 计算

原标题:新濠天地        // 拆分字符串 转成10进制 计算

浏览次数:103 时间:2019-09-20

    斯维夫特3.0 进制转变

  模块能够平素动用,写的不是很好,款待来喷

 

    // Data -> HexStrings

    func dataToHexStringArrayWithData(data: NSData) -> [String] {

        let byteArray:[Int] = DataToIntWithData(data: data)

        var byteStrings: [String] = []

        for (_,value) in byteArray.enumerated() {

            byteStrings.append(ToHex(tmpid: value))

        }

        return byteStrings

    }

    

    // HexString -> Data

    func hexToBytes(bytesStr: String) -> Data? {

        var data = Data(capacity: bytesStr.characters.count / 2)

        let regex = try! NSRegularExpression(pattern: "[0-9a-f]{1,2}", options: .caseInsensitive)

        regex.enumerateMatches(in: bytesStr, options: [], range: NSMakeRange(0, bytesStr.characters.count)) { match, flags, stop in

            let byteString = (bytesStr as NSString).substring(with: match!.range)

            var num = UInt8(byteString, radix: 16)!

            data.append(&num, count: 1)

        }

        guard data.count > 0 else {

            return nil

        }

        return data

    }

    

    

    // Data -> 10

    func DataToIntWithData(data: NSData) -> [Int] {

        var byteArray:[Int] = []

        for i in 0..<data.length {

            var temp:Int = 0

            data.getBytes(&temp, range: NSRange(location: i,length:1 ))

            byteArray.append(temp)

        }

        return byteArray

    }

    

    // 10 -> 16

    func ToHex(tmpid: Int) -> String {

        let leftInt: Int  = tmpid / 16

        let rightInt: Int = tmpid % 16

        var leftIndex: String = ""

        var rightIndex: String = ""

        let numberArray = ["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"]

        for i in 0..<numberArray.count {

            if i == leftInt {

                leftIndex = numberArray[i]

            }

            if i == rightInt {

                rightIndex = numberArray[i]

            }

        }

        return "(leftIndex)(rightIndex)"

    }    

    

    // 16 -> 10

    func ToInt(hexString: String) -> Int {

        var _hexString = hexString

        // 转小写

        _hexString = _hexString.lowercased()

        // 拆分字符串 转成10进制 总括

        var term = 0

        let intArray = ["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"];

        let _hex = NSString(string: _hexString)

        let left = String(format: "%c", _hex.character(at: 0))

        let right = String(format: "%c", _hex.character(at: 1))

        var leftValue = 0

        var rightValue = 0

        for (index,value) in intArray.enumerated() {

            if left == value {  

                leftValue = index

            }  

            if right == value {

                rightValue = index

            }

        }

        term = leftValue * 16 + rightValue

        return term;

    }

 

-----------------这里是分水线,后续更新---------------------------------

本文由新濠天地发布于E编程,转载请注明出处:新濠天地        // 拆分字符串 转成10进制 计算

关键词:

上一篇:4、泊松分布,4、随机变量期望值

下一篇:没有了