[英]String to hex value of FF in python 2.7
I know this is a basic question, but I am really struggling to work it out. 我知道这是一个基本的问题,但我真的努力去解决它。
I am trying to get a string to convert to FF in hex (ie OxFF) in python 2.7. 我试图得到一个字符串在Python 2.7转换为FF十六进制(即OXFF)。
I convert the string to bytearray then to hex using the following code: 我使用以下代码将字符串转换为字节数组,然后转换为十六进制:
>>> data = 'OxFF'
>>> array = bytearray(data)
>>> print binascii.hexlify(array)
4f784646
For context, I am sending a message over TCP/IP using twisted and the header needs to be FF FF 用于上下文中,我使用扭曲发送通过TCP / IP的消息和首部需要是FF FF
Any help would be amazing. 任何帮助都将是惊人的。
Cheers 干杯
You can create directly the header as a string literal: 您可以直接将标题创建为字符串文字:
>>> header = '\xff\xff'
>>> hedaer
'\xff\xff'
Converting from data
: 从
data
转换:
>>> data = '0xFF'
>>> binascii.unhexlify(data[2:])
'\xff'
The key is to skip over the 0x
prefix. 关键是跳过
0x
前缀。
Or as an integer: 或作为整数:
>>> int(data, 16)
255
where 255 is 0xFF of course. 其中255当然是0xFF。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.