开发者

Converting a hexadecimal character to an int in Python

I'm using the graphics library Pyglet to do some drawing and want to get the resulting image out as a Pyth开发者_运维问答on list (so I can convert it to a NumPy array).

Pyglet gives me a string of hex characters, like this: '\xff' (indicating a value of 255 at one pixel). How can I convert such a string to an int?

I've tried int('\xff', 16), but that doesn't work. Note that according to the documentation, '\xnn' is escaped and encoded as a hexedecimal char, but it doesn't tell me how to convert that to an int.


To get a NumPy array straight from a Python string, you can use

s = "\xff\x03"
a = numpy.frombuffer(s, numpy.uint8)

To get a list you can use

a =  map(ord, s)

An alternative to a list in Python 2.6 or above is to use bytesarray(s).


Try something like this:

a = '\xff'
print int(a.encode('hex'), 16)
255

Edit: sorry, the previous version had a mistake - decode instead of encode. This works.

Edit 2: I actually misread the question, as commenters noted. This may be already obvious but in case someone finds it helpful the regular python list solution would be:

>>> a = '\xff\xfe'
>>> [str(ord(char)) for char in a]
['255', '254']
>>> ' '.join([str(ord(char)) for char in a])
'255 254'


Here's a generalised approach that handles a hex string with variable length substrings, e.g.:

s = '5b1\n5\n3ad44'

The following code transforms a string with 3 million variable length hex substrings to a numpy integer array in 2 seconds (on a MacBook) through vectorisation:

import numpy as np, pandas as pd, cStringIO

s = ('5b1\n5\n3ad44\n' * 1000000)[:-1]    # 3m item hex string (variable element length)

# change hex to 2 digit decimal
for i in range(0,9): s = s.replace(str(i),'0' + str(i))  
for i in [['a','10'],['b','11'],['c','12'],['d','13'],['e','14'],['f','15']]:
    s = s.replace(i[0],i[1])

# read string into numpy
n = np.array(pd.read_csv(cStringIO.StringIO(s), header=None)[[0]]).astype('int64')    

# fix base
n = (n % 100) + 16 * ((n % 10000)/100) + 256 * ((n % 1000000)/10000) + 4096 * ((n % 100000000)/1000000) + 65536 * ((n % 10000000000)/100000000)

n[0:3]    # confirm correct transformation to [1457, 5, 240964]


Yes, \xff is the print representation of a hex value in a byte. But int() doesn't work on hex representations of bytes, but on string representation of numbers. A number in the base-16 world is '13' or 'ab' or 'ff'. Consequently (but still funnily), int('ff',16) works fine. If you want to go that route, you need to get rid of the '\x' :-).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜