简体   繁体   English

在 Python 中将十六进制字符串表示形式转换为实际字节

[英]Converting a hex-string representation to actual bytes in Python

i need to load the third column of this text file as a hex string我需要将此文本文件的第三列加载为十六进制字符串

http://www.netmite.com/android/mydroid/1.6/external/skia/emoji/gmojiraw.txt http://www.netmite.com/android/mydroid/1.6/external/skia/emoji/gmojiraw.txt

>>> open('gmojiraw.txt').read().split('\n')[0].split('\t')[2]
'\\xF3\\xBE\\x80\\x80'

how do i open the file so that i can get the third column as hex string:我如何打开文件,以便我可以将第三列作为十六进制字符串:

'\xF3\xBE\x80\x80'

i also tried binary mode and hex mode, with no success.我也尝试过二进制模式和十六进制模式,但没有成功。

You can:你可以:

  1. Remove the \\x -es删除\\x -es
  2. Use .decode('hex') on the resulting string在结果字符串上使用 .decode('hex')

Code:代码:

>>> '\\xF3\\xBE\\x80\\x80'.replace('\\x', '').decode('hex')
'\xf3\xbe\x80\x80'

Note the appropriate interpretation of backslashes.请注意对反斜杠的适当解释。 When the string representation is '\\xf3' it means it's a single-byte string with the byte value 0xF3.当字符串表示为 '\\xf3' 时,这意味着它是一个字节值为 0xF3 的单字节字符串。 When it's '\\\\xf3', which is your input, it means a string consisting of 4 characters: \\ , x , f and 3当它是 '\\\\xf3' 时,这是您的输入,它意味着一个由 4 个字符组成的字符串: \\xf3

Quick'n'dirty reply快速回复

your_string.decode('string_escape')

>>> a='\\xF3\\xBE\\x80\\x80'
>>> a.decode('string_escape')
'\xf3\xbe\x80\x80'
>>> len(_)
4

Bonus info奖金信息

>>> u='\uDBB8\uDC03'
>>> u.decode('unicode_escape')

Some trivia一些琐事

What's interesting, is that I have Python 2.6.4 on Karmic Koala Ubuntu ( sys.maxunicode==1114111 ) and Python 2.6.5 on Gentoo ( sys.maxunicode==65535 );有趣的是,我在 Karmic Koala Ubuntu ( sys.maxunicode==1114111 ) 上有 Python 2.6.4,在 Gentoo 上有 Python 2.6.5 ( sys.maxunicode==65535 ); on Ubuntu, the unicode_escape-decode result is \?\? and on Gentoo it's u'\\U000fe003' , both correctly of length 2. Unless it's something fixed between 2.6.4 and 2.6.5, I'm impressed the 2-byte-per-unicode-character Gentoo version reports the correct character.在 Ubuntu 上,unicode_escape-decode 结果是\?\?而在 Gentoo 上它是u'\\U000fe003' ,长度都是 2 -per-unicode-character Gentoo 版本报告正确的字符。

If you are using Python2.6+ here is a safe way to use eval如果您使用的是 Python2.6+,这里是使用 eval 的安全方法

>>> from ast import literal_eval
>>> item='\\xF3\\xBE\\x80\\x80'
>>> literal_eval("'%s'"%item)
'\xf3\xbe\x80\x80'

去掉 "\\x" 作为 Eli 的答案后,你可以这样做:

int("F3BE8080",16)

如果您信任来源,则可以使用eval('"%s"' % data)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM