0

The String to bytes[]

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

The Result is

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

for swift code, how convert to 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)
venlentine
  • 17
  • 8
  • https://stackoverflow.com/questions/7317860/converting-hex-nsstring-to-nsdata + https://stackoverflow.com/questions/43907919/how-to-convert-nsdata-to-array-of-uint8-ins-ios-objective-c ? – Larme Jul 19 '17 at 07:46
  • @Larme thanks, but can't work, the data no correct,ca convert to 202,but -54 is correct – venlentine Jul 19 '17 at 08:05
  • "ca convert to 202,but -54 is correct" what? What -54? What code did you tried exactly? – Larme Jul 19 '17 at 08:08
  • @Larme The Java Code screenshot http://uploads.im/tj7Fi.png – venlentine Jul 19 '17 at 08:55
  • `UInt8` How are you supposed to get -54 with `UInt8`, which `U` stands for unsigned. That's where lies your issue. Also, in the Swift Code, there should also have the issue. – Larme Jul 19 '17 at 08:56
  • @Larme I want use socket send hex data to server,the swift\java code send success, but objective-c can't – venlentine Jul 19 '17 at 09:13

0 Answers0