1

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

1
  • 2
    The values in the array are the same whether they are represented in decimal or hexadecimal. Commented Jun 27, 2016 at 14:39

1 Answer 1

1

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).

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

1 Comment

okay thank you! I did't know that you could add a decimal number to a bytearray.

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.