0

This Python code gives exactly what I want:

a_hex_str = ':'.join('%02x' % b for b in a_bytes)

which is a string of the byte values in the byte array a_bytes printed in hex with a colon between each value.

I have been going mad trying to get the same output using the new format() function in place of the old % but all my attempts give errors.

I would be very grateful for assistance.

simonc8
  • 27
  • 1
  • 1
  • 3

2 Answers2

0
a_hex_str = ':'.join('{}02x'.format(b) for b in a_bytes)

Try this line hopefully it will work.

Vashdev Heerani
  • 660
  • 7
  • 21
  • No, it doesn't. It prints the bytes in decimal and puts 02x after each. I want to print the bytes in hex. – simonc8 Sep 28 '19 at 13:27
0
a_hex_str = ':'.join('{:02x}'.format(b) for b in a_bytes)

I adapted the answer from https://stackoverflow.com/questions/5661725/format-ints-into-string-of-hex

I didn't realise I needed the colon inside the curly braces, otherwise it gives an out of range error.

simonc8
  • 27
  • 1
  • 1
  • 3