0

I need your help to convert this Python script to Swift:

startcmd = b'0s000000000000000000'
data = NSData.dataWithBytes_length_(self.startcmd, len(self.startcmd))

Indeed, I wrote this but I know ... I am wrong:

let bytes : [UInt8] = [ 0x0, 0x73, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 ]
let data = Data(bytes:bytes)
seikida
  • 407
  • 6
  • 17
  • You should check this: https://stackoverflow.com/questions/32769929/convert-bytes-uint8-array-to-int-in-swift – Sagar Chauhan Feb 19 '19 at 04:54
  • your code looks correct. note the the bytes keyword can be omitted `Data(bytes)` – Leo Dabus Feb 19 '19 at 05:21
  • Note that if startcmd is a string the printable digit character `0`, ASCII value is `0x30`same as 48 not `0x0` – Leo Dabus Feb 19 '19 at 05:25
  • `let bytes: [UInt8] = [48, 115, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48] String(data: Data(bytes), encoding: .ascii)! // "0s000000000000000000"` – Leo Dabus Feb 19 '19 at 05:30
  • @Leo Dabus : Thank you so much for you help. Can you correct me. 1) I need to explod the string to get a array? 2) I need to loop the array and convert character to ASCII value? Thank you so much for your help. ^^ – seikida Feb 20 '19 at 07:03
  • https://stackoverflow.com/questions/29835242/whats-the-simplest-way-to-convert-from-a-single-character-string-to-an-ascii-va/29835826?r=SearchResults&s=1|47.3419#29835826 – Leo Dabus Feb 20 '19 at 15:51

0 Answers0