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.
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)
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:]