May 24, 2018

Python :: break a string into lines of Hex

(Last Updated On: 17th September 2016)

You may have seen output like this in programs such as HxD and it can be really useful.  I forgot the exact reason I needed this script, but it will take a string and output it in definable line lengths of hexidecimal.  It may be easier to see it in the screen below.  If you find it useful, if you don’t then try looking around for something that might be useful.

pythonhexchunks

import sys

def StringToHex(Input):
x = " --- "
for Character in Input:
x = x + " " + hex(ord(Character))
return(Input+" "+str(x))

def BreakIntoChunks(Input,ChunkSize):
x = 1
line = ""
hexline = ""
output=""
for Character in Input:
line = line + Character
hexline = hexline + " " + hex(ord(Character))
if x % ChunkSize == 0:
hexline = hexline + str(chr(int("0x0a",0)))
output = output + line + " ---" + hexline
hexline = ""
line = ""
x = x + 1
else:
if(hexline):
output = output + line + " "*(ChunkSize - len(line)) + " ---" + hexline
return(output)

print("")
print(BreakIntoChunks("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum turpis ipsum, varius eget metus quis, venenatis interdum tortor.",17))

Previous «
Next »

Simon is a sysadmin for Local Government in North Yorkshire with a real passion for security and coding.

Leave a Reply

Subscribe to SYNACK via Email

%d bloggers like this: