Trees | Indices | Help |
---|
|
AIS binary helper functions.
Code to convert AIS messages between binary BitVectors and strings. They are usually encoded an ASCII 6-bit packing within NMEA !AIVDM/!AIVDO messages.Author: Kurt Schwehr
Version: 2075
Copyright: 2006
To Do:Date: 2006-05-03
|
|||
BitVector |
|
||
float |
|
||
|
|||
BitVector |
|
||
BitVector |
|
||
BitVector |
|
||
BitVector |
|
||
int |
|
||
BitVector(6) |
|
||
BitVector |
|
||
|
|||
str, pad |
|
|
|||
decode =
cache of character to BitVector lookup |
|||
encode =
cache of ais int value to charcter |
|
>>> print float2bitvec(1.) 00111111100000000000000000000000 >>> print float2bitvec (-1.) 10111111100000000000000000000000 >>> print float2bitvec (-999999.) 11001001011101000010001111110000
|
Bug: May have bite order backwards See Also: struct module |
Bug: replace with a faster algorithm! |
To Do: What to do if the vector is larger than size? |
>>> print addone(BitVector(bitstring='1100')) 1101 >>> print addone(BitVector(bitstring='1111')) 0000
|
>>> print subone(BitVector(bitstring='1111')) 1110 >>> print subone(BitVector(bitstring='0010')) 0001 >>> print subone(BitVector(bitstring='0000')) 1111
|
Create a twos complement BitVector from a signed integer. Positives must have a '0' in the left hand position.>>> print bvFromSignedInt(0,bitSize=4) 0000 >>> print bvFromSignedInt(1,bitSize=4) 0001 >>> print bvFromSignedInt(7,bitSize=4) 0111Negative numbers must have a '1' in the left hand position. >>> print bvFromSignedInt(-1,bitSize=4) 1111 >>> print bvFromSignedInt(-2,bitSize=4) 1110 >>> print bvFromSignedInt(-7,bitSize=4) 1001
|
Interpret a bit vector as an signed integer. int(BitVector) defaults to treating the bits as an unsigned int. Assumes twos complement representation. http://en.wikipedia.org/wiki/Twos_complement Positive values decode like so:>>> signedIntFromBV(BitVector(bitstring='0000')) 0 >>> signedIntFromBV(BitVector(bitstring='0101')) 5Here are some negative integer examples: >>> signedIntFromBV(BitVector(bitstring='1111')) -1 >>> signedIntFromBV(BitVector(bitstring='1110')) -2 >>> signedIntFromBV(BitVector(bitstring='1010')) -6 >>> signedIntFromBV(BitVector(bitstring='1001')) -7 >>> signedIntFromBV(BitVector(bitstring='1000')) -8
Note: Does not know the difference between byte orders. |
>>> print int(ais6chartobitvec('0')) 0 >>> print int(ais6chartobitvec('1')) 1 >>> print int(ais6chartobitvec('9')) 9 >>> print int(ais6chartobitvec('<')) 12 >>> print int(ais6chartobitvec('=')) 13 >>> print int(ais6chartobitvec('@')) 16 >>> print int(ais6chartobitvec('A')) 17 >>> print int(ais6chartobitvec('O')) 31 >>> print int(ais6chartobitvec('P')) 32 >>> print int(ais6chartobitvec('Q')) 33 >>> print int(ais6chartobitvec('R')) 34 >>> print int(ais6chartobitvec('Z')) 34 >>> print int(ais6chartobitvec('a')) 41 >>> print int(ais6chartobitvec('w')) 63 >>> print ais6chartobitvec('w') 111111x, y, and z will not appear.
Bug: need to cut down the doctest here and copy all of the current one to tests/test_binary.py |
Note: If the original BitVector had ((len(bitvector) % 6 > 0), then there will be pad bits in the str6. This function has no way to know how many pad bits there are. >>> print ais6tobitvec('6') 000110 >>> print ais6tobitvec('6b') 000110101010 >>> print ais6tobitvec('6bF:Z') 000110101010010110001010100010 Bug: Need to add pad bit handling |
>>> getPadding(BitVector(bitstring='0')) 5 >>> getPadding(BitVector(bitstring='01')) 4 >>> getPadding(BitVector(bitstring='010')) 3 >>> getPadding(BitVector(bitstring='0101')) 2 >>> getPadding(BitVector(bitstring='01010')) 1 >>> getPadding(BitVector(bitstring='010101')) 0 >>> getPadding(BitVector(bitstring='0101010')) 5 @rtype: int @return: number of pad bits required for this bitvector to make it bit aligned to the ais nmea string |
>>> print bitvectoais6(BitVector(bitstring='000110101010010110001010100010')) ('6bF:Z', 0)
To Do: make a test base for needing padding Bug: handle case when padding needed |
|
decodecache of character to BitVector lookup
|
encodecache of ais int value to charcter
|
Trees | Indices | Help |
---|
Generated by Epydoc 3.0beta1 on Sat Mar 31 08:52:53 2007 | http://epydoc.sourceforge.net |