0

I'm recieving data using an OSC server and the data looks like this:

b'Person0/elements/alpha_absolute\x00,dddd\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00?\xbbP\x128\xe6/\xd4\x00\x00\x00\x00\x00\x00\x00\x00'
b'Person0/elements/alpha_absolute\x00,dddd\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00?\xbbOw\x8f\xa7\xac\x10\x00\x00\x00\x00\x00\x00\x00\x00'
b'Person0/elements/alpha_absolute\x00,dddd\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00?\xbb\x10\x1f\xf2JN\xed\x00\x00\x00\x00\x00\x00\x00\x00'
b'Person0/elements/alpha_absolute\x00,dddd\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00?\xbah[\nY\xe9K\x00\x00\x00\x00\x00\x00\x00\x00'
b'Person0/elements/alpha_absolute\x00,dddd\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00?\xb8\x8f\x97\xb1\x04\xc4B\x00\x00\x00\x00\x00\x00\x00\x00'

The data title is: Person0/elements/alpha_absolute and the arguments is: dddd (4 floats)

The rest seems to be encoded. I don't understand how to get the 4 floats I'm supposed to recieve

This is my whole code:

import socket

print('Program Initiated')
UDP_IP = "127.0.0.1"
UDP_PORT = 6000

sock = socket.socket(socket.AF_INET,  # Internet
                 socket.SOCK_DGRAM)  # UDP
sock.bind((UDP_IP, UDP_PORT))

while True:
    data, addr = sock.recvfrom(2048)  # buffer size is 1024 bytes

if 'alpha' in str(data):
    print(data)
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
ZenSolo
  • 13
  • 4
  • 1
    With great difficulty (in reference to your title). You can try `chardet` but it's still a guess. – roganjosh Dec 08 '18 at 22:50
  • Welcome to SO ! Maybe take a look at this question https://stackoverflow.com/questions/436220/determine-the-encoding-of-text-in-python – Acemad Dec 08 '18 at 22:50
  • 3
    This has nothing to do with *text encoding*. Your floats are stored in binary, not as any kind of text. You can use `struct.unpack` – but you must know first what to use it on. – Jongware Dec 08 '18 at 22:54
  • 1
    Concurring with @usr2564301. Who sends these data? Why don't you check what is the expected data format? If it is indeed a binary representation of double numbers, you should use `struct.unpack`. – DYZ Dec 08 '18 at 22:59
  • What are the actual bytes represented by `dddd`? How many are there? – Mark Ransom Dec 10 '18 at 02:02
  • The OSC format is defined [here](http://opensoundcontrol.org/spec-1_0) – snakecharmerb Mar 02 '19 at 07:15

1 Answers1

0

Use this:

title,args,flt1,flt2,flt3,flt4 = struct.unpack('>32s8sdddd', data)

Since you don't know anything about the structure, this is based on the following guesses:

  1. The title field is a string of maximally 32 bytes long because it is a text string and the 32rd byte is always a 0.
  2. The args field is a string of maximally 8 bytes long because that's what is left when you assume …
  3. each of the four "floats" are 8 bytes long and thus, technically, a double float. The last 8 bytes are always all 0. That is a valid double number (0, actually); the 8 bytes before them are all valid floats in big-endian format, and that makes 2 valid floats. So, counting backwards, 16 more zeroes must be the other 2 values, and the few bytes left must belong to the args field.

Running the unpack on the provided data gives you this result:

b'Person0/elements/alpha_absolute\x00' b',dddd\x00\x00\x00' 0.0 0.0 0.1066905392564757 0.0
b'Person0/elements/alpha_absolute\x00' b',dddd\x00\x00\x00' 0.0 0.0 0.10668132073594472 0.0
b'Person0/elements/alpha_absolute\x00' b',dddd\x00\x00\x00' 0.0 0.0 0.10571479478158681 0.0
b'Person0/elements/alpha_absolute\x00' b',dddd\x00\x00\x00' 0.0 0.0 0.10315484049525485 0.0
b'Person0/elements/alpha_absolute\x00' b',dddd\x00\x00\x00' 0.0 0.0 0.09594104835265774 0.0

where about the only thing remarkable is that from the four float items, only one seems actually used.

Jongware
  • 22,200
  • 8
  • 54
  • 100