如果我有一些 1 位长如 0x1 和一些 5 位长如 0x1e4b1 的十六进制数,我该如何编写代码使它们都是 8 位长?

If I have hexadecimal numbers with some 1 digit long like 0x1 and some 5 digits long like 0x1e4b1, how do I write code to make them all 8 digits long?

我在 .txt 文件中有一些这样的十六进制数:0x1、0x2、0x1e4b1、0x5b、0x80、0x52、0x111、0x6b0d、0x4e、0x34a、0x2067、0x6ef3、0x1cf、0x1b、0x15b、0x4 0xba8、0x319。 What I am trying to do now is overwrite the contents (using code) of the file and make the end result like this: 0x00000001, 0x00000002, 0x0001e4b1, 0x0000005b, 0x00000080, 0x00000052, 0x00000111, 0x00006b0d, 0x0000004e, 0x0000034a, 0x00002067, 0x00006ef3, 0x000001cf、0x0000001b、0x0000015b、0x0000004f、0x00000ba8、0x00000319。

这里是一些必要的背景信息:我有一个 .txt 文件,上面有一些数字,它们都用逗号分隔。然后,我使用 Python 代码打开并阅读。在那之后,我列出了文件中的所有数字。当我制作一个列表时,数字都是字符串(例如:'9'、'8' 等),所以我使用一些 Python 代码将列表的值转换为整数。这样做之后,我将所有整数转换为十六进制形式。然后,我采用十六进制形式的整数并将它们放入一个名为 Hexadecimal Numbers 的新 .text 文件中。现在,我要做的是覆盖十六进制数文件,以便将其替换为用零填充的十六进制数,使它们全部为 8 位数字。

我曾尝试在 Google 上搜索此内容,但找不到与此相关的内容。请帮忙!谢谢!如果你还是不明白我的问题,记得评论问我。

输出前的图片:

预期输出:

到目前为止,这是我的代码:

my_file = open(r'C:\Users\KAPS\Downloads\List of Numbers File.txt', encoding='utf-8-sig') content = my_file.read() print(content)

content_list = content.split(",") my_file.close() print(content_list)

for i in range(0, len(content_list)):
    content_list[i] = int(content_list[i]) print(str(content_list))

hex_list = [hex(int(x)) for x in content_list] print(hex_list)

with open(r'C:\Users\KAPS\Downloads\Hexadecimal Numbers.txt', 'w') as my_file:
    my_file.write(', '.join(hex_list))

padded = '0x' + '0' * (10 - len(mystring)) + mystring[2:]

我们得到 hex-number 字符串,然后我们:

  1. 切掉前缀“0x”
  2. 8-(len(s)-2) 零填充,因为我们必须从字符串的长度中减去 2,因为它们是 '0x' 前缀的一部分
  3. 添加实际的 hex-number


简单方法:

def pad_hex_strings(s):
    # Add the '0x' prefix of a hex-number
    padded = '0x'
    # Add the zeroes padding
    padded += '0' * (8-(len(s)-2))
    # Adding the actual hex-number, going from index 2 till the end of the string
    for i in range(2, len(s)):
        padded += s[i]
    return padded


使用提供的代码:

def pad_hex_strings(s):
    padded = '0x'
    padded += '0' * (8-(len(s)-2))
    # Adding the actual hex-number
    for i in range(2, len(s)):
        padded += s[i]
    return padded

my_file = open(r'C:\Users\KAPS\Downloads\List of Numbers File.txt', encoding='utf-8-sig') content = my_file.read() print(content)

content_list = content.split(",") my_file.close() print(content_list)

padded_strings = [pad_hex_strings(s) for s in content_list]

with open(r'C:\Users\KAPS\Downloads\Hexadecimal Numbers.txt', 'w') as my_file:
    my_file.write(', '.join(padded_strings))


作为字符串列表,就像我们从文件中读取的那样:
list_strings = ['0x1', '0x2', '0x1e4b1', '0x5b', '0x80', '0x52', '0x111', '0x6b0d', '0x4e', '0x34a', '0x2067', '0x6ef3', '0x1cf', '0x1b', '0x15b', '0x4f', '0xba8']

def pad_hex_strings(s, total_length=8, prefix_length=2):
    # Slicing prefix of '0x', padding with zeroes, and writing the rest of the string
    return s[:prefix_length] + (total_length-(len(s)-prefix_length)) * '0' + s[prefix_length:]

# Apply padding on each item of the list
padded_strings = [pad_hex_strings(s) for s in list_strings]
print(f'Padded Strings:\n{padded_strings}')

# Write to output file
with open("Hexadecimal Numbers.txt", "w") as text_file:
    text_file.write(', '.join(padded_strings))



如果是数字:
def pad_hex_numbers(n, total_length=8):
    prefix_length = 2 # For '0x'
    # Formatting the hex number to be padded with zeroes
    return '{0:#0{1}x}'.format(n, total_length+prefix_length)

list_numbers = [ 0x1, 0x2, 0x1e4b1, 0x5b, 0x80, 0x52, 0x111, 0x6b0d, 0x4e, 0x34a, 0x2067, 0x6ef3, 0x1cf, 0x1b, 0x15b, 0x4f, 0xba8 ]

# Apply padding on each item of the list
padded_numbers = [pad_hex_numbers(n) for n in list_numbers]
print(f'Padded Numbers:\n{padded_numbers}')

# Write to output file
with open("Hexadecimal Numbers.txt", "w") as text_file:
    text_file.write(', '.join(padded_numbers))

我是 python 的新手,所以这可能行不通,但您可以尝试使用

len(十六进制(x))

找到十六进制的长度,然后对十六进制的每个长度使用 if 语句,将其转换为字符串,然后在末尾添加 0?然后您可以将其转换回带有添加的零的十六进制。让我知道它是否有效!