1

I'm working with pySerial library to read some byte from Serial Port.

I need to store these bytes into a list, but I would store data in Hex format because the serial peripheral send to me specific command code like 0x10, 0x30, ...

The code:

readByte = serialCOM.read(1)
print 'Read Payload byte:' + str(readByte)
packetPayload.append(readByte)

creates a list of char.

Which is the way to create a list of hex values?

Thanks!

1
  • How do you figure out that you have a list of chars? If you are printing it, python justs decodes these bytes to ASCII symbols while doing so. Commented Mar 29, 2016 at 15:18

1 Answer 1

1

In Python, hex values are just an alternate view of integers:

>>> 1 == 0x01
True
>>> 16 == 0x10
True

So, here is a list:

>>> print([0x10, 0x20])
[16, 32]

It's also possible to use the int function to get integer value from a string:

>>> int('0x10', 16)  # here, the 16 is for the base, and 0x is optionnal
16
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your help!

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.