# 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?

0

I have some hexadecimal numbers like this in a .txt file: 0x1, 0x2, 0x1e4b1, 0x5b, 0x80, 0x52, 0x111, 0x6b0d, 0x4e, 0x34a, 0x2067, 0x6ef3, 0x1cf, 0x1b, 0x15b, 0x4f, 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.

Here is some necessary background info: I have a .txt file with some numbers on it that are all separated by commas. Then, using Python code, I opened it and read it. After that, I made a list out of all of the numbers that were on the file. When I made a list, the numbers were all strings (example: '9', '8', etc.), so I used some Python code to convert the values of the list into integers. After doing that, I converted all of the integers into a hexadecimal form. Then, I took the integers in hexadecimal form and put them into a new .text file called Hexadecimal Numbers. Now, what I am trying to do is overwrite the Hexadecimal Numbers file in order to replace it with the hexadecimal numbers padded with zeros to have them all 8 digits.

I have tried to search this on Google, but couldn't find something specific to this. Please help! Thank you! If you still don't understand my question, make sure to comment and ask me.

Picture before Output:

Expected Output:

Here is my code so far:

``````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)

my_file.write(', '.join(hex_list))

padded = '0x' + '0' * (10 - len(mystring)) + mystring[2:]
``````
python
hex
asked on Stack Overflow Aug 23, 2020 by Kapur • edited Aug 23, 2020 by Kapur

1

We get the hex-number string, then we:

1. Slice the prefix '0x'
2. Pad with `8-(len(s)-2)` zeroes, as we have to subtract 2 from the length of the string because they are part of the '0x' prefix

Simple method:

``````def pad_hex_strings(s):
# Add the '0x' prefix of a hex-number
# Adding the actual hex-number, going from index 2 till the end of the string
for i in range(2, len(s)):
``````

Using provided code:

``````def pad_hex_strings(s):
for i in range(2, len(s)):

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

``````

As a list of string, as we read from a file:
``````list_strings = ['0x1', '0x2', '0x1e4b1', '0x5b', '0x80', '0x52', '0x111', '0x6b0d', '0x4e', '0x34a', '0x2067', '0x6ef3', '0x1cf', '0x1b', '0x15b', '0x4f', '0xba8']

# 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

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

``````

If it were numbers:
``````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

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