Python 如何将数字列表编码为base64

Python 如何将数字列表编码为base64,python,base64,Python,Base64,我有一个包含负数的列表,我想用base64对列表进行编码。我该怎么做 我试过: l = [-10, -48, 100, -20] bytes(l) 但我得到了一个错误: ValueError:字节必须在范围(0,256)内 我希望得到与Java代码相同的输出: byte[]bytes={-10,-48,100,-20}; 字符串结果=Base64Utils.encodeToUrlSafeString(字节); System.out.println(结果);//9tBk7A== 出现错误是因为

我有一个包含负数的列表,我想用base64对列表进行编码。我该怎么做

我试过:

l = [-10, -48, 100, -20]
bytes(l)
但我得到了一个错误:

ValueError:字节必须在范围(0,256)内
我希望得到与Java代码相同的输出:

byte[]bytes={-10,-48,100,-20};
字符串结果=Base64Utils.encodeToUrlSafeString(字节);
System.out.println(结果);//9tBk7A==

出现错误是因为Python需要无符号8位数据,这可以通过模运算
%
获得
unsigned==signed%2**num\u bits

import base64

l = [-10, -48, 100, -20]
# 2 ** 8 == 256
base64.b64encode(bytes(x % 256 for x in l))
# b'9tBk7A=='