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
..
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
..
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']
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