zoukankan      html  css  js  c++  java
  • Python3 bytes与hex字符串之间相互转换

    环境:Python 3.6

    1、字符串转bytes

    '''
    string to bytes
    eg:
    '0123456789ABCDEF0123456789ABCDEF'
    b'0123456789ABCDEF0123456789ABCDEF'
    '''
    def stringTobytes(str):
    return bytes(str,encoding='utf8')
    
    

    2、bytes转字符串

    '''
    bytes to string
    eg:
    b'0123456789ABCDEF0123456789ABCDEF'
    '0123456789ABCDEF0123456789ABCDEF'
    '''
    def bytesToString(bs):
    return bytes.decode(bs,encoding='utf8')
    
    

    3、十六进制字符串转bytes

    '''
    hex string to bytes
    eg:
    '01 23 45 67 89 AB CD EF 01 23 45 67 89 AB CD EF'
    b'x01#Egx89xabxcdxefx01#Egx89xabxcdxef'
    '''
    def hexStringTobytes(str):
    str = str.replace(" ", "")
    return bytes.fromhex(str)
    # return a2b_hex(str)
    
    

    4、bytes转十六进制字符串

    '''
    bytes to hex string
    eg:
    b'x01#Egx89xabxcdxefx01#Egx89xabxcdxef'
    '01 23 45 67 89 AB CD EF 01 23 45 67 89 AB CD EF'
    '''
    def bytesToHexString(bs):
    # hex_str = ''
    # for item in bs:
    # hex_str += str(hex(item))[2:].zfill(2).upper() + " "
    # return hex_str
    return ''.join(['%02X ' % b for b in bs])
    
    
    未经本人同意 请务转载 David QQ:435398366
  • 相关阅读:
    sklearn linear_model,svm,tree,naive bayes,ensemble
    便利过滤
    js下载
    小程序修改radio的大小
    el-tree 问题与需求
    jsp页面用html引入vue.js注意问题
    WebPack
    yarn
    vue-cli 4以上 vue.config.js
    Cannot find module 'core-js/modules/es6.regexp.constructor'
  • 原文地址:https://www.cnblogs.com/dreamblog/p/10219175.html
Copyright © 2011-2022 走看看