1

I have an array of strings like the following in the little endian format how can I change each line to big endian format:

e28f6001
e12fff16
220c
4679

I want an output like the following:

01608fe2
16ff2fe1
0c22
7946
..
  • 1
    Possible duplicate of [Reverse a string in Python](https://stackoverflow.com/questions/931092/reverse-a-string-in-python) – Kallz Jul 22 '17 at 12:24
  • 1
    Possible duplicate of [byte reverse AB CD to CD AB with python](https://stackoverflow.com/questions/14543065/byte-reverse-ab-cd-to-cd-ab-with-python) – 101 Jul 22 '17 at 12:29

2 Answers2

0

You could use a small regex (..) to reverse the hex bytes:

>>> import re
>>> ''.join(re.findall('..', 'e28f6001')[::-1])
'01608fe2'

You can use a function and a list comprehension in order to apply this transformation to a list:

import re

def swap_endian(hexa_string):
    return ''.join(re.findall('..', hexa_string)[::-1])

strings = ['e28f6001', 'e12fff16', '220c', '4679']

print([swap_endian(hexa) for hexa in strings])
# ['01608fe2', '16ff2fe1', '0c22', '7946']
Eric Duminil
  • 52,989
  • 9
  • 71
  • 124
0

Here is the simple approach used for the solution.

a=raw_input()
str1=""
for i in range(len(a)-1, -1, -2):
    if i-1==-1:
        str1 += str(a[i])
        break
    str1+=str(a[i-1])
    str1+=str(a[i])
print str1
Rohit-Pandey
  • 2,039
  • 17
  • 24