Python 十六进制hex-bytes-str之间的转换和Bcc码的生成
前言
近期做测试模拟器用到了hex-bytes-str之间的转换bcc码的校验,这里总结了一些方法。
实例
直接上代码
- 转为十六进制(Hex)字符串
def getStringFromNumber(self,size,value): """ 转为十六进制(Hex)字符串 :param size: :param value: :return: """ size=int(size) value=int(value) by = bytearray([]) for i in range(1,size+1): val = value >> 8 * (size - i) & 255 by.append(val) val = by.hex() print("===============================") print("%s转为%s个字节十六进制(Hex)字符串:%s"%(value,size,val)) print("===============================") return val getStringFromNumber(5,11)
执行代码:
方法:getStringFromNumber(size,value)参数一为生成几个batys,参数二为被转化数字
- 生成bcc校验码
def bcc(self,value): """ bcc 检查码 :param number: :return: """ numb = len(number) nb = int(numb / 2) bcc = 0 for i in range(nb): a = 2 * i b = 2 * (i + 1) aa = number[a:b] bcc = bcc ^ int(aa, 16) print("===============================") print("生成bcc码:%s" % f'{bcc:x}') print("===============================") return f'{bcc:x}' bcc('11111111111111111111111111111111111111111111111111111111111222222222222222222222')
执行代码:
方法:bcc(value)参数被转化数字
- 时间字节数组(byte[])转为十六进制(Hex)字符串和解码时间
def datetime(self): """ 字节数组(byte[])转为十六进制(Hex)字符串 :return: """ print("===============================") current_time = time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime(time.time())) datatime = current_time[2:] by = bytearray([]) aa = datatime.split("-") for val in aa: by.append(int(val)) val = by.hex() print("当前%s转化十六进制:%s" % (current_time, val)) return val def time_hex_dec(self,number): """ 时间解码 :param number: :return: """ numb = len(number) nb = int(numb / 2) datatime=[] for i in range(nb): a = 2 * i b = 2 * (i + 1) num = number[a:b] datatime.append(num) Gettime ="20" for key,val in enumerate(datatime): b = eval("0x" + val) Gettime += str(b) if key <5: Gettime = Gettime +"-" print('时间解码:%s' % Gettime) print("===============================") return Gettime time = datetime() time_hex_dec(time)
执行代码:
方法:time (value)参数被转化数字 time_hex_dec(time)解码时间
- 字符串转16进制 与 16进制转字符串
def str_to_hex(self,data): """ # 字符串转16进制 :param data: :return: """ print("===============================") str_16 = binascii.b2a_hex(data.encode('utf-8')) print("字符串%s转16进制:%s"%(data,str_16)) return str_16 def hex_to_str(self,data): """ # 16进制转字符串 :param data: :return: """ strs = (binascii.unhexlify(data)).decode() print("16进制%s转字符串:%s"%(data,strs)) print("===============================") return strs data = str_to_hex("wencheng你好") hex_to_str(data)
执行代码:
方法:str_to_hex(value) ,hex_to_str(data)
- 字符串转字节 str --> bytes与字节转字符串 bytes --> str
def str_to_bytes(self,b): """ 字符串转字节 str --> bytes :param b: :return: """ print("===============================") print(str.encode(b)) # 默认 encoding="utf-8" print(bytes(b, encoding="utf8")) print(b.encode()) # 默认 encoding="utf-8" return str.encode(b) def bytes_to_str(self,a): """ 字节转字符串 bytes --> str :param b: :return: """ print(bytes.decode(a)) # 默认encoding="utf-8" print(str(a, encoding="utf-8")) print(a.decode()) # 默认 encoding="utf-8" print("===============================") data = str_to_bytes("wencheng你好") bytes_to_str(data)
执行代码:
方法:str_to_bytes(value) ,bytes_to_str(data)
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家的支持。
更多文章关注小编公众号:自动化测试 To share