我不知道你想如何獲取十六進制值後的整數...他們應該是1,2或x數字?
所以,我這樣做:
import re
# convert unicode or string to raw
def raw(s):
if isinstance(s, str):
s = s.encode('string-escape')
elif isinstance(s, unicode):
s = s.encode('unicode-escape')
return s
s = '\x1e\x00\x00\x00\xf0\x0f184203308373388492761797873987'
print [ re.sub(r'\\', r'0', raw(i)) for i in s]
而且我得到這個:
['0x1e', '0x00', '0x00', '0x00', '0xf0', '0x0f', '1', '8', '4', '2', '0', '3', '3', '0', '8', '3', '7', '3', '3', '8', '8', '4', '9', '2', '7', '6', '1', '7', '9', '7', '8', '7', '3', '9', '8', '7']
希望能夠幫助
編輯:簡化了列表理解
編輯:如果你真的想擺脫非十六進制值,那麼你可以使用
>>> print [int(re.sub(r'\\', r'0', raw(i)), 16) for i in s if len(raw(i))>1]
[30, 0, 0, 0, 240, 15]
和比較...甚至更好的方式,因爲DSM說
>>> s = '\x1e\x00\x00\x00\xf0\x0f184203308373388492761797873987'
>>> ord(max(s))
240
你指的輸出'lz4.compress()'?如果是這樣,該字符串不是十六進制數字的列表。例如,'lz4.compress(「z」* 10)=='\ n \ x00 \ x00 \ x00 \ xa0zzzzzzzzzz''。 – DSM
@DSM我指的是lz4.dumps()的輸出。但是,輸出看起來很相似,除了我的例子中的結尾是整數。 '\ x'後字符串的部分是什麼? – mac389
同樣的事情存在:考慮'lz4.dumps(「z」* 10)=='\ n \ x00 \ x00 \ x00 \ xa0zzzzzzzzzz''。我同意「x」後面的兩個數字是十六進制的,但是哪些字符能得到它們,哪些不是編碼的僥倖。例如,查看'lz4.dumps(「\ xff」* 10)'。你是否簡單地在max(ord(x)for x in s)之後? – DSM