Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.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_Hex_Ascii - Fatal编程技术网

如何在python中将完整的ascii字符串转换为十六进制?

如何在python中将完整的ascii字符串转换为十六进制?,python,hex,ascii,Python,Hex,Ascii,我有这个字符串: string='{'id':'other\u aud1\u aud2','kW':15}' 简单地说,我希望我的字符串变成如下的十六进制字符串:'7B2769664273A276F746865725725F617564315F617564322C2727657373A31357D' 一直在尝试binascii.hexlify(字符串),但它不断返回: TypeError:需要类似字节的对象,而不是“str” 另外,它只需使用以下方法:bytearray.fromhex(data

我有这个字符串:
string='{'id':'other\u aud1\u aud2','kW':15}'

简单地说,我希望我的字符串变成如下的十六进制字符串:
'7B2769664273A276F746865725725F617564315F617564322C2727657373A31357D'

一直在尝试binascii.hexlify(字符串),但它不断返回:

TypeError:需要类似字节的对象,而不是“str”

另外,它只需使用以下方法:
bytearray.fromhex(data['string\u hex']).decode()

对于此处的整个代码,它是:

string_data = "{'id':'"+self.id+"','kW':"+str(value)+"}"
print(string_data)
string_data_hex = hexlify(string_data)
get_json = bytearray.fromhex(data['string_hex']).decode()
这也是python 3.6

您可以
encode()
字符串:

string = "{'id':'other_aud1_aud2','kW':15}"
h = hexlify(string.encode())
print(h.decode())
# 7b276964273a276f746865725f617564315f61756432272c276b57273a31357d

s = unhexlify(hex).decode()
print(s) 
# {'id':'other_aud1_aud2','kW':15}
您可以
encode()
字符串:

string = "{'id':'other_aud1_aud2','kW':15}"
h = hexlify(string.encode())
print(h.decode())
# 7b276964273a276f746865725f617564315f61756432272c276b57273a31357d

s = unhexlify(hex).decode()
print(s) 
# {'id':'other_aud1_aud2','kW':15}

这里比较棘手的一点是,Python3字符串是一个Unicode字符序列,与ASCII字符序列不同

  • 在Python2中,
    str
    类型和
    bytes
    类型是同义词,还有一个单独的类型,
    unicode
    ,它表示一个unicode字符序列。如果你有一个字符串,这让它成为一个谜:它是字节序列,还是某个字符集中的字符序列

  • 在Python3中,
    str
    现在的意思是
    unicode
    ,我们使用
    bytes
    表示过去的
    str
    。给定一个字符串(Unicode字符序列),我们使用
    encode
    将其转换为可以表示它的字节序列,如果存在这样的序列:

    >>> 'hello'.encode('ascii')
    b'hello'
    >>> 'sch\N{latin small letter o with diaeresis}n'
    'schön'
    >>> 'sch\N{latin small letter o with diaeresis}n'.encode('utf-8')
    b'sch\xc3\xb6n'
    
    但是:

    >>'sch\N{带分音符的拉丁文小写字母o}N'.encode('ascii'))
    回溯(最近一次呼叫最后一次):
    文件“”,第1行,在
    UnicodeEncodeError:“ascii”编解码器无法对位置3中的字符“\xf6”进行编码:序号不在范围内(128)
    

一旦你有了
字节
对象,你就知道该怎么做了。在Python2中,如果你有一个
str
,你就有一个
字节
对象;在Python3中,使用所选的编码使用
.encode

这里需要技巧的是,Python3字符串是一个Unicode字符序列,与ASCII字符序列不同

  • 在Python2中,
    str
    类型和
    bytes
    类型是同义词,还有一个单独的类型,
    unicode
    ,它表示一个unicode字符序列。如果你有一个字符串,这让它成为一个谜:它是字节序列,还是某个字符集中的字符序列

  • 在Python3中,
    str
    现在的意思是
    unicode
    ,我们使用
    bytes
    表示过去的
    str
    。给定一个字符串(Unicode字符序列),我们使用
    encode
    将其转换为可以表示它的字节序列,如果存在这样的序列:

    >>> 'hello'.encode('ascii')
    b'hello'
    >>> 'sch\N{latin small letter o with diaeresis}n'
    'schön'
    >>> 'sch\N{latin small letter o with diaeresis}n'.encode('utf-8')
    b'sch\xc3\xb6n'
    
    但是:

    >>'sch\N{带分音符的拉丁文小写字母o}N'.encode('ascii'))
    回溯(最近一次呼叫最后一次):
    文件“”,第1行,在
    UnicodeEncodeError:“ascii”编解码器无法对位置3中的字符“\xf6”进行编码:序号不在范围内(128)
    

一旦你有了
字节
对象,你就知道该怎么做了。在Python2中,如果你有一个
str
,你就有一个
字节
对象;在Python3中,使用所选的编码使用
.encode

我能够运行相同的代码段,方法是用
@mad_uuu.string_udata=“{id':”+self.id+”,'kW':“+str(value)+“}”print(string_data)string_data\uhex=hexlify(string_data)尝试过,但仍然是相同的问题,顺便说一句,这是python 3.6I能够运行相同的代码段,方法是用
@mad_uuu.string_data=”{id':“+self.id+”,'kW':“+str(value)+“}”print(string_data)string_data\u hex=hexlify(string_data)尝试过,但仍然是相同的问题,顺便说一句,这是Python3.6Oh没有看到编码,是的,我认为它工作了,谢谢你。Oh没有看到编码,是的,我认为它工作了,谢谢你。