Python Forum

Full Version: Type consistency cross-application via UDP
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
When sending data from Max/MSP to Python via UDP all I wind up receiving on the other side is stuff that looks like this:

b'float\x00\x00\x00,f\x00\x00\xc1\x1f\xc2\x8f'

What format is this and how do I convert it back to a float? What I really need to do is send a list of floats such as

3 -8.22241 -7.066427 -5.288864 -2.530465 0.458666 2.289094 2.566208 1.953798 1.114607 0.296125 -0.339662 -0.604555 -0.518344 -0.328184 -0.239883 -0.265401 -0.312797 -0.300493 -0.189546

but that winds up as

b'list\x00\x00\x00\x00,ffffffffffffffffffffffff\x00\x00\x00\xc2\x80\xed%A\xc5\x10\xabA \xddT\xc0\x1c\x89\x1a\xc0\xfb^\xf8\xc1\x03\x8e\xfe\x
c0\xe2 ,\xc0\xa9>_\xc0!\xf3%>\xea\xd6B@\x12\x80\x86@$<\xc1?\xfa\x16\x0f?\x8e\xabt>\x97\x9d\xc1\xbe\xad\xe8=\xbf\x1a\xc4\x1e\xbf\x04\xb2*\xb
e\xa8\x07\xc3\xbeu\xa3\xcb\xbe\x87\xe2\x96\xbe\xa0&\xe8\xbe\x99\xda.\xbeB\x18^'

How does one decode that?

I've discovered that I'm dealing with raw bytes here. However, if I try to use decode() I get an error like this one

File "C:\Users\tothesun\AppData\Local\Programs\Python\Python35\lib\encodings\utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xed in position 38: invalid continuation byte
not 100% sure, but take a look at   xdrlib module or struct module
it's difficult to tell more given we don't know how the original data look like and how they were packed

>>> import xdrlib
>>> x = b'float\x00\x00\x00,f\x00\x00\xc1\x1f\xc2\x8f'
>>> unpkr = xdrlib.Unpacker(x)
>>> unpkr.unpack_float()
2.7913326703400993e+23
>>> unpkr.unpack_double()
5.727781731053143e+250
>>> import struct
>>> struct.unpack('4f',x)
(2.7603642276376897e+20, 1.6255062186167878e-43, 3.6652362632879915e-41, -1.9142108111389164e-29)
Interesting. Those methods unpack something, but it's not the something I sent. The 1st one always returns the same number no matter what float I'm sending from Max. The 2nd one returns a list of 4 numbers. Only the 4th changes as I send different floats from Max, but it never corresponds to the number I'm sending.

There's no special format or packing as I send from Max. Just individual floats going into [udpsend]. Strings and lists of floats also wind up as raw bytes.
Given that you are sending data from other program I was suspecting you are dealing is C structs represented as Python bytes objects, but maybe I'm wrong
can you provide input->output pair?
also some links to the software you are using
MaxMSP is a pretty complex structure.
In order to get a better read on what you are sending and receiving, is it possible to do the following?

Using a program like Hxd: https://mh-nexus.de/en/hxd/ look at first 100 bytes of file on both computers
Select the first 100 bytes (highlight and paste to text file).
do this for 8, 16, 32 and 64 bit ANSI words (easy to change with Hxd)

This will allow for a comparison of the data on both computers.

There is a possibility of an endian problem
I discovered that the object I was using ([udpsend]) uses the OSC protocol and that's what was responsible for all the excess encryption. I switched to [MXJ net.udp.send] and the numbers came through cleanly, except for being surrounded by apostrophes with a b in front. This apparently indicates that it's in bytes and can be removed with bytes.decode().