Example #1
0
 def _encode_(self, encodeValue, inProcessPacket):
     """Encodes the provided value into a signed integer fit within the specified number of bytes.
     
     encodeValue -- the signed integer to be encoded.
     """
     twosComplementRepresentation = utilities.signedIntegerToTwosComplement(encodeValue, self.bitSize)  #note that function handles both pos and neg numbers.
     return utilities.unsignedIntegerToBytes(twosComplementRepresentation, self.size) # convert to byte list.
Example #2
0
 def _encode_(self, encodeValue, inProcessPacket):
     """Encodes the provided value into a signed fixed-point decimal."""
     
     bitShiftedValue = encodeValue * 2**self.fractionalBits   #fixed-point encoding is as simple as left-shifting by the fractional bits.
     if self.integerBits > 0:    #signed value
         twosComplementRepresentation = utilities.signedIntegerToTwosComplement(int(bitShiftedValue), self.bitSize) # convert to twos complement
     else:   #no integer bits, unsigned value
         twosComplementRepresentation = bitShiftedValue
     return utilities.unsignedIntegerToBytes(twosComplementRepresentation, self.size)    #convert to byte list
Example #3
0
 def _encode_(self, encodeValue, inProcessPacket):
     """Returns the length of the inProcessPacket, either including or nor itself.
     
     inProcessPacket -- contains a second-pass in-process packet whose length should be measured.
     
     NOTES:  
         -Assumes that there are not multiple length tokens in a single packet. 
         -Because checksums are encoded last, they are not counted in the length value reported by this function.
     """
     
     if len(inProcessPacket)>0:  #an inProcess packet has been provided
         #create and count a list only containing integers from the flattened inProcessPacket
         length = len(list(itertools.ifilter(lambda token: type(token) == int, utilities.flattenList(inProcessPacket))))
         if self.countSelf: length += 1
         return utilities.unsignedIntegerToBytes(length, self.size)  #convert to integer of lenth self.size
     else: return self   #no in-process packet has been provided.
Example #4
0
 def _encode_(self, encodeDictionary, inProcessPacket):
     """Encodes the provided inputValue into a bitfield.
     
     encodeDictionary -- a dictionary containing bitName:bitValue pairs to encode into the bitfield
     """
     outputValue = 0 #default bit field is zeroed out.
     for bitName in self.bitNameBitPositionDictionary:   #iterate thru stored bitfield definition
         bitPosition = self.bitNameBitPositionDictionary[bitName]    #get bit position
         if bitName in encodeDictionary: #check if bitName is in the provided encode dictionary
             bitValue = encodeDictionary[bitName]    #bitName is in encodeDictionary, set bitValue from encodeDictionary
         elif self.bitPositionDefaultValueDictionary[bitPosition] != None:   #check if a default bit value was provided on token creation
             bitValue = self.bitPositionDefaultValueDictionary[bitPosition]  #Bit value not specified in input, use default value instead
         else:
             bitValue = False    #by default set to 0
             
         outputValue = utilities.changeBitInInteger(outputValue, bitPosition, bitValue)
     
     return utilities.unsignedIntegerToBytes(outputValue, self.size)
Example #5
0
 def _encode_(self, encodeValue, inProcessPacket):
     """Converts an unsigned integer into a sequence of bytes.
     
     encodeValue -- contains an unsigned integer.
     """
     return utilities.unsignedIntegerToBytes(encodeValue, self.size)