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.

1
  • Sorry. I wanted to type bytes. Commented Feb 14, 2018 at 21:27

1 Answer 1

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!

Sign up to request clarification or add additional context in comments.

1 Comment

Awesome! It's what exactly I wanted.

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.