Python Forum
Python crypto byte plaintext to hexint - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Python crypto byte plaintext to hexint (/thread-24595.html)



Python crypto byte plaintext to hexint - Mangaz90 - Feb-21-2020

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



RE: Python crypto byte plaintext to hexint - micseydel - Feb-21-2020

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.


RE: Python crypto byte plaintext to hexint - Mangaz90 - Feb-21-2020

Thanks for the help i really am grateful