1

I have a string and would like to send them over the socket. How to send it using python 3.6?

string1 = "0001ff438a9b"

I would like to convert the sting and send it as bytes 0x00, 0x01,0xff,0x43,0x8a,0x9b over the socket.

It's different from the question marked as duplicate. My actual byte values are stored as string.

Kitcha
  • 1,641
  • 6
  • 19
  • 20

1 Answers1

2

I don't know if this is the best way to do this, but this works:

string1 = "0001ff438a9b"
hexSplits = [string1[index:index + 2] for index in range(0, len(string1), 2)] # split the string every 2 characters
normalizedSplits = [int("0x" + hexSplits[index], 16) for index in range(len(hexSplits))] # transform every split to base10 int
bytes = bytes(normalizedSplits) # join normalizedSplits into bytes variable as bytes

Now you can send bytes variable between sockets!

Tocutoeltuco
  • 56
  • 2
  • 2