2

I am trying to convert an integer. E.g 3063294273, to a 64-bit big endian encoded byte representation. So for example if I have a value = 1 my output should be
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01

What would be the best approach to do this in python?

Edit: I saw the other question provided in Convert a Python int into a big-endian string of bytes. But in the other question, the author was not asking for a specific size of the output. Which is important for my problem.

3
  • Your example shows 128 bits (16 instances of 8-bit (two hex digit)) values. Commented Jun 8, 2019 at 10:37
  • Possible duplicate of Convert a Python int into a big-endian string of bytes Commented Jun 8, 2019 at 10:38
  • I dont see the duplicate? In the provided other question, the author was not asking for a specific size of the output. Commented Jun 8, 2019 at 11:54

1 Answer 1

1

int.to_bytes

>>> value = 1
>>> value.to_bytes(8, 'big')
b'\x00\x00\x00\x00\x00\x00\x00\x01'

(Your sample output is 128 bits, so if you meant that, use 16.)

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.