java字節碼與python字節碼互轉

def pb2jb(byte_arr):
    """
    python字節碼轉java字節碼
    :param byte_arr:
    :return:
    """
    return [int(i) - 256 if int(i) > 127 else int(i) for i in byte_arr]


def jb2pb(byte_arr):
    """
    java 字節碼轉python字節碼
    :return:
    """
    return [i + 256 if i < 0 else i for i in byte_arr]


def hex2jb(hex_str):
    """
    十六進制數據轉java字節碼
    eg:
        hex_str = "5f 3c f2 81 c8 0f 88 89 c7 b1 99 77 58 c5 4c 04"
    :return:
    """
    return [int(i, 16) - 256 if int(i, 16) > 127 else int(i, 16) for i in hex_str.split(" ")]


def hex2pb(hex_str):
    """
    十六進制數據轉python字節碼
    eg:
        hex_str = "5f 3c f2 81 c8 0f 88 89 c7 b1 99 77 58 c5 4c 04"
    :return:
    """
    return [int(i, 16) for i in hex_str.split(" ")]


def pb2str(byte_arr, encoding="utf-8"):
    """
    python字節碼轉str
    :return:
    """
    return bytes(byte_arr).decode(encoding)


def jb2str(byte_arr, encoding="utf-8"):
    """
    java字節碼轉str
    :return:
    """
    return bytes(jb2pb(byte_arr)).decode(encoding)


def hex2str(hex_str, encoding="utf-8"):
    """
    hex轉str
    :param hex_str: "2c 22 70 61 79 63 68 65 63 6b 6d 6f 64 65 22 3a"
    :param encoding:
    :return:
    """
    return bytes(hex2pb(hex_str)).decode(encoding)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章