Python实现16进制读写

最近的工作中需要一个工具来读写网络的封包,因此用python实现了一个读写16进制数据的小工具
Python实现16进制读写_第1张图片

class Hex_Read(object):
    def __init__(self):
        super(Hex_Read, self).__init__()
        self.len=0
        self.ptr=0

    def set_data(self,data):
        data=data.replace(" ","")
        assert len(data) % 2 == 0 ,"输入Hex不完整"
        self.bytes=bytes.fromhex(data.upper())
        self.len=len(self.bytes)
        self.ptr=0

    def int(self):
        assert self.len - self.ptr >= 4  ,"超出索引"
        value=int.from_bytes(self.bytes[self.ptr:self.ptr+4],"big")
        self.ptr+=4
        return value

    def byte(self):
        assert self.len-self.ptr>=1 ,"超出索引"
        value = int.from_bytes(self.bytes[self.ptr:self.ptr+1], "big")
        self.ptr += 1
        return value

    def short(self):
        assert self.len - self.ptr >= 2 ,"超出索引"
        value = int.from_bytes(self.bytes[self.ptr:self.ptr+2], "big")
        self.ptr += 2
        return value

    def text_1(self):
        assert self.len - self.ptr >= 1, "超出索引"
        index= int.from_bytes(self.bytes[self.ptr:self.ptr + 1], "big")
        self.ptr += 1
        assert self.len - self.ptr >= index, "超出读取索引"
        value = self.bytes[self.ptr:self.ptr + index].decode("GB2312")
        self.ptr += index
        return value

    def text_2(self):
        assert self.len - self.ptr >= 2, "超出索引"
        index = int.from_bytes(self.bytes[self.ptr:self.ptr + 2], "big")
        self.ptr += 2
        assert self.len - self.ptr>=index , "超出读取索引"
        value=self.bytes[self.ptr:self.ptr +  index ].decode("GB2312")
        self.ptr += index
        return value

    def skip(self,step):
        assert self.len - self.ptr >= step, "超出索引"
        self.ptr += step

    def hex(self,num):
        assert self.len - self.ptr >= num, "超出索引"
        value=self.bytes[self.ptr:self.ptr+num].hex(" ").upper()
        self.ptr += num
        return value

    def get_raw_len(self):
        return self.len

    def get_raw_data(self):
        return self.bytes.hex(" ").upper()

    def get_ptr(self):
        return self.ptr

    def get_len(self):
        return self.len-self.ptr


class Hex_Writer(object):
    def __init__(self):
        super(Hex_Writer, self).__init__()
        self.len = 0
        self.bytes=bytes()


    def int(self,value):
        self.bytes+=value.to_bytes(4,"big")
        self.len+=4



    def byte(self,value):
        self.bytes += value.to_bytes(1, "big")
        self.len += 1


    def short(self,value):
        self.bytes += value.to_bytes(2, "big")
        self.len += 2

    def text_1(self,value):
        byte= value.encode("GB2312")
        str_len=len(byte)
        self.bytes += str_len.to_bytes(1, "big")
        self.bytes += byte
        self.len = self.len + 1 + str_len



    def text_2(self,value):
        byte = value.encode("GB2312")
        str_len = len(byte)
        self.bytes += str_len.to_bytes(2, "big")
        self.bytes += byte
        self.len = self.len + 2 + str_len

    def hex(self, value):
        value=value.replace(" ","")
        str_len = len(value)
        assert str_len % 2 == 0, "无法整除"
        self.bytes +=bytes.fromhex(value)
        self.len += int(str_len/2)


    def get_len(self):
        return self.len

    def get_packet(self):
        hex = self.bytes.hex().upper()
        assert (len(hex) % 2) == 0, "无法整除"
        return hex

    def get_packet_space(self):
        hex=self.bytes.hex().upper()
        assert (len(hex)%2)==0,"无法整除"
        text_list=[hex[i*2:2*i+2] for i in range(int(len(hex)/2))]
        return ' '.join(text_list)



if __name__=='__main__':
    w=Hex_Writer()
    w.int(122)
    w.short(232)
    w.byte(12)
    w.text_1("哈哈哈哈哈")
    w.text_2("哇哇哇哇!!")
    w.hex(" 00 15 45")


    r=Hex_Read()
    r.set_data(w.get_packet_space())
    print(r.int())
    print(r.short())
    print(r.byte())
    print(r.text_1())
    print(r.text_2())
    print(r.hex(3))
    print(r.get_raw_data())

你可能感兴趣的:(Pytorch学习笔记,python)