Python 十六进制变量赋值 [英] Python hex variable assignment

查看:48
本文介绍了Python 十六进制变量赋值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个变量来存储由套接字发送的数据.当我在我的程序中分配它时它可以工作,但是当我从文件中读取它时它被视为一个字符串.

示例:

data = '\x31\x32\x33'打印数据

输出

123 # <--- 这是我从文件读取分配数据时想要的结果f = open('datafile') <--- 数据文件在一行中包含 \x31\x32\x33数据 = f.readline()打印数据

输出

\x31\x32\x33 # <--- 希望它打印 123,而不是 \x31\x32\x33.

解决方案

在 Python 中字符串 '\x31\x32\x33' 实际上只有三个字符 '\x31' 是序号为 0x31 (49) 的字符,所以 '\x31' 等价于 '1'.听起来您的文件实际上包含 12 个字符 \x31\x32\x33,相当于 Python 字符串 '\\x31\\x32\\x33',其中转义的反斜杠表示单个反斜杠字符(这也可以用原始字符串文字 r'\x31\x32\x33' 表示).

如果您确实确定该数据应该是 '123',那么您需要查看该文件是如何写入的.如果这是你可以控制的,那么你应该在那里解决它,这样你就不会得到由表示十六进制转义的几个字节组成的数据.

也有可能,无论写入数据的内容已经在使用某种数据交换格式(类似于 JSON),在这种情况下,您无需更改其写入方式,只需使用解码器即可数据交换格式(如 json.loads(),但这不是 JSON).

如果以上两种方法都不是您真正想要的,而您只想弄清楚如何将像 r'\x31\x32\x33' 这样的字符串转换为 '123' 在 Python 中,您可以这样做:

<预><代码>>>>r'\x31\x32\x33'.decode('string_escape')'123'

或者在 Python 3.x 中:

<预><代码>>>>br'\x31\x32\x33'.decode('unicode_escape')'123'

根据评论,您实际上正在获得像 '313233' 这样的十六进制字符串,将这样的字符串转换为 '123' 你可以使用十六进制解码:

<预><代码>>>>'313233'.decode('hex')'123'

或者在 Python 3.x 上:

<预><代码>>>>bytes.fromhex('313233').decode('utf-8')'123'

I'm using a variable to store data that gets sent by a socket. When I assign it in my program it works but when I read it from a file it is treated as a string.

Example:

data = '\x31\x32\x33'

print data

Outputs

123   # <--- this is the result I want when I read from a file to assign data


f = open('datafile')  <--- datafile contains \x31\x32\x33 on one line

data = f.readline()

print data

Outputs

\x31\x32\x33  # <--- wanted it to print 123, not \x31\x32\x33. 

解决方案

In Python the string '\x31\x32\x33' is actually only three characters '\x31' is the character with ordinal 0x31 (49), so '\x31' is equivalent to '1'. It sounds like your file actually contains the 12 characters \x31\x32\x33, which is equivalent to the Python string '\\x31\\x32\\x33', where the escaped backslashes represent a single backslash character (this can also be represented with the raw string literal r'\x31\x32\x33').

If you really are sure that this data should be '123', then you need to look at how that file is being written. If that is something you can control then you should address it there so that you don't end up with data consisting of several bytes representing hex escapes.

It is also possible that whatever is writing this data is already using some data-interchange format (similar to JSON), in which case you don't need to change how it is written, you just need to use a decoder for that data-interchange format (like json.loads(), but this isn't JSON).

If somehow neither of the above are really what you want, and you just want to figure out how to convert a string like r'\x31\x32\x33' to '123' in Python, here is how you can do that:

>>> r'\x31\x32\x33'.decode('string_escape')
'123'

Or in Python 3.x:

>>> br'\x31\x32\x33'.decode('unicode_escape')
'123'

edit: Based on comments it looks like you are actually getting hex strings like '313233', to convert a string like that to '123' you can decode using hex:

>>> '313233'.decode('hex')
'123'

Or on Python 3.x:

>>> bytes.fromhex('313233').decode('utf-8')
'123'

这篇关于Python 十六进制变量赋值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆