python中的错误解码结果

2024-04-29 05:39:19 发布

您现在位置:Python中文网/ 问答频道 /正文

{1>为什么我的字符串隐藏在前面?在

我在结果中得到这个字符串: '1101000011001010110110001101100011'

def retr(filename):
    img = Image.open(filename)
    binary = ''

    if img.mode in ('RGBA'):
        img = img.convert('RGBA')
        datas = img.getdata()

        for item in datas:
            digit = decode(rgb2hex(item[0], item[1], item[2]))
            if digit == None:
                pass
            else:
                binary = binary + digit
                if (binary[-16:] == '1111111111111110'):
                   # print("Success")
                    return bin2str(binary[:-16])

        return str(bin2str(binary))
    return "Incorrect Image Mode, Couldn't Retrieve"

但控制台中的结果是:b'hello'b来自哪里?在

retr()之前做一些准备工作:

^{pr2}$

请帮我抓住b。。在


Tags: 字符串inimageimgreturnifdeffilename
3条回答

bin2str正在返回字节文本。您可以使用.decode()返回字符串。在

def bin2str(binary):
    message = binascii.unhexlify('%x' % (int('0b' + binary, 2)))
    return message.decode("utf-8") # or encoding of choice

我相信任何一个字节串都会在字符串前面加上“b'”来表示它来自一个二进制值。将二进制值转换为字符串后,可以执行替换函数:

newstring = message.replace("b", "")
newstring = message.replace("'", "")
x = b'hello'
print(x)
   b'hello'
print(x.decode('utf-8'))
   'hello'

我希望这足够说明,以便您了解如何将它恢复为utf-8字符串

相关问题 更多 >