2017-08-29 6 views
1

J'ai une charge utile hexagonale:hex Décodage pour coordonner

872fa5596122f23e24efb4fc1013b7000000000718 

Le lat et lng est peu endian et aux endroits suivants:

lng - binary[20:28] 
lat - binary[28:32] 

Je ne suis pas sûr de savoir comment obtenir le bon résultats. Je pensais d'abord que je devrais changer l'hexagone en petit boutiste? ? Puis le convertir en int J'ai essayé ceci:

data = struct.unpack('<ll',binary[12:20]) 

sortie:

TypeError: a bytes-like object is required, not 'str' 
+1

Essayez d'encoder le binaire en premier: 'struct.unpack (' L3viathan

+0

Ah, je ddint réaliser, fonctionne avec python 2.7 – Harry

+0

Suis-je dans la bonne logique? D'abord déballer puis binaire? – Harry

Répondre

0

Vous devez d'abord convertir la représentation hexdecimal en octets objet:

import codecs 

binary = "872fa5596122f23e24efb4fc1013b7000000000718" 
binary_bytes = codecs.decode(binary, 'hex') 
print(binary_bytes) 
# b'\x87/\xa5Ya"\xf2>$... 

Ensuite vous pouvez utiliser struct pour décoder en entiers ou autre:

import struct 
# Guessed the offsets... 
lng, lat = struct.unpack('<ll', binary_bytes[0:4] + binary_bytes[20:24]) 
print((lng, lat)) 
# (15003997831, 1056055905) 
+0

[bytes.fromhex] (https://docs.python.org/3/library/stdtypes.html#bytes.fromhex) –