arrays - Python convert string repr of byte data into float data -
i have 12 bytes being sent on web socket used represent 3 float values. other program this:
float m_floatarray[3]; ... serial.write((byte*) m_floatarray, 12); //12 b/c 3 floats @ 4 bytes each the data receive in python program looks this:
#the data printed from: received = ser.read(12) received = '\xe4c\x82\xbd-\xe4-\xbe&\x00\x12\xc0' i want in python program:
x = getfirstfloat(received) y = getsecondfloat(received) z = getthirdfloat(received) how can parse data? thank you.
a short example:
>>> a,b,c = [random.random()*10 in range(3)] >>> 0.9446191909332258 >>> b 7.578277797297625 >>> c 8.061585451293366 >>> recieved = struct.pack('fff', a, b, c) >>> recieved '\x90\xd2q?@\x81\xf2@a\xfc\x00a' >>> x,y,z = struct.unpack('fff', recieved) >>> x 0.9446191787719727 >>> 0.9446191909332258 >>> b 7.578277797297625 >>> y 7.578277587890625 >>> c 8.061585451293366 >>> z 8.061585426330566 >>> note may want specify byte order in format string
Comments
Post a Comment