解码十六进制UTF8字符

2024-09-28 18:55:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我有Apache生成的以下错误代码:

\xed\xe5 \xff\xb\xff\xe5\xf2\xf1\xff \xef\xf0\xe8\xeb\xee\xe6\xe5\xed\xe8\xe5\xec

我发现\x转义序列声明以下两个字符是UTF十六进制符号。例如,单词HELLO可以编码为\x48\x45\x4C\x4C\x4F。但我似乎不知道如何解码我的字符串? 我已经搜索了UTF编码表,但是没有找到任何与我的编码符号匹配的字符。我甚至不知道我应该找一个字节还是两个字节的编码。在

我在使用俄语语言环境的电脑上,如果有帮助的话。在


Tags: 编码字节apache符号字符utfxff错误代码
2条回答

使用^{}, ^{} encoding

>>> r'\x48\x45\x4C\x4C\x4F'.decode('unicode-escape')
u'HELLO'
>>> r'\x48\x45\x4C\x4C\x4F'.decode('string-escape')
'HELLO'

看起来你的字符串是cp-1251编码的:

s.decode('string_escape').decode('cp1251')

打印一些看起来有意义的内容(除了\xb不正确-复制粘贴错误?)公司名称:

^{pr2}$

I don't even know should I be looking for one byte or 2 bytes encodings.

这就是chardet来救援的地方:

import chardet

s = r'\xed\xe5 \xff?xb\xff\xe5\xf2\xf1\xff \xef\xf0\xe8\xeb\xee\xe6\xe5\xed\xe8\xe5\xec'

print chardet.detect(s.decode('string_escape'))
# {'confidence': 0.99, 'encoding': 'windows-1251'}

如果不懂python,也可以使用javascript,例如http://jsfiddle.net/L3Z4b/

相关问题 更多 >