prob with struct and byte order

Grant Edwards grante at visi.com
Mon Jul 24 15:30:50 EDT 2006


On 2006-07-24, nephish at xit.net <nephish at xit.net> wrote:
> ok, i did this print ' '.join(["%02.2x" % ord(b) for b in message])
> and i got this in the text file

> 5354580000002c000000ea3137353834363638353500000000000000000000000000000000000000000000d6090d5400000000454e58

No, I don't think so. If you did what you said you did, you
would have gotten something with spaces in it:

>>> ' '.join(["%02.2x" % ord(b) for b in "hi there, how are you"])
'68 69 20 74 68 65 72 65 2c 20 68 6f 77 20 61 72 65 20 79 6f 75'

It's very important that you cut-paste things into postings
exactly as they appear in your program and output.  We can
sometimes guess what you did, but not always.  In this case,
I'm guessing you did ''.join rather than ' '.join.

> so, yes, more of the info seems discernable now.
>
> according to their docs, all of their variables are sent as 32bit long
> int (prefferably unsigned long int).

Then you can use the struct module to pull those values out of
the message:

  def tohex(bytes):
    return ' '.join(['%02.2x' % ord(b) for b in bytes])

  startDelimiter,msglen = struct.unpack('>3sI', message[:7])
  endDelimiter = struc.unpack('>3s',message[-3:])
  assert startDelimiter == 'STX'
  assert endDelimiter == 'ENX'
  payload = message[7:-3]  
  assert msglen == len(payload)

  while len(payload) >= 4:
    print struct.unpack('>I',payload[:4])
    payload = payload[4:]

  if payload:
    print "extra bytes", tohex(payload)  
  
... or whatever.  You'll probably want to pull the message type
out first, and then look up a format string using the message
type as the key.
    
NB:  The above code is off-the-cuff and untested.  It almost
certainly contains typos and maybe even a thinko.

> the big deal in the docs goes on about how the clients need the byte
> order to match

That's what the '>' at the beginning of the struct format
string does.  Your message appears to be big-endian, so you use
the '>' specifier to tell the struct module to interprent the
data as big-endian.

> that of the arch that the server runs on 'Sun UltraSPARC'

Which is indeed big-endian.

> i think they run Solaris.

Which doesn't actually matter -- you'd see the same thing if
they were running Linux, BSD, or some other OS.

-- 
Grant Edwards                   grante             Yow!  Are we live or
                                  at               on tape?
                               visi.com            



More information about the Python-list mailing list