2017-08-29 67 views
1

我有一個十六進制有效載荷:解碼十六進制協調

872fa5596122f23e24efb4fc1013b7000000000718 

的緯度和經度是在小尾數和在以下位置:

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

林不知道如何得到正確的結果。我以爲首先我必須將十六進制改爲小端? ?然後將其轉換爲int我嘗試這樣做:

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

輸出:

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

嘗試先編碼二進制:'struct.unpack(' L3viathan

+0

啊,我ddint意識到,使用python 2.7 – Harry

+0

我遵循正確的邏輯嗎?首先解壓縮到二進制文件? – Harry

回答

0

首先,您需要的hexdecimal表示轉換成一個字節對象:

import codecs 

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

然後你可以使用struct解碼爲整數或任何:

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) –