Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python: Split a str list into elements of size 8 (4 bytes) and reverse every byte of each element in the list (endianess)

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading