简体   繁体   中英

How do I decode data when I don't know the encoding

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM