0

I need to change This (string):

"0xab,0xcd,0x00,0x01,0xff,0xff,0xab,0xcd,0x00,0x00,0x00,0x00,0x10,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00"

to (bytes)

[0xab,0xcd,0x00,0x01,0xff,0xff,0xab,0xcd,0x00,0x00,0x00,0x00,0x10,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ]

using java.

I've successfully implement that in swift.

// Your original string
let hexString = "0xab,0xcd,0x00,0x01,0xff,0xff,0xab,0xcd,0x00,0x00,0x00,0x00,0x10,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00"

// Remove all of the "0x"
let cleanString = hexString.replacingOccurrences(of: "0x", with: "")

// Create an array of hex strings
let hexStrings = cleanString.components(separatedBy: ",")

// Convert the array of hex strings into bytes (UInt8)
let bytes = hexStrings.flatMap { UInt8($0, radix: 16) }
  • Per your example, it looks like all you did was convert the comma delimited string into an array. Is this correct? All you might need to do is a string split on the comma character. https://stackoverflow.com/questions/3413586/string-to-string-array-conversion-in-java – Cody May 31 '17 at 15:42

1 Answers1

0

Have a look at the String Doc, there are "split" and "replaceAll" methods. To convert String to hex value, have a look at this question.