Decoder une entete SAP (RFC 2974)
Alex Martelli
aleax at aleax.it
Wed May 14 04:52:43 EDT 2003
<posted & mailed>
Benoit BESSE wrote:
...
> Hello, I would like to decode heading SAP of a message which I receive on
> port 9875. Information are
>
>> 0 1 2 3
>> 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
>> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
>> | V=1 |A|R|T|E|C| auth len | msg id hash |
>> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
>> | |
>> : originating source (32 or 128 bits) :
...
> My problem is to decode the version and information which follows. Is
> there someone to propose a solution because I maitrise not well the unpack
struct.unpack is suitable to take some binary bytes and make them into
Python values. However, it does not deal with single bits within a byte,
which is what you must be dealing with here. For that, you use bit-level
operations of 'masking' (&) and shifting (<< or >>).
I'm not sure if this schema is to be read as 'big-endian' (the 3 bits
of V are the top bits in the first byte) or 'little-endian' (they are
the bottom bits). In the first case, you could transform the first
byte (seen as a Python value 0-255) into the V-A-R-T-E-C values as
follows, for example (warning, untested code):
def byte_to_VARTEC(byte):
vartec = []
for i in range(5):
vartec.append(byte & 1)
byte >>= 1
vartec.append(byte & 7)
vartec.reverse()
return vartec
If you have the byte you need as the first byte in a plain string
object s, you can get the equivalent 0-255 value as ord(s[0]).
If the bits are the other way around (V takes the 3 least significant
ones, then A, and so on), then (again untested):
def byte_to_VARTEC(byte):
vartec = [byte & 7]
byte >>= 3
for i in range(5):
vartec.append(byte & 1)
byte >>= 1
return vartec
If you don't know either, get an actual example or two, try decoding
them in each way, and see which one makes sense;-).
Alex
More information about the Python-list
mailing list