Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python如何让数组中的ASCII编码的十六进制被视为十六进制,而不是字符串中的字符?_Python_Python 2.7_Serial Port_Ascii - Fatal编程技术网

Python如何让数组中的ASCII编码的十六进制被视为十六进制,而不是字符串中的字符?

Python如何让数组中的ASCII编码的十六进制被视为十六进制,而不是字符串中的字符?,python,python-2.7,serial-port,ascii,Python,Python 2.7,Serial Port,Ascii,我需要使用串行端口与设备通信,并且需要向端口写入字符串。我通过键盘输入字符串,同时使用循环将每个十六进制字符写入字符串。我需要将这个字符串\u-to\u-write转换为字符串。如何使Python将四个字符的组标识为十六进制。只需使用内置的编码库,例如: In [301]: string_to_write Out[301]: '0x010x530x380x430x430x330x460x460x300x300x300x300x300x300x310x320x310x0D' In [302]:

我需要使用串行端口与设备通信,并且需要向端口写入字符串。我通过键盘输入字符串,同时使用循环将每个十六进制字符写入字符串。我需要将这个字符串\u-to\u-write转换为字符串。如何使Python将四个字符的组标识为十六进制。

只需使用内置的编码库,例如:

In [301]: string_to_write
Out[301]: '0x010x530x380x430x430x330x460x460x300x300x300x300x300x300x310x320x310x0D'

In [302]: len(string_to_write)
Out[302]: 72

In [303]: thestring="\x01\x53\x38\x43\x43\x33\x46\x46\x30\x30\x30\x30\x30\x30\x31\x32\x31\x0D"

In [304]: print thestring
S8CC3FF000000121

In [305]: len(thestring)
Out[305]: 18

只需使用内置的编码库,例如:

In [301]: string_to_write
Out[301]: '0x010x530x380x430x430x330x460x460x300x300x300x300x300x300x310x320x310x0D'

In [302]: len(string_to_write)
Out[302]: 72

In [303]: thestring="\x01\x53\x38\x43\x43\x33\x46\x46\x30\x30\x30\x30\x30\x30\x31\x32\x31\x0D"

In [304]: print thestring
S8CC3FF000000121

In [305]: len(thestring)
Out[305]: 18

您需要将
string\u剪切为长度为4的字符串,将这些字符串转换为整数,然后将每个整数转换为字符。在Python2中有一种有效的方法(Python3中需要一种不同的方法)。请注意,此代码假定所有十六进制代码都正好包含4个字符,前导为
0x
。此脚本还使用
binascii.hexlify
以方便的格式打印输出数据

>>> "hello".encode("hex")
'68656c6c6f'
>>> "68656c6c6f".decode("hex")
'hello'
>>>
输出

from binascii import hexlify

def hex_to_bin(s):
    return ''.join([chr(int(''.join(u), 16)) for u in zip(*[iter(s)] * 4)])

s2w = '0x010x530x380x430x430x330x460x460x300x300x300x300x300x300x310x320x310x0D'
thestring = "\x01\x53\x38\x43\x43\x33\x46\x46\x30\x30\x30\x30\x30\x30\x31\x32\x31\x0D"

out = hex_to_bin(s2w)
print repr(thestring)
print repr(out)
print hexlify(out)
print thestring == out

您需要将
string\u剪切为长度为4的字符串,将这些字符串转换为整数,然后将每个整数转换为字符。在Python2中有一种有效的方法(Python3中需要一种不同的方法)。请注意,此代码假定所有十六进制代码都正好包含4个字符,前导为
0x
。此脚本还使用
binascii.hexlify
以方便的格式打印输出数据

>>> "hello".encode("hex")
'68656c6c6f'
>>> "68656c6c6f".decode("hex")
'hello'
>>>
输出

from binascii import hexlify

def hex_to_bin(s):
    return ''.join([chr(int(''.join(u), 16)) for u in zip(*[iter(s)] * 4)])

s2w = '0x010x530x380x430x430x330x460x460x300x300x300x300x300x300x310x320x310x0D'
thestring = "\x01\x53\x38\x43\x43\x33\x46\x46\x30\x30\x30\x30\x30\x30\x31\x32\x31\x0D"

out = hex_to_bin(s2w)
print repr(thestring)
print repr(out)
print hexlify(out)
print thestring == out

可能您可以将此作为示例:

'\x01S8CC3FF000000121\r'
'\x01S8CC3FF000000121\r'
01533843433346463030303030303132310d
True   
作品:


可能您可以将此作为示例:

'\x01S8CC3FF000000121\r'
'\x01S8CC3FF000000121\r'
01533843433346463030303030303132310d
True   
作品:


谢谢你的铁拳编辑。你在用Python2吗?是的,我在用Python2.7。谢谢你的铁拳编辑。你在用Python2吗?是的,我在用Python2.7。这不符合OP的要求。这不符合OP的要求。太好了。工作起来很有魅力。非常感谢,太好了。工作起来很有魅力。非常感谢你。