我工作了一整天,在 Python 3.2中将整数转换为形式为"\x.."的字符串没有任何结果。当使用 ascii 转换或其他时,我得到 '0x..' 或 '\\x..',这不适合我。任何字节或 unicode ("\u92") 添加操作都会导致"SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-2: end of string in escape sequence"
>>>bytes([92])
b'\\'
>>>chr(92) + "x" + str(42)
'\\x42'
>>> str(hex(66))
'0x42'
>>>ascii(bytes([255])).replace(r"'b\", "")
File "<stdin>", line 1
ascii(bytes([255])).replace(r"'b\", "")
^
SyntaxError: invalid syntax
>>> "\x".encode('raw_unicode_escape').decode('ascii')
File "<stdin>", line 1
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-1: end of string in escape sequence