Python Forum

Full Version: Python crypto byte plaintext to hexint
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"

This is a 32 byte plaintext from a file where each byte are encoded and represents 2 hexadecimals and i want to convert it to hexadecimal integers like this: 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31

i have been struggling for a while with this and given hits about using functions: hex(), open(), file.read(), string.decode(), map(), ord()

This is how for i gotten and would appreciate any help

key = open('testKey')
getKey = key.read()

print(getKey)
integerKey = int(getKey,16)
hexKey = hex(integerKey)
for i in hexKey:
    print(ord(i), end=" , ")
# test = map(integerKey, hexKey)
print(getKey)
print(integerKey)
# print(list(test))
I believe you're overcomplicating things a bit:
with open("testKey") as f:
    while chars := f.read(2):
        print(int(chars, 16))
I use a context manager (you can Google this term if you're unfamiliar with it) for the file management, which is a recommended best-practice. I use the "walrus operator" (Googleable as well). And lastly, I use the fact that f.read(2 will return an empty string, which will terminate the while loop if the file is empty.
Thanks for the help i really am grateful