python - Converting string into hex representation -


i looking way convert string hex string.

for example:

  • '\x01\x25\x89' -> '0x012589'
  • '\x25\x01\x00\x89' -> '0x25010089'

here have come with:

def to_hex(input_str):     new_str = '0x'      char in input_str:         new_str += '{:02x}'.format(ord(char))      return new_str 

it seems there better way haven't been able find yet.

you want binascii module.

>>> binascii.hexlify('\x01\x25\x89') '012589' >>> binascii.hexlify('\x25\x01\x00\x89') '25010089' 

Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -