gpt4 book ai didi

python - 将整数数组转换为 unicode

转载 作者:太空宇宙 更新时间:2023-11-03 21:00:27 24 4
gpt4 key购买 nike

我有一个数组 arr=[07,10,11,05]。我实际上希望将这些值转换为十六进制并将其发送到函数。我知道我可以将这些值单独转换为十六进制,例如 hex(arr[0])、hex(arr[1]) 等。我现在所做的是将数组转换为unicode as arr =[u'07 0a 0b 05'] 手动然后发送到为我服务的函数。但是当我必须向 arr 中的 unicode 字符串发送数百个字符串(如

中所示)时,这太不切实际了
arr = [u'a3 06 06 01 00 01 01 00',
u'a3 06 06 02 00 01 02 00',
u'a3 06 06 03 00 01 03 00',
u'a3 06 06 04 00 01 04 00',
u'a3 06 06 05 00 01 05 00',
u'a3 06 06 06 00 01 06 00',
u'a3 06 06 07 00 01 07 00']

我使用循环将其一一发送(arr[i])。有没有一种方法可以动态地将整数数组转换为 unicode 并将其发送到易于接受此类 unicode 字符串的外部函数?

最佳答案

' '.join(map(hex,arr)) 是您要找的吗?

更具体地说:

import numpy as np

a = np.array([[1,2,3],[6,11,23]])

def strip_hexstring(s):
return s.strip('0x').strip('L')

def format_line(line):
return u'{}'.format(' '.join(map(strip_hexstring,map(hex,line))))

arr = [format_line(a[i,:]) for i in range(a.shape[0])]

如有必要,您可以像示例中那样用零填充。

关于python - 将整数数组转换为 unicode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55737995/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com