在Objective-C中将十六进制字符串转换为UInt8数组

时间:2017-07-19 07:11:36

标签: objective-c

String to bytes []

  

NSString * strData = @" ca 20 fe c1 04 03 03 07 00 ac&#34 ;;

结果

  

UInt8 bytes [] = {0xca,0x20,0xfe,0xc1,0x04,0x03,0x03,0x07,0x01,0xac};

对于swift代码,如何转换为Objective-C

    let data = NSString(string : "ca 20 fe c1 04 03 03 07 01 ac");
    let dataArr = data.componentsSeparatedByString(" ")
    var bytes : [UInt8] = [];

    for item in dataArr {
        let byte = UInt8(item, radix: 16)
        bytes.append(byte!);
    }

    let hexData = NSData(bytes: bytes, length: 10)

0 个答案:

没有答案