2017-07-22 3 views
1

j'ai un tableau de chaînes comme suit dans le petit format endian comment puis-je changer chaque ligne grand format endian:Inverser un tableau de la chaîne

e28f6001 
e12fff16 
220c 
4679 

Je veux une sortie comme ce qui suit:

01608fe2 
16ff2fe1 
0c22 
7946 
.. 
+1

double possible de [inverser une chaîne en Python] (https://stackoverflow.com/questions/931092/reverse-a-string-in-python) – Kallz

+1

double possible de [octet inverse AB CD à CD AB avec python] (https://stackoverflow.com/questions/14543065/byte-reverse-ab-cd-to-cd-ab-with-python) – 101

Répondre

0

Vous pouvez utiliser une petite regex (..) pour inverser les octets hexagonaux:

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

Vous pouvez utiliser une fonction et une compréhension de la liste afin d'appliquer cette transformation à une liste:

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

Voici l'approche simple utilisée pour la 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