首页 > 解决方案 > 如何在 python 3.7 中将 ascii 字符串更改为十六进制,反之亦然?

问题描述

我在这个网站上寻找了一些解决方案,但那些在 python 3.7 中不起作用。所以,我问了一个新问题。

“the”的十六进制字符串是“746865”

我想要一个将“the”转换为“746865”和“746865”转换为“the”的解决方案

标签: data-conversionpython-3.7

解决方案


    #!/usr/bin/python3
    """
    Program name:  txt_to_ASC.py

    The program transfers
    a string of letters -> the corresponding
      string of hexadecimal ASCII-codes,
    eg.  the -> 746865

    Only letters in [abc...xyzABC...XYZ] should be input.

    """
    print("Transfer letters to hex ASCII-codes")
    print("Input range is [abc...xyzABC...XYZ].")
    print()
    string = input("Input set of letters, eg. the: ")
    print("hex ASCII-code: " + " "*15, end = "")

    def str_to_hasc(x):
        global glo

        byt = bytes(x, 'utf-8')
        bythex = byt.hex()

        for b1 in bythex:
            y = print(b1, end = "")
            glo = str(y)
        return glo

    str_to_hasc(string)

推荐阅读