In Python, decoding hex file to decimal 'literal' equivalent -
i have hex file of first 100 1000000 digits of pi after decimal, in hex editor looks this:
14 15 92 65 35 89 79 32 etc. i need convert string (and integer) '1415926435897932' etc. not need 'converted' hex decimal, it's decimal, hex bytes. (i used word 'literal' in quotes in title, it's totally wrong word since implies string literal.)
when seek read file in python, kinds of encoding difficulties (because first byte, 15, non-printing character, etc.)
for example:
>>> f = open('pi100m.hexbin.000', 'rb') >>> contents = f.read() >>> f.close() >>> snippet = contents[:50] >>> snippet '\x14\x15\x92e5\x89y28f&c82yp(\x84\x19qi9\x93u\x10x \x97idy#\x07\x81d\x06(b\x08\x99\x86(\x03h%4!\x17\x06y' >>> # if 'print snippet', question marks in triangles >>> # nonprinting characters, not reproduce >>> # in stackoverflow i've tried lots of stackoverflow articles , python help docs encoding, have feeling i'm missing quite basic.
encode to hex, @ to the lowest degree have string representation:
integer_string = contents.encode('hex') demo:
>>> snippet = '\x14\x15\x92e5\x89y28f&c82yp(\x84\x19qi9\x93u\x10x \x97idy#\x07\x81d\x06(b\x08\x99\x86(\x03h%4!\x17\x06y' >>> snippet.encode('hex') '1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679' this hex editor does, represent bytes hex characters.
python python-2.7 character-encoding hex
No comments:
Post a Comment