[英]Function byte encode same input and have different responses
How is this possible?这怎么可能? I'm using python3.
我正在使用python3。
a= b"\xfc\x48\x83\xe4\xf0\xe8"
b= "\xfc\x48\x83\xe4\xf0\xe8"
if (a == b.encode('ascii',errors='replace')):
print ("Winner")
print (a)
b'\xfcH\x83\xe4\xf0\xe8'
print (b)
üHäðè
I've tried different type of errors but nothing.我尝试了不同类型的错误,但没有。 If I don't put any error, it pop ups an error that It says
codec can't encode character '\xfc' in position 0: ordinal not in range(128)
.如果我没有输入任何错误,它会弹出一个错误,它说
codec can't encode character '\xfc' in position 0: ordinal not in range(128)
。 I have read the manual onthe official manual but I can't find any good answer.我已经阅读了官方手册上的手册,但我找不到任何好的答案。
I would like to know how to convert (force) B to be as A. Same print, same ouput.我想知道如何将(强制)B 转换为 A。相同的打印,相同的输出。
EDIT: I found the solution .编辑:我找到了解决方案。 I wanted to convert from hex string to bytes.
我想从十六进制字符串转换为字节。 Finally what I did was to replace "\x" from the string.
最后我所做的是从字符串中替换“\x”。 This leaves the string with HEX characters.
这会留下带有十六进制字符的字符串。 Then I used the bytes function bytes.fromhex().
然后我使用了字节 function bytes.fromhex()。
b = b.replace("\\x", "")
b = bytes.fromhex(b)
From the documentation you linked:从您链接的文档中:
Encodings don't have to handle every possible Unicode character, and most encodings don't.
编码不必处理所有可能的 Unicode 字符,大多数编码不需要。 For example, Python's default encoding is the 'ascii' encoding.
例如,Python 的默认编码是 'ascii' 编码。 The rules for converting a Unicode string into the ASCII encoding are simple;
将 Unicode 字符串转换为 ASCII 编码的规则很简单; for each code point:
对于每个代码点:
If the code point is < 128, each byte is the same as the value of the code point.
如果代码点 < 128,则每个字节都与代码点的值相同。
If the code point is 128 or greater, the Unicode string can't be represented in this encoding.
如果代码点为 128 或更大,则 Unicode 字符串无法在此编码中表示。 (Python raises a UnicodeEncodeError exception in this case.)
(在这种情况下,Python 会引发 UnicodeEncodeError 异常。)
It looks like the first code point \xfc > 128
which means you cannot represent it in ascii
encoding.它看起来像第一个代码点
\xfc > 128
这意味着你不能用ascii
编码来表示它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.