0

I have a file with text and binary. Text is used to identify beginning and end of a chunk of binary data, which represents a list of complex numbers, e.g. (1.5+5.6j)(...).

I have implemented the following to detect where the data starts and ends but I am not sure how to parse the data to extract the complex numbers.

Input file 'short.log':

NOW: ^@ºÿ¦ÿÉ^B<80>^AÓ^@k^C^HüÀ^Búú<88>^@^Uü4ÿ^[ÿ<88>^@»^@}^A^@N^Auÿi^Bòü5^E¿ùï^DHûÍÿö^CÈü^C^KÌ^@¡^H^D^GÿþP^E¡ö]ý<95>öñ÷Ùûøú^@j^@ END

And this is the code to read it:

from bitstring import ConstBitStream
import os
import struct

bin_file = 'short.log'
byte_data = b'NOW: '

def parse(byte_data):
    fileSizeBytes = os.path.getsize(bin_file)
    data = open(bin_file, 'rb')

    s = ConstBitStream(filename=bin_file)
    occurances = s.findall(byte_data, bytealigned=True)
    occurances = list(occurances)
    totalOccurances = len(occurances)
    byteOffset = 0                                                          
    occurances2 = s.findall(b' END', bytealigned=True)
    occurances2 = list(occurances2)
    totalOccurances2 = len(occurances2)
    byteOffset = 0   

    for i in range(0, len(occurances)):
        occuranceOffset = (hex(int(occurances[i]/8)))  
        s.bitpos = occurances[i]
        data = s.readto(b' END')

        parsedata(data)

def parsedata(data):
    #Here to parse complex number

parse(byte_data)

1 Answer 1

0

It depends on how the complex numbers have been encoded in the binary data. For example it might be as floats with 32 bits for the real part, then 32 for the imaginary. If so then s.readlist('2*float:32') would read and interpret the real and imaginary parts.

Or it could be 64 bits each, or a different format altogether. If you have an example where you know what the result should be then experiment with a few formats and see what works.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.