我必须将一些代码翻译成 python。两者都是 32 位。我该如何验证?
const char kEncryptionKey[] = {
0xb0, 0x8c, 0x70, 0xcf, 0xbc, 0xb0, 0xeb, 0x6c, 0xab, 0x7e, 0x82, 0xc6,
0xb7, 0x5d, 0xa5, 0x20, 0x72, 0xae, 0x62, 0xb2, 0xbf, 0x4b, 0x99, 0x0b,
0xb8, 0x0a, 0x48, 0xd8, 0x14, 0x1e, 0xec, 0x07
};
const char kIntegrityKey[] = {
0xbf, 0x77, 0xec, 0x55, 0xc3, 0x01, 0x30, 0xc1, 0xd8, 0xcd, 0x18, 0x62,
0xed, 0x2a, 0x4c, 0xd2, 0xc7, 0x6a, 0xc3, 0x3b, 0xc0, 0xc4, 0xce, 0x8a,
0x3d, 0x3b, 0xbd, 0x3a, 0xd5, 0x68, 0x77, 0x92
};
所以..在 python 中:
kIntegrityKey = ????
kEncryptionKey = ????
最佳答案
您可以将十六进制数存储在列表中:
kIntegrityKey = [0xbf, 0x77, 0xec, 0x55, 0xc3, 0x01, 0x30, 0xc1, 0xd8, 0xcd, 0x18, 0x62,
0xed, 0x2a, 0x4c, 0xd2, 0xc7, 0x6a, 0xc3, 0x3b, 0xc0, 0xc4, 0xce, 0x8a,
0x3d, 0x3b, 0xbd, 0x3a, 0xd5, 0x68, 0x77, 0x92]
kEncryptionKey
也是一样。
更新:如果您担心数据大小,可以将数据打包到 array 中。 .例如:
>>> import array
>>> kIntegrityKey = array.array('B', [0xbf, 0x77, 0xec, 0x55, 0xc3, 0x01, 0x30, 0xc1,
... 0xd8, 0xcd, 0x18, 0x62, 0xed, 0x2a, 0x4c, 0xd2, 0xc7, 0x6a, 0xc3, 0x3b, 0xc0, 0xc4,
... 0xce, 0x8a, 0x3d, 0x3b, 0xbd, 0x3a, 0xd5, 0x68, 0x77, 0x92])
>>> len(kIntegrityKey)
32
>>> kIntegrityKey.itemsize
1
array
('B'
) 的第一个参数表示我们要使用 unisgned 列表存储第二个参数(您的无符号字符列表)字符
。
itemsize
属性在文档字符串中描述为:
itemsize -- the length in bytes of one array item
关于c++ - 如何在 Python 中表示字节数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12135087/