0

So, I've been for hours searching for this conversion without success.

I need to convert some string with Python to HEX to send to a LED Controller.

So my purpose is to convert "Hello" into HEX String and then into a Byte String to send this directly to the controller via socket.

>>> "hello".encode("hex")
'68656c6c6f'

And then convert that string into something like:

string = b'\x68\x65\x6c\x6c\x6f'

Any advise on how to do this conversion? If I just:

'68656c6c6f'.decode("hex")

Then throws out the same "hello" instead of the bytestring.

7
  • 1
    If you need a bytearray – not a 'string' – then use: bytearray('68656c6c6f'.decode("hex")) Commented Dec 19, 2018 at 12:05
  • @usr2564301 Thats just also returning the same "hello", just like "string".decode("hex") does. Commented Dec 19, 2018 at 12:09
  • Perhaps you want to specify your Python version, as some of the syntax and results changed between 2.7 and 3.x. Commented Dec 19, 2018 at 12:09
  • I'm using Python 2.7 Commented Dec 19, 2018 at 12:10
  • Changing string to byte type in Python 2.7 suggests that is not possible ... Why is it important to have a 'byte string' instead of a regular string? Commented Dec 19, 2018 at 12:12

1 Answer 1

1

As stated by @usr2564301 there is no direct bytestring conversion in python 2.7 as bytestrings are just "strings". Using simple hex strings also works with the LED Controller, so no need to use bytestrings.

But the question remains open as this is not a solution... at least for python 2.7.

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

Comments

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.