数据转换方法

1.bytes的hex函数,将bytes的列表对象值转换为hexstr(十六进制字符串) 

举例,将一个int列表转换为16进制字符串

>>> bytes([9,10,11,12,13,14]).hex()
'090a0b0c0d0e'

2.bytes函数可以将列表转换为bytes"字节串”

>>>data=bytes([11,12,13,14,15,16])
>>>print(data)
结果:
b'\x0b\x0c\r\x0e\x0f\x10'

3.整型列表int-list或十六进制字符串hex-string 转 byets


def ToBytes(data):
    if type(data) == type('12'):#如果是字符串
        if len(data)%2 != 0:   #数据长度为奇数
            data += '0'        #在数据后面补零
            print("add '0' at end,amended: ",end="")
            print(data)
        return bytes().fromhex(data)#将字符串数据转换为bytes格式,并返回
    elif type(data) == type([1,]):#如果数据类型为int列表
        return bytes(data)#直接返回bytes格式的数据

4.bytes或int-list    转 hex-strin

def ToHexStr(data):#定义一个处理数据的方法
    if type(data) == type([1,]):#如果数据类型是整型列表
        bytes_data = ToBytes(data)##先将整型列表转换为bytes格式
        return bytes_data.hex()#bytes格式数据转换为16进制字符串
    elif type(data) == type(b'\x00'):#如果是bytes格式数据
        return data.hex()#直接转换为16进制字符串
    else:
        print("only 'list' or 'bytes' is valid!")
        return None

5.bytes或hex-string 转 int-list

def ToIntList(data):#定义一个处理数据的函数
    if type(data) == type('12'):#如果数据类型是16进制字符串
        bytes_data = ToBytes(data)#先将字符串数据转为bytes格式数据
        return list(bytes_data)#返回一个列表型的bytes数据
    elif type(data) == type(b'\x00'):#如果数据类型是bytes格式数据
        return list(data)#直接返回一个列表型的bytes数据
    else:
        print("only 'str' or 'bytes' is valid!")
        return None

列表解析:

举例

hex_str="111213141516"

hex_str_len = len(hex_str)


hex_str_list = ["".join(hex_str[2*i:2*i+2]) for i in range(hex_str_len//2)]
print(hex_str_list)
结果为
['11', '12', '13', '14', '15', '16']



hex_str_list=[]
for i in range(hex_str_len//2):
    hex_str_list1="".join(hex_str[2*i:2*i+2])
    hex_str_list.append(hex_str_list1)
print(hex_str_list)

结果为
['11', '12', '13', '14', '15', '16']

#对hex-string 每2位增加一个空格

#对hex-string 每2位增加一个空格
def InsertBlock(hex_str):#定义一个针对16进制字符串执行插入空格操作的函数
    if type(hex_str) != type("12"):#如果数据类型不是16进制字符串
        print("hex_str is not 'str' type!")#
        return None
    else:
        if len(hex_str)%2 != 0:#如果数据长度是奇数
            hex_str += '0'#字符串末尾增加一个0字符串
            print("add '0' at end,amended: ",end="")
            print(hex_str)
        hex_str_len = len(hex_str)
        # print(hex_str_len)
        hex_str_list = ["".join(hex_str[2*i:2*i+2]) for i in range(hex_str_len//2)]#

bdata=['5', '5', 'a', 'b', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0']


list_0=list(bdata.hex())#间隔一个字节的列表
list_1=[]#创建已各空的列表
list_len=len(list_0)-2
for i in range(0,list_len,2):
    list_2=list_0[i]+list_0[i+1]
    list_1.append(list_2)
print(list_1)


['55', 'ab', '00', '00', '01', '00', '00', '00', '00', '00', '00', '00', '00', '00']

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值