1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
# -*- coding: utf-8 -*-
class ByteUtil:
@staticmethod
def combine(*args):
baos = bytearray()
for a in args:
if type(a) in (list, bytearray, str, bytes):
baos.extend(a)
else:
baos.append(a)
return baos
# @staticmethod
# def xsplit(inp, firstLength, secondLength, thirdLength = None):
# parts = []
# parts.append(inp[:firstLength])
# parts.append(inp[len(parts[0]): secondLength + 1])
# if thirdLength:
# parts.append(inp[len(parts[1]): thirdLength + 1])
# return parts
@staticmethod
def split(inp, firstLength, secondLength, thirdLength=None):
parts = []
parts.append(inp[:firstLength])
parts.append(inp[firstLength:firstLength + secondLength])
if thirdLength is not None:
parts.append(inp[firstLength + secondLength: firstLength + secondLength + thirdLength])
return parts
@staticmethod
def trim(inp, length):
return inp[:length]
@staticmethod
def intsToByteHighAndLow(highValue, lowValue):
highValue = ord(highValue) if type(highValue) is str else highValue
lowValue = ord(lowValue) if type(lowValue) is str else lowValue
return ((highValue << 4 | lowValue) & 0xFF) % 256
@staticmethod
def highBitsToInt(value):
bit = ord(value) if type(value) is str else value
return (bit & 0xFF) >> 4
@staticmethod
def lowBitsToInt(value):
return value & 0xF
@staticmethod
def intToByteArray(_bytes, offset, value):
_bytes[offset + 3] = value % 256
_bytes[offset + 2] = (value >> 8) % 256
_bytes[offset + 1] = (value >> 16) % 256
_bytes[offset] = (value >> 24) % 256
return 4
|