swift POS8583报文组装工具

前端之家收集整理的这篇文章主要介绍了swift POS8583报文组装工具前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

最近两天在学习Swift,想着把OC里边的8583POS工具转成Swift版本。

今天做了几个转换工具

1、data转换为十六进制字符串

2、十六进制字符串转换为 data

3、十六进制字符串转为ASC

4、ASC码转为十六进制字符串


代码如下

//

// StringPosUtilTransExtension.swift

// SwiftPOS

// Created by 刘通超 on 15/12/16.

// Copyright © 2015刘通超. All rights reserved.

//


import Foundation


extension String{

//MARK: data 转换为十六进制字符串

/**

data 转换为十六进制字符串

<24211D34 98FF62AF> --> "24211D3498FF62AF"

- parameter data: 要转换的data

- returns: 转换后的字符串

*/

static func hexStringFromData(data data:NSData)->String{

let dataLength = data.length

let buffer = UnsafePointer<UInt8>(data.bytes)

var result = ""

for(var i=0;i<dataLength;i++){

let num = Int(buffer[i]&0xff)

result += decIntToHexString(decNum: num)

}

return result

}

//MARK: 十六 进制字符串转换为 data

/**

十六 进制字符串转换为 data

"24211D3498FF62AF" --> <24211D34 98FF62AF>

- parameter hexStr: 要转换的字符串

- returns: 转换后的data数据

*/

static func dataFromHexString(hexStr hexStr:String)->NSData{

let strLength = hexStr.utf16.count

let data = NSMutableData()

for(var index=0; index+2<=strLength; index+=2){

let subStr = hexStr.substringWithRange(Range(start: hexStr.startIndex.advancedBy(index),end: hexStr.startIndex.advancedBy(index+2)))

let dataInt = UnsafeMutablePointer<UInt32>.alloc(1)

NSScanner.init(string: subStr).scanHexInt(dataInt)

data.appendBytes(dataInt,length: 1)

}

return data

}

//MARK: hex字符串转为ASC 00 --> 3030

/**

hex字符串转为ASC 00 --> 3030

- parameter hexString: hex字符串

- returns: 转码后的ASC字符串

*/

static func ascStringFromHexString(hexString:String)->String{

var result = ""

let length = hexString.utf16.count

let buffer = hexString.cStringUsingEncoding(NSUTF8StringEncoding)

for(var i=0;i<length;i++){


result += decIntToHexString(decNum:Int(buffer![i]))

}

return result

}

//MARK: ASC码转为Hex字符串 3030 --> 00

/**

ASC码转为Hex字符串 3030 --> 00

- parameter ascStr: ASC字符串

- returns: 转码后的Hex字符串

*/

static func hexStringFromAscString(ascStr ascStr:String)->String{

let dda = String.dataFromHexString(hexStr: ascStr)

let result = String.init(data: dda,encoding: NSUTF8StringEncoding)

return result!

}


/**

UInt8 十进制数转为十六进制字符串(两位)

- parameter num: 十进制数字

- returns: 两位十六进制字符串

*/

private staticfunc decIntToHexString(decNum num:Int)->String{

let temp = String(format: "%0x",arguments: [num])

let result = (temp.utf16.count <=1) ?"0"+temp: temp;

return result

}

}


亲测可用


let strToData =String.dataFromHexString(hexStr:"3132332e34353678")

let dataToStr = String.hexStringFromData(data: strToData)

let ascToHex = String.hexStringFromAscString(ascStr: dataToStr)

let hexToAsc = String.ascStringFromHexString(ascToHex)

print("strToData:\(strToData)")

print("dataToStr:\(dataToStr)")

print("hexToAsc:\(hexToAsc)")

print("ascToHex:\(ascToHex)")


运行结果为:


strToData:<3132332e 34353678>

dataToStr:3132332e34353678

hexToAsc:3132332e34353678

ascToHex:123.456x



如有问题请及时沟通 —— LC 原文链接:https://www.f2er.com/swift/325121.html

猜你在找的Swift相关文章