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: enter image description here

Expected Output: enter image description here

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:]
python
hex
asked on Stack Overflow Aug 23, 2020 by Kapur • edited Aug 23, 2020 by Kapur

2 Answers

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
  3. Add the actual hex-number


Simple method:

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


Using provided code:

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


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']

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



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
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))
answered on Stack Overflow Aug 23, 2020 by Aviv Yaniv • edited Aug 23, 2020 by Aviv Yaniv
0

I'm new to python, so this might not work, but you could try to use the

len(hex(x))

to find the length of the hexadecimal, then use a if statement for each length of hexadecimals where you would convert it to strings and then add the 0s at the end of it? Then you could convert it back to a hexadecimal with the added zeros. let me know if it works!

answered on Stack Overflow Aug 23, 2020 by Tyler Schroeder

User contributions licensed under CC BY-SA 3.0