2017-09-15 397 views
0

我將從UDP消息中提取/解壓浮動數並使用解壓縮。接收到的浮點值未正確解碼。 該收到的值的示例:Python從UDP消息中解壓十六進制浮點數

value_1; value_2; value_3; value_4; value_5; value_6; value_7; value_8; value_9 
113; 25; 99; -5,935507590692059e-21; 3,6735358238220215; 9,607040496462105e-09; 3,105335657942221e+16; 1,677943189259233e-31; -1,1667551647054353e-10; 158 
113; 25; 99; 1,6007581002917826e-13; -2,7373925907322235e+32; -5,90081314613487e+37; -1,1715147514317771e-33; -84894,453125; 1,6881092411861532e-15; 214 
113; 25; 99; 89819568,0; -7,121651712793664e-09; -1,0874276739486488e-12; 5,0595508846403285e+31; 7,212918281555176; -9,193915300799425e-38; 21 

的113,圖25和99是正確的,並且最後的值是校驗和也是正確的。

作爲下一個示例,浮點數應該更像0.0003234的值。

-1.5357945225e-004 -2.3622182564e-004 8.0991359160e-005 -1.4786999673e-003 2.0443745889e-003 4.9047302455e-002 
1.9647086933e-004 -2.3016422241e-004 3.8992474000e-004 -1.4826891711e-003 2.0534156356e-003 4.9040760845e-002 
1.3971551841e-004 -6.4107143726e-005 3.6151042455e-004 -1.4849692816e-003 2.0616210531e-003 4.9035679549e-002 

下面是從wireshark捕獲的收到的UDP消息的圖片。 enter image description here

如何提取此數據中正確的浮點數?

這裏是我的代碼的必要:

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 
sock.bind((UDP_IP,UDP_PORT)) 
f.write("'value_1', value_2, value_3, value_4, value_5, value_6, value_7, value_8, value_9" '\n') 

while i < NumberOfSamples: 
    data, addr = sock.recvfrom(4096) # buffer size is 1024 bytes 
    print >>f, struct.unpack("<BBBffffffB", data) 

for line in f: 
    f2.write(line.replace(',',';').replace('.',',').replace('(',' ').replace(')',' ')) 
+0

那麼,什麼是你的預期輸出'打印 「{:.20f}」。格式(struct.unpack( 「> F」, 「\ xb6 \ xd5 \ x38 \ x53」)[0])'?我得到了'-0.00000635445167063153' –

回答

1

你可以嘗試分裂拆包成兩個部分,例如:

import struct 

data = "\x71\x19\x63\xb6\xd5\x38\x53\x36\x81\x4c\x59\xb6\xa0\x93\xac\xba\xa5\x9a\x76\x3a\x3f\xcb\x8f\x3d\x48\xfd\xab\x69" 

print struct.unpack("4B", data[:3] + data[-1]) 
print struct.unpack(">6f", data[3:-1]) 

這將顯示:

(113, 25, 99, 105) 
(-6.3544516706315335e-06, 3.853387624985771e-06, -4.785562850884162e-06, -0.0012634533923119307, 0.0007316404371522367, 0.04907004162669182) 

你也可以利用Python的CSV庫來幫助寫作:

import struct 
import csv 

data = "\x71\x19\x63\xb6\xd5\x38\x53\x36\x81\x4c\x59\xb6\xa0\x93\xac\xba\xa5\x9a\x76\x3a\x3f\xcb\x8f\x3d\x48\xfd\xab\x69" 

with open("output.csv", 'wb') as f_output: 
    csv_output = csv.writer(f_output, delimiter=';') 
    csv_output.writerow(["value_{}".format(v) for v in range(1, 10)]) 
    csv_output.writerow(struct.unpack(">3B6fB", data)) 

這將給你一個output.csv文件,如下所示:

value_1;value_2;value_3;value_4;value_5;value_6;value_7;value_8;value_9 
113;25;99;-6.3544516706315335e-06;3.853387624985771e-06;-4.785562850884162e-06;-0.0012634533923119307;0.0007316404371522367;0.04907004162669182;105 
+0

謝謝,那是我的解決方案。 –