Skip to content

Instantly share code, notes, and snippets.

@erikson1970
Last active October 24, 2020 02:52
Show Gist options
  • Select an option

  • Save erikson1970/4bb5f7f790d99b6a6dc8c8fb9a2a67fd to your computer and use it in GitHub Desktop.

Select an option

Save erikson1970/4bb5f7f790d99b6a6dc8c8fb9a2a67fd to your computer and use it in GitHub Desktop.
Random Useful Python one-to-two-liners
# pretty print string as Hex
pphex = lambda data:"0x0000 " + ''.join("{}{}{}".format("{0:#0{1}x}".format(ord(xxx),4)[2:] , " " if (1+i) % 8 == 0 else ":", "\t{} {}\n{} ".format(data[i-15:i-7],data[i-7:i+1],"{0:#0{1}x}".format(i,6)) if (1+i) % 16 == 0 else "") for i,xxx in enumerate(data)) + (" " if len(data) % 16 < 8 else "").join((lambda ii:[":".join([" "] *((8-ii) % 8 if ii<8 else 0)) , ":".join([" "] * (0 if ii==0 else (8-ii) % 8 if ii>8 else 8))]) (len(data) % 16)) + ("\t{} {}".format(data[-(len(data) % 16):][:8],data[-(len(data) % 16):][8:16]) if (len(data) % 16) != 0 else "")
data=b'Once upon a time in a land called Xanth, there was a king who was sad because his queen had died in childbirth of their only daughter. The princess was the \x02apple of the King\'s eye.'
#
print(pphex(data[:-5].decode("utf-8")))
#
#0x0000 4f:6e:63:65:20:75:70:6f 6e:20:61:20:74:69:6d:65 Once upo n a time
#0x000f 20:69:6e:20:61:20:6c:61 6e:64:20:63:61:6c:6c:65 in a la nd calle
#0x001f 64:20:58:61:6e:74:68:2c 20:74:68:65:72:65:20:77 d Xanth, there w
#0x002f 61:73:20:61:20:6b:69:6e 67:20:77:68:6f:20:77:61 as a kin g who wa
#0x003f 73:20:73:61:64:20:62:65 63:61:75:73:65:20:68:69 s sad be cause hi
#0x004f 73:20:71:75:65:65:6e:20 68:61:64:20:64:69:65:64 s queen had died
#0x005f 20:69:6e:20:63:68:69:6c 64:62:69:72:74:68:20:6f in chil dbirth o
#0x006f 66:20:74:68:65:69:72:20 6f:6e:6c:79:20:64:61:75 f their only dau
#0x007f 67:68:74:65:72:2e:20:20 54:68:65:20:70:72:69:6e ghter. The prin
#0x008f 63:65:73:73:20:77:61:73 20:74:68:65:20:02:61:70 cess was the ap
#0x009f 70:6c:65:20:6f:66:20:74 68:65:20:4b:69:6e:67:27 ple of t he King'
#0x00af 73: : : : : : : : : : : : : : s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment