[英]How to convert bytes into string?
I have a bytes and I want to convert it into string in python ?我有一个字节,我想在 python 中将它转换为字符串?
These are the bytes that I want to convert:这些是我要转换的字节:
b'\xfeD1\xc2B\xac\xf86Mx\xc1\x0e\xa4\xe2x\x0c,\xd4\x1a\xb1'
but I am not able to convert it into normal characters.但我无法将其转换为普通字符。
Also I am providing the above byte as input.我也提供上述字节作为输入。
I had tried:我试过:
my_str = b'\xfeD1\xc2B\xac\xf86Mx\xc1\x0e\xa4\xe2x\x0c,\xd4\x1a\xb1'.decode()
print(my_str)
and the error that I am getting is:我得到的错误是:
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
Edit:1 This is the code i used to encode the text.编辑:1这是我用来编码文本的代码。 Here i am trying to some pn sequence to encrypt an inputed text and the resultant output that i am getting is a byte.
在这里,我试图用一些 pn 序列来加密输入的文本,而我得到的结果输出是一个字节。 Now i want to create another program that takes this byte as input and decode it into plain text.
现在我想创建另一个程序,将这个字节作为输入并将其解码为纯文本。
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)
decode
converts bytes
to str
. decode
将bytes
转换为str
。 encode
converts str
to bytes
. encode
将str
转换为bytes
。
my_str = b'\xfeD1\xc2B\xac\xf86Mx\xc1\x0e\xa4\xe2x\x0c,\xd4\x1a\xb1'.decode()
You need to use latin1
encoding but this have consequences of if these bytes are non latin characters you will end up with Mojibake character which seems to be the case in your situation.您需要使用
latin1
编码,但这会导致如果这些字节是非拉丁字符,您最终会得到 Mojibake 字符,这在您的情况下似乎就是这种情况。 Can you say what kind of string this is?你能说出这是什么类型的字符串吗?
my_str = b'\xfeD1\xc2B\xac\xf86Mx\xc1\x0e\xa4\xe2x\x0c,\xd4\x1a\xb1'
print(my_str.decode("latin1"))
EDIT: You are trying to decode encrypted ciphertext
and get your text back un tacked which is what encryption try to prevent, To get your text back you will have to decrypt first then decode use this code:编辑:您正在尝试解码加密的
ciphertext
并使您的文本恢复原状,这是加密试图阻止的内容,要恢复您的文本,您必须先解密,然后使用以下代码解码:
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"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.