Tuesday, August 6, 2019

Python字符串十六进制编码的破事


刚才做ctf题目发现这种十六进制编码还是挺烦的, 整理一下思绪

1
2
string <-> hex string
string <-> dec integer

彻底解决这些烦人的玩意

string <-> hex string

string -> hex string


1
2
3
4
5
6
# all python version
import binascii
binascii.b2a_hex('string')
binascii.hexlify('string')
# only python2
'string'.encode('hex')

string <- hex string


1
2
3
4
5
6
# all python version
import binascii
binascii.a2b_hex('737472696e67')
binascii.unhexlify('737472696e67')
# only python2
'737472696e67'.decode('hex')

string <-> dec integer

string -> dec integer


1
2
# string -> hex string -> integer
int(binascii.hexlify('string'), 16)

string <- dec integer


1
2
3
# integer -> hex string -> string
binascii.unhexlify('%x' % 126943972912743)
binascii.unhexlify('{0:x}'.format(126943972912743))

Reference

0 comments:

Post a Comment