Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将字节转换为字符串?_Python_Python 3.x_String_Byte_Python Unicode - Fatal编程技术网

Python 如何将字节转换为字符串?

Python 如何将字节转换为字符串?,python,python-3.x,string,byte,python-unicode,Python,Python 3.x,String,Byte,Python Unicode,我有一个字节,我想把它转换成python中的字符串 以下是我要转换的字节: b'\xfeD1\xc2B\xac\xf86Mx\xc1\x0e\xa4\xe2x\x0c,\xd4\x1a\xb1' 但我无法将其转换为普通字符 此外,我提供上述字节作为输入 我试过: my_str = b'\xfeD1\xc2B\xac\xf86Mx\xc1\x0e\xa4\xe2x\x0c,\xd4\x1a\xb1'.decode() print(my_str) 我得到的错误是: Traceback (most

我有一个字节,我想把它转换成python中的字符串

以下是我要转换的字节:

b'\xfeD1\xc2B\xac\xf86Mx\xc1\x0e\xa4\xe2x\x0c,\xd4\x1a\xb1'
但我无法将其转换为普通字符

此外,我提供上述字节作为输入

我试过:

my_str = b'\xfeD1\xc2B\xac\xf86Mx\xc1\x0e\xa4\xe2x\x0c,\xd4\x1a\xb1'.decode()
print(my_str)
我得到的错误是:

Traceback (most recent call last):
  File "E:/Mainproject.py", line 39, in <module>
    my_str = b'\xfeD1\xc2B\xac\xf86Mx\xc1\x0e\xa4\xe2x\x0c,\xd4\x1a\xb1'.decode()
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xfe in position 0: invalid start byte

decode
字节
转换为
str
<代码>编码将
str
转换为
字节

my_str = b'\xfeD1\xc2B\xac\xf86Mx\xc1\x0e\xa4\xe2x\x0c,\xd4\x1a\xb1'.decode()

您需要使用
latin1
编码,但这会导致如果这些字节是非拉丁字符,则最终将使用Mojibake字符,这在您的情况下似乎是如此。你能说这是什么样的绳子吗

my_str = b'\xfeD1\xc2B\xac\xf86Mx\xc1\x0e\xa4\xe2x\x0c,\xd4\x1a\xb1'
print(my_str.decode("latin1"))
编辑:您正在尝试解码加密的
密文
,并使您的文本返回未锁定状态,这是加密试图阻止的,要返回您的文本,您必须先解密,然后解码使用以下代码:

from pylfsr import LFSR

state = [0,0,0,1,0,1,0,1,0,1,1]
poly = [2,2,3,4,2]
l = LFSR(fpoly=poly, initstate =state)
print(l)
message = input("Enter message to encrypt: ").encode()
ciphertext = b""

allseq = l.runFullCycle()
seq = ""
seq_index = 0

for x in allseq:
    seq += str(x)
for counter in range(len(message)):
    ran_seq = seq[seq_index: seq_index+8]
    ciphertext += bytes([int(message[counter]) ^ int(ran_seq, 2)])
    seq_index += 8  # Move sequence to Next byte

print(ciphertext)
ciphertext_file_name = "ciphertext"
with open(ciphertext_file_name, "wb") as out_file:
    out_file.write(ciphertext)

# Read ciphertext and decrypt it
with open(ciphertext_file_name, "rb") as in_file:
    ciphertext = in_file.read()
    seq_index = 0
    plaintext_again = b""
    for counter in range(len(ciphertext)):
        ran_seq = seq[seq_index: seq_index + 8]
        plaintext_again += bytes([int(ciphertext[counter]) ^ int(ran_seq, 2)])
        seq_index += 8  # Move sequence to Next byte
    print(plaintext_again.decode("latin1"))

我尝试了这个方法,但出现了一个错误,我在ques中更新了它。您拥有的字节不代表UTF-8字符串,因此没有合理的方法将其转换为UTF-8字符串。这些字节是如何创建的?如果它们是使用不同的编码创建的,您可以指定在
decode(encoding='whatever')
@martineau中,使用这种编码不会给我任何错误,但同时也没有解码正确的输出:(是的,是真的,我已经尝试并获得了Mojibake字符:(@WOZNIK这个字符串是什么语言?英语,“这是纯文本”@WOZNIK:问题中显示的字节数据不是纯文本。@WOZNIK您可以用如何编码这个纯文本来编辑问题,因为这个纯文本似乎与字节字符串无关
from pylfsr import LFSR

state = [0,0,0,1,0,1,0,1,0,1,1]
poly = [2,2,3,4,2]
l = LFSR(fpoly=poly, initstate =state)
print(l)
message = input("Enter message to encrypt: ").encode()
ciphertext = b""

allseq = l.runFullCycle()
seq = ""
seq_index = 0

for x in allseq:
    seq += str(x)
for counter in range(len(message)):
    ran_seq = seq[seq_index: seq_index+8]
    ciphertext += bytes([int(message[counter]) ^ int(ran_seq, 2)])
    seq_index += 8  # Move sequence to Next byte

print(ciphertext)
ciphertext_file_name = "ciphertext"
with open(ciphertext_file_name, "wb") as out_file:
    out_file.write(ciphertext)

# Read ciphertext and decrypt it
with open(ciphertext_file_name, "rb") as in_file:
    ciphertext = in_file.read()
    seq_index = 0
    plaintext_again = b""
    for counter in range(len(ciphertext)):
        ran_seq = seq[seq_index: seq_index + 8]
        plaintext_again += bytes([int(ciphertext[counter]) ^ int(ran_seq, 2)])
        seq_index += 8  # Move sequence to Next byte
    print(plaintext_again.decode("latin1"))