在以下代码中,我将根据单选按钮的选择将图像文件转换为字符串:
def convert_now(self):
self.img_data = ""
self.img_data_encoded = ""
file1 = open(self.filedict,'rb')
self.img_data = file1.read()
#RADIO_BUTTONS CHOICES, Convert to: 0-ascii, 1-base64, 2-Hex
v = self.rvar.get()
if v==0:
self.img_data_encoded=self.img_data
elif v==1:
self.img_data_encoded=base64.b64encode(self.img_data) (!)
elif v==2:
self.img_data_encoded=base64.b16encode(self.img_data) (!!)
我尝试使用此行(!)从图像文件中获取base64字符串,并将其保存到名为字符串的“ st”中。
然后我尝试使用这个(!!)
问题是,当我使用“ st”(代码中的base64字符串)将我从上述代码中获得的结果与从该网站“ https://www.branah.com/ascii-converter”中获得的结果进行比较时,
他们根本不匹配。
我写错什么了吗?
答案 0 :(得分:0)
不,看来您做错了什么。您的代码看起来不错。另外,对我来说,b64encode
和b16encode
都产生与您链接到的网页相同的输出。这是一个例子。
>>> import base64
>>> base64.b64encode(b"test")
b'dGVzdA=='
>>> base64.b16encode(b"test")
b'74657374'
您可以将其与网页上的结果进行比较。他们匹配。因此,肯定还有其他问题。 b64encode
和b16encode
工作正常。