def long2raw(value, endian, size=None): r""" Convert a number (positive and not nul) to a raw string. If size is given, add nul bytes to fill to size bytes. >>> long2raw(0x1219, BIG_ENDIAN) '\x12\x19' >>> long2raw(0x1219, BIG_ENDIAN, 4) # 32 bits '\x00\x00\x12\x19' >>> long2raw(0x1219, LITTLE_ENDIAN, 4) # 32 bits '\x19\x12\x00\x00' """ assert (not size and 0 < value) or (0 <= value) assert endian in (LITTLE_ENDIAN, BIG_ENDIAN) text = [] while (value != 0 or text == ""): byte = value % 256 text.append( chr(byte) ) value >>= 8 if size: need = max(size - len(text), 0) else: need = 0 if need: if endian is BIG_ENDIAN: text = chain(repeat("\0", need), reversed(text)) else: text = chain(text, repeat("\0", need)) else: if endian is BIG_ENDIAN: text = reversed(text) return "".join(text)
def long2raw(value, endian, size=None): r""" Convert a number (positive and not nul) to a raw string. If size is given, add nul bytes to fill to size bytes. >>> long2raw(0x1219, BIG_ENDIAN) '\x12\x19' >>> long2raw(0x1219, BIG_ENDIAN, 4) # 32 bits '\x00\x00\x12\x19' >>> long2raw(0x1219, LITTLE_ENDIAN, 4) # 32 bits '\x19\x12\x00\x00' """ assert (not size and 0 < value) or (0 <= value) assert endian in (LITTLE_ENDIAN, BIG_ENDIAN, MIDDLE_ENDIAN) text = [] while (value != 0 or text == ""): byte = value % 256 text.append(chr(byte)) value >>= 8 if size: need = max(size - len(text), 0) else: need = 0 if need: if endian is LITTLE_ENDIAN: text = chain(text, repeat("\0", need)) else: text = chain(repeat("\0", need), reversed(text)) else: if endian is not LITTLE_ENDIAN: text = reversed(text) if endian is MIDDLE_ENDIAN: text = arrswapmid(text) return "".join(text)
def str2long(data, endian): r""" Convert a raw data (type 'str') into a long integer. >>> chr(str2long('*', BIG_ENDIAN)) '*' >>> str2long("\x00\x01\x02\x03", BIG_ENDIAN) == 0x10203 True >>> str2long("\x2a\x10", LITTLE_ENDIAN) == 0x102a True >>> str2long("\xff\x14\x2a\x10", BIG_ENDIAN) == 0xff142a10 True >>> str2long("\x00\x01\x02\x03", LITTLE_ENDIAN) == 0x3020100 True >>> str2long("\xff\x14\x2a\x10\xab\x00\xd9\x0e", BIG_ENDIAN) == 0xff142a10ab00d90e True >>> str2long("\xff\xff\xff\xff\xff\xff\xff\xff", BIG_ENDIAN) == (2**64-1) True """ assert 1 <= len(data) <= 32 # arbitrary limit: 256 bits try: return unpack(_struct_format[endian][len(data)], data)[0] except KeyError: pass assert endian in (BIG_ENDIAN, LITTLE_ENDIAN) shift = 0 value = 0 if endian is BIG_ENDIAN: data = reversed(data) for character in data: byte = ord(character) value += (byte << shift) shift += 8 return value
def _getPath(self): if not self._parent: return '/' names = [] field = self while field: names.append(field._name) field = field._parent names[-1] = '' return '/'.join(reversed(names))
def bin2long(text, endian): """ Convert binary number written in a string into an integer. Skip characters differents than "0" and "1". >>> bin2long("110", BIG_ENDIAN) 6 >>> bin2long("110", LITTLE_ENDIAN) 3 >>> bin2long("11 00", LITTLE_ENDIAN) 3 """ assert endian in (LITTLE_ENDIAN, BIG_ENDIAN) bits = [ (ord(character)-ord("0")) \ for character in text if character in "01" ] assert len(bits) != 0 if endian is not BIG_ENDIAN: bits = reversed(bits) value = 0 for bit in bits: value *= 2 value += bit return value