回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我有一个字节,我想把它转换成python中的字符串</p>
<p>以下是我要转换的字节:</p>
<pre><code>b'\xfeD1\xc2B\xac\xf86Mx\xc1\x0e\xa4\xe2x\x0c,\xd4\x1a\xb1'
</code></pre>
<p>但我无法将其转换为普通字符</p>
<p>此外,我提供上述字节作为输入</p>
<p>我试过:</p>
<pre><code>my_str = b'\xfeD1\xc2B\xac\xf86Mx\xc1\x0e\xa4\xe2x\x0c,\xd4\x1a\xb1'.decode()
print(my_str)
</code></pre>
<p>我得到的错误是:</p>
<pre><code>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
</code></pre>
<p><strong>编辑:1</strong>
这是我用来编码文本的代码。这里我尝试使用一些pn序列来加密输入的文本,得到的结果输出是一个字节。
现在我想创建另一个程序,将这个字节作为输入,并将其解码为纯文本</p>
<pre><code>from pylfsr import LFSR
# The initial state
state = [0,0,0,1,0,1,0,1,0,1,1]
# The LFSR polynomial use a primitive polynomail to get maximum period length
poly = [2,2,3,4,2]
l = LFSR(fpoly=poly, initstate =state)
print(l)
message = input().encode()
ciphertext = b""
# generate all LFSR sequence
allseq = l.runFullCycle()
seq = ""
seq_index = 0
# Convert LFSR bits into a string
for x in allseq:
seq += str(x)
for counter in range(len(message)):
ran_seq = seq[seq_index: seq_index+8]
# Now encrypt by XOR convert to bytes and append to ciphertext
# print(type(message[counter]),message[counter],type(ran_seq),ran_seq,int(message[counter]^int(ran_seq)))
ciphertext += bytes([int(message[counter]) ^ int(ran_seq, 2)])
# print(ciphertext)
seq_index += 8 # Move sequence to Next byte
print(ciphertext)
</code></pre>