简体   繁体   English

Function 字节编码相同的输入并有不同的响应

[英]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.

相关问题 如何为同一个功能使用不同的输入类型? - How to have different input types for the same function? 相同的 HTTP 请求,不同的响应? - Same HTTP requests, different responses? 不同的Unicode字符串编码相同 - Different Unicode strings encode the same 我如何创建一个函数,该函数根据在Python中输入相同输入的次数,使用户输入具有不同的结果? - How do I create a function that allows for user input to have a different outcomes based on how many times the same input was entered in Python? 是否可以在同一个 function 中获得不同的回报? - Is it possible to have different return in the same function? 为什么我对相同文本的 python 和 java base64 编码有不同的结果? - Why I have different results for python and java base64 encode for the same text? 如何识别输入语句的不同响应? - How to identify different responses for an input statement? 与不同的输入文件和不同的输出文件并行运行相同的功能 - Run same function in parallel with different input files and different outputfiles 在两个不同的py virtualenv中使用相同的程序包进行不同的HTTP响应 - Different HTTP responses using same packages in two different py virtualenv 在同一测试中模拟对同一 function 的两个单独响应 - Mock two separate responses to same function in same test
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM