Naval Kishore
Naval Kishore

Reputation: 145

Concatenating two Hex values: TypeError: unsupported operand type(s) for <<: 'str' and 'int'

In Python 2.7

Statement:

a = hex(17)<<8 | hex(22)<<10

print(a)

gives out:

Traceback (most recent call last):
  File "./prog.py", line 18, in <module>
TypeError: unsupported operand type(s) for <<: 'str' and 'int'

but if I replace them with real hex:

a = 0x11<<8 | 0x12<<10

this works

Upvotes: 0

Views: 303

Answers (1)

tango-2
tango-2

Reputation: 94

hex (..) returns a string to you and you are processing a string and an integer using the << operator, it's normal to get an error here. Instead, you need to cast the "hex number string" you get with hex, in base 16 to int because you are using hex numbers.

Example:

a = int(hex(17), 16) << 8 | int(hex(22), 16) << 10

Upvotes: 1

Related Questions