Search code examples
pythonencryptionhexkey

Hexadecimal number stored in str format is stored back in hexadecimal format


I tried to create a list (the key I am trying to create) with 32 hexadecimal numbers in the function below. The result is:

def make_key(str : str) : 
    base = []
    res = []
    for i in str :
        base.append(i)
    for i in base :
        res.append(hex(ord(i)))
    return res

key = make_key('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')
print(key) 

result is

['0x61', '0x61', '0x61', '0x61', '0x61', '0x61', '0x61', '0x61', '0x61', '0x61', '0x61', '0x61', '0x61', '0x61', '0x61', '0x61', '0x61', '0x61', '0x61', '0x61', '0x61', '0x61', '0x61', '0x61', '0x61', '0x61', '0x61', '0x61', '0x61', '0x61', '0x61', '0x61']

Is there a way to convert this string formatted hexadecimal to hexadecimal format only?

To sum it up:

I've tried things like:

def make_key(str : str) : 
    base = []
    res = []
    for i in str :
        base.append(i)
    for i in base :
        res.append(hex(ord(i)))
    return res

key = make_key('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')
print(key) 

I hope the result is something like this:

[0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,0x61, 0x61, 0x61, 0x61]

Solution

  • Given a list of integers, printing that list will show the values (unsurprisingly) as base 10 integers.

    If this is just a formatting exercise then:

    def make_key(s):
        return [hex(ord(c)) for c in s]
    
    print('[' + ', '.join(make_key('aaaa')) + ']')
    

    What's happening here is that we construct a string to look like a Python list which gives this output:

    [0x61, 0x61, 0x61, 0x61]