Hex to Base64 conversion in Python

前端 未结 7 1040
一整个雨季
一整个雨季 2021-02-04 03:47

I want to convert a simple HEX string such as 10000000000002ae to Base 64.

The hex string is to be converted to bytes, and the bytes are then encoded to base64 notation,

7条回答
  •  花落未央
    2021-02-04 04:09

    In python3, you may use bytes.fromhex to bytes, use base64 package convert bytes to base64

    hex_str = '01'
    encoded_str = base64.b64encode(bytes.fromhex(hex_str)).decode('utf-8')
    decoded_str = base64.b64decode(encoded_str.encode('utf-8')).hex()
    

提交回复
热议问题