I have a list of string elements and I want to reverse the endianess of the elements and not just reverse them. So far I have tried this
import re
test_list = '81dfe9803e26e1808032da803fcd3940'
replaced = re.sub(r'((\w){8})', r'\1\n', test_list)
splitted_list = replaced.splitlines()
//print(splitted_list)
//['81dfe980', '3e26e180', '8032da80', '3fcd3940']
reversed_list = [i[::-1] for i in splitted_list]
//print(reversed_list)
//['089efd18', '081e62e3', '08ad2308', '0493dcf3']
I want the reversed_list output to have the endianess(reversing every 2 characters) changed and not exactly reversed like
[’80e9df81′, ’80e1263e’, ’80da3280′, ‘4039cd3f’]
>Solution :
try this and let me know if it worked for you.
reversed_list = ['089efd18', '081e62e3', '08ad2308', '0493dcf3']
def reverse_endianness(hex_str):
return ''.join(hex_str[i:i+2][::-1] for i in range(0, len(hex_str), 2))
reversed_endianness_list = [reverse_endianness(elem) for elem in reversed_list]
print(reversed_endianness_list)