python 2.7 函数用于 python 3.8 hex/encoding 问题

python 2.7 function to use for python 3.8 hex/encoding issue

我有以下十六进制函数,它与 python 2.7

结合使用效果很好
#Python 2.7 code
 def dehex(d):
        return "".join(map(chr, d))

test = dehex([0xff,0xff,0xff,0xff,0x71,0x7f,0xd8,0xfe,0x03,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x00])
sock.sendto(test, (addr, port))
response = sock.recv(4096)

但是在 python 3.8 中,它不允许我发送“字符串”,因此它希望我使用 encode() 但这会扰乱测试 dehex 输出并导致格式错误的字节,例如以下:

#Python 3.8 fail code
 def dehex(d):
        return "".join(map(chr, d))

test = dehex([0xff,0xff,0xff,0xff,0x71,0x7f,0xd8,0xfe,0x03,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x00])
sock.sendto(test.encode(), (addr, port))
response = sock.recv(4096)

我怎样才能在 python 3.8 上使用 dehex 函数,这样我就不必使用 encode('utf-8')?

bytearray 应该与 2.7 和 3.8 兼容:

def dehex(d):
    return bytes(bytearray(d))