i need to convert hex string to byte array using swift. I have tried with following example hex string
"7B737369643A32333A34333A34353A31352C70617373776F72643A31323334357D"
This is my code
func sendCmd (CMD: [UInt8],length: Int ,hexString: String)
{
var tosend = Array<UInt8>()
var lenval = Array<UInt8>(repeating: 0, count: 2)
lenval[0] = (UInt8)(length & 0xff)
lenval[1] = (UInt8)((length >> 8) & 0xff)
tosend.append(CMD[0])
tosend.append(CMD[1])
tosend.append(lenval[0])
tosend.append(lenval[1])
}
I got output like this
[123, 115, 115, 105, 100, 58, 50, 51, 58, 52, 51, 58, 52, 53, 58, 49, 53, 44, 112, 97, 115, 115, 119, 111, 114, 100, 58, 49, 50, 51, 52, 53, 125]
But i need to get output like this
[0x7B, 0x73, 0x73, 0x69, ......]
and finally i have to append output array to "tosend" byte array(Array).
Any idea?