[Tutor] binary string parsing

Robert Clement robert.clement at ed.ac.uk
Tue Mar 8 11:25:32 CET 2011


I am receiving a string over a socket connection.  The string may be 
either line and value delimited ascii, or formated binary.  The skeleton 
of the code which handles the data is:

             buffer = socket.recv(1000)
             lines = buffer.split(linedelim)
              for line in lines:
                         if datatype  == 'binary':
                             if len(line) == binaryrecordsize:
                                 values = unpack(formatin,line)
                             values = line.split(signaldelim)

My problem arises when handling data which may be either big or little 
endian.  The endianess is handled properly when unpacking each line of 
data from the buffer but I was not sure how to handle the process if 
splitting the buffer into individual lines using the line delimiter.

The problem was first encountered when unpacking a formated binary 
buffer which had \xBA\xBA as the line delimiter.  The above code worked 
fine most of the time but occasionally an \xBA would crop up as the last 
byte of the line becasue the byte order was different than the default 
assumed for the buffer.


Robert Clement

202 Crew Building
West Mains Road
UK,  EH9 3JN

phone +44 131 650 7732

The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.

More information about the Tutor mailing list