HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //home/arjun/projects/env/lib/python3.10/site-packages/pathlib_mate/str_encode.py
# -*- coding: utf-8 -*-

"""
utility functions for string encoding.
"""

import binascii


def encode_hexstr(text):
    """
    Convert any utf-8 string to hex string.

    :type text: str

    :rtype: str

    Example::

        >>> encode_hexstr("/home/your_username")
        2f686f6d652f796f75725f757365726e616d65

    **中文文档**

    将任意 utf-8 字符串编码为 16 进制字符串。
    """
    return binascii.b2a_hex(text.encode("utf-8")).decode("utf-8")


def decode_hexstr(text):
    """
    Reverse operation of :func:`encode_hexstr`.

    :type text: str

    :rtype: str

    **中文文档**

    将 16 进制字符串解码为原字符串。
    """
    return binascii.a2b_hex(text.encode("utf-8")).decode("utf-8")