将十六进制值附加到 list/converting 字符串列表到十六进制
Appending hex values to a list/converting string lists to hex
我有一个数据框,其中每一行都是一个必须为十六进制的 CAN 消息。但是当我将行放入列表时,各个值存储为字符串。例如:
['0x0', '0x0', '0x0', '0x0', '0x0', '0x0', '0x0', '0x1']
有没有办法用 [0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1]
代替?
很简单!使用 base parameter on the int
function to convert from hex string to integer, and you can use hex 从整数返回到十六进制字符串。
# Convert hex string to int
str_vals = ['0x0', '0x0', '0x0', '0x0', '0x0', '0x0', '0x0', '0x1']
int_vals = [int(val, 16) for val in str_vals]
# Convert int to hex string
int_vals = [0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1]
str_vals = [hex(val) for val in int_vals]
我有一个数据框,其中每一行都是一个必须为十六进制的 CAN 消息。但是当我将行放入列表时,各个值存储为字符串。例如:
['0x0', '0x0', '0x0', '0x0', '0x0', '0x0', '0x0', '0x1']
有没有办法用 [0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1]
代替?
很简单!使用 base parameter on the int
function to convert from hex string to integer, and you can use hex 从整数返回到十六进制字符串。
# Convert hex string to int
str_vals = ['0x0', '0x0', '0x0', '0x0', '0x0', '0x0', '0x0', '0x1']
int_vals = [int(val, 16) for val in str_vals]
# Convert int to hex string
int_vals = [0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1]
str_vals = [hex(val) for val in int_vals]