Posts

Showing posts from March 25, 2019

Hex dump of a list of 16-bit numbers

Image
5 $begingroup$ This is some code which prints a hexdump of a list of 16 bit numbers, at 16 bytes distance for each, after the "print data" comment: #!/usr/bin/python3 # creating test data data = for i in range(500): data = data + [ i & 0xff, i >> 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] # print data c = 0 out = "" for i in range(int(len(data) / 16)): if i == 512: break low = data[i * 16] high = data[i * 16 + 1] d = low | (high << 8) out = out + ("%04x " % d) if (i % 16) == 15: out = out + "n" print(out) It works, but I think I can write it simpler with "join" and maybe list comprehension? But shouldn't be a cryptic one-liner or something, I'm just looking for a more idiomatic Python solut