使用Variab打印原始字节

2024-04-18 21:26:40 发布

您现在位置:Python中文网/ 问答频道 /正文

啊!解决了的!在

在python 2命令行中运行:

>>> print r"\x72"
\x72

python将返回:\x72 但如果我这么做了:

^{pr2}$

它将打印“r”。我也知道如果我这样做:

>>> a = r"\x72"
>>> print a
\x72

但我想做的是:

>>> a = "\x72"

把它做成这样我就可以把它打印成:

r"\x73"

我怎么转换?在

编辑: 我正在打印从服务器接收的字节。在

工作溶液:

def byte_pbyte(data):
    # check if there are multiple bytes
    if len(str(data)) > 1:
        # make list all bytes given
        msg = list(data)
        # mark which item is being converted
        s = 0
        for u in msg:
            # convert byte to ascii, then encode ascii to get byte number
            u = str(u).encode("hex")
            # make byte printable by canceling \x
            u = "\\x"+u
            # apply coverted byte to byte list
            msg[s] = u
            s = s + 1
        msg = "".join(msg)
    else:
        msg = data
        # convert byte to ascii, then encode ascii to get byte number
        msg = str(msg).encode("hex")
        # make byte printable by canceling \x
        msg = "\\x"+msg
    # return printable byte
    return msg

Tags: toconvertdatamakeifbytesasciimsg
2条回答

多亏了ginginsha,我才能够创建一个将字节转换为可打印字节的函数:

def byte_pbyte(data):
    # check if there are multiple bytes
    if len(str(data)) > 1:
        # make list all bytes given
        msg = list(data)
        # mark which item is being converted
        s = 0
        for u in msg:
            # convert byte to ascii, then encode ascii to get byte number
            u = str(u).encode("hex")
            # make byte printable by canceling \x
            u = "\\x"+u
            # apply coverted byte to byte list
            msg[s] = u
            s = s + 1
        msg = "".join(msg)
    else:
        msg = data
        # convert byte to ascii, then encode ascii to get byte number
        msg = str(msg).encode("hex")
        # make byte printable by canceling \x
        msg = "\\x"+msg
    # return printable byte
    return msg

这可能是python : how to convert string literal to raw string literal?的副本

我想你想要的是逃避你的越狱程序被解释。在

a = '\\x72'

为了让它打印完整的\x72

相关问题 更多 >