简体   繁体   English

将包含十六进制和 ASCII 的字符串解码为十进制

[英]Decode string containing both hexidecimal and ASCII into decimal

I have a long string containing both hexadecimal and ASCII characters and need to convert the string into a list of individual decimal (integers) values.我有一个包含十六进制和 ASCII 字符的长字符串,需要将字符串转换为单个十进制(整数)值的列表。

All of the hexadecimal characters are contained within square brackets and I have figured out how to extract these and convert these into integers.所有的十六进制字符都包含在方括号内,我已经想出了如何提取这些并将它们转换为整数。 This is shown in the code below:这显示在下面的代码中:

# String from log file
log7 = ',[[0x8][0x1c][0x12][0x6][0xc][0x13][0xa6][0x1]a[0xc][0x9c][0x1][0xf4][0x0][0xa4][0x1][0xf7][0x1][0xa8][0x0],[0x4][0xfe][0x0][0x7][0x0][0x7][0x2][0xf0][0x7]L[0x2]u[0x0][0xa5][0x0][0xe3][0x0][0xb9][0x0]@[0x4][0xb][0x1][0x4][0x0][0x6][0x2][0x14][0x8]m[0x2]v[0x0][0xb3][0x0][0xbb][0x0][0xc5][0x0][0xa2][0xff][0x12][0x1][0x6][0x0]'

# Extract all text in square brackets and put into a list
extract = re.findall(r"\[([A-Za-z0-9_]+)\]", log7)

# Convert all hexidecimal values in list to decimal
convert = [int(x, 16) for x in extract]

# [8, 28, 18, 6, 12, 19, 166, 1, 12, 156, 1, 244, 0, 164, 1, 247, 1, 168, 0, 4, 254, 0, 7, 0, 7, 2, 240, 7, 2, 0, 165, 0, 227, 0, 185, 0, 4, 11, 1, 4, 0, 6, 2, 20, 8, 2, 0, 179, 0, 187, 0, 197, 0, 162, 255, 18, 1, 6, 0]

However, the above code does not include the ASCII characters and what I really need to do, is to convert the mix of hex and ASCII to decimals in the order they occur in the string.但是,上面的代码不包括 ASCII 字符,我真正需要做的是按照它们在字符串中出现的顺序将十六进制和 ASCII 的混合转换为十进制。

I know that I can convert the individual ASCII code to decimal using:我知道我可以使用以下方法将单个 ASCII 代码转换为十进制:

converted_ascii = [ord(x) for x in ascii_list]

The outcome I am need is, where bold are the missing ASCII characters - from the first lot of code above:我需要的结果是,粗体是缺少的 ASCII 字符 - 来自上面的第一批代码:

44 , 91 , 8, 28, 18, 6, 12, 19, 166, 1, 97 ,12, 156, 1, 244, 0, 164, 1, 247, 1, 168, 0, 44 , 4, 254, 0, 7, 0, 7, 2, 240, 7, 76 , 2, 117 , 0, 165, 0, 227, 0, 185, 0, 64 , 4, 11, 1, 4, 0, 6, 2, 20, 8, 109 , 2, 118 , 0, 179, 0, 187, 0, 197, 0, 162, 255, 18, 1, 6, 0 44,91,8,28,18,6,12,19,166,1,97,12,156,1,244,0,164,1,247,1,168,0,44,4,254, 0, 7, 0, 7, 2, 240, 7, 76 , 2, 117 , 0, 165, 0, 227, 0, 185, 0, 64 , 4, 11, 1, 4, 0, 6, 2, 20, 8, 109 , 2, 118 , 0, 179, 0, 187, 0, 197, 0, 162, 255, 18, 1, 6, 0

Please, can someone offer some advice on how to get through this next step?请问,有人可以就如何完成下一步提供一些建议吗?

I would do我会做

extract = re.findall(r"\[0x[0-9A-Za-z]+\]|.", log7)

convert = [ord(x) if len(x) == 1 else int(x[1:-1], 16) for x in extract]

Using re you can use the split function to get all those value as:使用re您可以使用 split 函数将所有这些值作为:

r = re.split('\[|\]',log7[2:])
values = []
for i in r:
    if i:
        try:
            values.append(int(i, 16)) # Tries to convert hex into integer
        except ValueError: # if failed it is an assci value
            values.append(ord(i)) # convert it into integer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM