ivanpolipas
ivanpolipas

Reputation: 11

Adding bytes in python 2.7

Hi I've been trying to iterate through a bytearray, add up all the bytes and then append the result back into the same bytearray. The bytearray looks like this: key = bytearray([0x12, 0x10, 0x32]) However, when I call sum(key) I get the decimal representation of 84. Any idea how I can change the decimal representation and put it back into a hexadecimal format while keeping it of type int. Thank You

Upvotes: 1

Views: 1290

Answers (1)

Klaus D.
Klaus D.

Reputation: 14369

A bytearray is always a list of integers. How they are displayed is only their representation. The same applies to the way you entered them. Python understand the 0x?? (hexadicimal) and 0?? (octal) notation for integers but it will display the decimal notation.

To convert an integer to a string in the 0x?? format use hex(value).

Upvotes: 1

Related Questions