如何将base64解码后的\x转换为可读的?

时间:2022-08-06 18:33:52

I'm trying to decode the following Base64 string into readable text.

我正在尝试把下面的Base64字符串解码成可读的文本。

T2ggeWVhaCEgAQ==

I'm using Python Base64 library to do so. However, when I do, I get:

我正在使用Python Base64库这样做。然而,当我这样做时,我得到:

>>> base64.b64decode("T2ggeWVhaCEgAQ==")
'Oh yeah! \x01'

What is \x01?

\ x01是什么?

How do I decode such that all the text is readable and I don't get any weird symbols?

我如何解码,使所有的文字都是可读的,我没有任何奇怪的符号?

3 个解决方案

#1


1  

'\x01' is a text representation of bytes in Python 2. '\x01' is a single byte. Bytes that are in ASCII printable range represent themselves e.g., you see 'O' instead of '\x4f':

'\x01'是Python 2中字节的文本表示形式。“\x01”是一个单字节。ASCII可打印范围内的字节表示它们自己,例如,你看到'O'而不是'\x4f':

>>> b'\x4f\x68\x20\x79\x65\x61\x68\x21\x20\x01'
'Oh yeah! \x01'

To remove all "weird" bytes (to keep characters from string.printable):

删除所有“奇怪”字节(防止字符被串起。可打印):

#!/usr/bin/env python
import string

weird = bytearray(set(range(0x100)) - set(map(ord, string.printable)))
print(b'Oh yeah! \x01'.translate(None, weird).decode())
# -> Oh yeah!

string.printable contains some non-printable characters such as '\t' (tab), '\n' (newline). To exclude them too and to leave only printing character:

字符串。可打印包含一些不可打印的字符,如'\t' (tab)、'\n' (newline)。把它们也排除在外,只留下印刷字符:

printing_chars = range(0x20, 0x7e + 1)
weird = bytearray(set(range(0x100)) - set(printing_chars))
print(b'Oh yeah! \x01'.translate(None, weird))
# -> Oh yeah! 

#2


1  

You could filter out the unreadable characters:

你可以过滤掉不可读的字符:

from string import printable
print ''.join(c for c in base64.b64decode('T2ggeWVhaCEgAQ==') if c in printable)

#3


0  

The last byte of the Base64 encoded data is hex 01. This isn't a printable character in any commonly used encoding; there's no way to make it into "readable text" without turning it into something it isn't.

Base64编码数据的最后一个字节是hex01。在任何常用的编码中,这都不是可打印的字符;要把它变成“可读的文本”,就必须把它变成它不是的东西。

#1


1  

'\x01' is a text representation of bytes in Python 2. '\x01' is a single byte. Bytes that are in ASCII printable range represent themselves e.g., you see 'O' instead of '\x4f':

'\x01'是Python 2中字节的文本表示形式。“\x01”是一个单字节。ASCII可打印范围内的字节表示它们自己,例如,你看到'O'而不是'\x4f':

>>> b'\x4f\x68\x20\x79\x65\x61\x68\x21\x20\x01'
'Oh yeah! \x01'

To remove all "weird" bytes (to keep characters from string.printable):

删除所有“奇怪”字节(防止字符被串起。可打印):

#!/usr/bin/env python
import string

weird = bytearray(set(range(0x100)) - set(map(ord, string.printable)))
print(b'Oh yeah! \x01'.translate(None, weird).decode())
# -> Oh yeah!

string.printable contains some non-printable characters such as '\t' (tab), '\n' (newline). To exclude them too and to leave only printing character:

字符串。可打印包含一些不可打印的字符,如'\t' (tab)、'\n' (newline)。把它们也排除在外,只留下印刷字符:

printing_chars = range(0x20, 0x7e + 1)
weird = bytearray(set(range(0x100)) - set(printing_chars))
print(b'Oh yeah! \x01'.translate(None, weird))
# -> Oh yeah! 

#2


1  

You could filter out the unreadable characters:

你可以过滤掉不可读的字符:

from string import printable
print ''.join(c for c in base64.b64decode('T2ggeWVhaCEgAQ==') if c in printable)

#3


0  

The last byte of the Base64 encoded data is hex 01. This isn't a printable character in any commonly used encoding; there's no way to make it into "readable text" without turning it into something it isn't.

Base64编码数据的最后一个字节是hex01。在任何常用的编码中,这都不是可打印的字符;要把它变成“可读的文本”,就必须把它变成它不是的东西。