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)
6
  • You should check this: stackoverflow.com/questions/32769929/… Commented Feb 19, 2019 at 4:54
  • your code looks correct. note the the bytes keyword can be omitted Data(bytes) Commented Feb 19, 2019 at 5:21
  • Note that if startcmd is a string the printable digit character 0, ASCII value is 0x30same as 48 not 0x0 Commented Feb 19, 2019 at 5: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" Commented Feb 19, 2019 at 5: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. ^^ Commented Feb 20, 2019 at 7:03

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.