python - Create text file of hexadecimal from binary -


i convert binary hexadecimal in format , save text file.

the end product should this:

"\x7f\xe8\x89\x00\x00\x00\x60\x89\xe5\x31\xd2\x64\x8b\x52" 

input executable file "a". current code:

with open('a', 'rb') f:     byte = f.read(1)     hexbyte = '\\x%02s' % byte     print hexbyte 

a few issues this:

  1. this prints first byte.
  2. the result "\x" , box this:

00 7f

in terminal looks this: enter image description here

why so? , finally, how save hexadecimals text file end product shown above?

edit: able save file text with

txt = open('out.txt', 'w') print >> txt, hexbyte txt.close() 

you can't inject numbers escape sequences that. escape sequences constants, so, can't have dynamic parts.

there's module this, anyway:

from binascii import hexlify open('test', 'rb') f:   print(hexlify(f.read()).decode('utf-8')) 

just use hexlify function on byte string , it'll give hex byte string. need decode convert ordinary string.

not quite sure if decode works in python 2, should using python 3, anyway.


Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -