php base64转十六进制,Python中十六进制到Base64的转换

链接到的工具只是将十六进制解释为字节,然后将这些字节编码为Base64。from binascii import unhexlify, b2a_base64

result = b2a_base64(unhexlify(hex_string))

或者from binascii import b2a_base64

result = b2a_base64(bytes.fromhex(hex_string))

在Python 2中,还可以使用str.decode()和str.encode()方法来实现相同的功能:result = hex_string.decode('hex').encode('base64')

在Python 3中,您必须为此使用codecs.encode()函数。

Python3中的演示:>>> bytes.fromhex('10000000000002ae')

b'\x10\x00\x00\x00\x00\x00\x02\xae'

>>> from binascii import unhexlify, b2a_base64

>>> unhexlify('10000000000002ae')

b'\x10\x00\x00\x00\x00\x00\x02\xae'

>>> b2a_base64(bytes.fromhex('10000000000002ae'))

b'EAAAAAAAAq4=\n'

>>> b2a_base64(unhexlify('10000000000002ae'))

b'EAAAAAAAAq4=\n'

Python2.7上的演示:>>> '10000000000002ae'.decode('hex')

'\x10\x00\x00\x00\x00\x00\x02\xae'

>>> '10000000000002ae'.decode('hex').encode('base64')

'EAAAAAAAAq4=\n'

>>> from binascii import unhexlify, b2a_base64

>>> unhexlify('10000000000002ae')

'\x10\x00\x00\x00\x00\x00\x02\xae'

>>> b2a_base64(unhexlify('10000000000002ae'))

'EAAAAAAAAq4=\n'

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值