MainMailGuy1119
MainMailGuy1119

Reputation: 23

Converting a hex string into the correct form for the bytes function

I am getting two hex string from a function:

def getHex(hexIn):
    return hex(hexIn >> 8), hex(hexIn & 0xFF)

Then I want to do this:

Hi, Lo = getHex(14290)
Cmd = bytes([0x66, 0x44, 0xA6, Hi, Lo])

But I get the error:

TypeError: 'str' object cannot be interpreted as an integer

How can I convert this to a form like 0x66?

Upvotes: 2

Views: 520

Answers (1)

John1024
John1024

Reputation: 113814

The error that you report indicates that you are using python3.

Replace:

Cmd = bytes([0x66, 0x44, 0xA6, Hi, Lo])

With:

Cmd = bytes([0x66, 0x44, 0xA6, int(Hi, 16), int(Lo, 16)])

The hex function that is used in getHex returns strings. bytes wants a list of integers. The solution is to convert the strings to integers using int.

Upvotes: 1

Related Questions