def _decode(data, mode=16, offset=0, end=None):
   """
   Decodes numheader from data (which is byte or bytearray).
   Mode can be either 16 or 32 (number of bits parsed when long_bit is 1)
   
   Returns tuple (bytesParsed, value)
   """   
   bytesParsed=0
   value=None
   if end is None:
      end=len(data)
   if(offset+1<=end): #at least 1 byte in data?
      b1=data[offset]
      if b1 & 0x80:         
         b1&=0x7F
         #MSB is set, parse next 1 or 3 bytes depending on mode
         if mode == 16: #NumHeader16
            if(offset+2<=end):
               bytesParsed=2
               b2=data[offset+1]
               value=(b1<<8 | b2)
               if value < 128: #range(0,128) with MSB set to 1 shall be interpreted as 32768..32895
                  value+=32768
         elif mode == 32: #NumHeader32
            if(offset+4<=end):
               bytesParsed=4
               b2,b3,b4=data[offset+1:offset+4]
               value=(b1<<24)|(b2<<16)|(b3<<8)|b4
         else:
            raise ValueError('invalid mode argument: '+str(mode))
      else:
         bytesParsed=1
         value=b1
   return (bytesParsed,value)