python中的ANSI编码

问题描述

在 Windows 上我可以这样做:

SecureRandom::random_bytes(16).each_byte.map { |b| sprintf("%02X",b) }.join

在 Linux 上它会抛出一个错误

>>> "\x98".encode("ANSI")
b'\x98'

如何在 Linux 上从 >>> "\x98".encode("ANSI") Traceback (most recent call last): File "<stdin>",line 1,in <module> LookupError: unkNown encoding: ANSI 获取 b"\x98""\x98" 编码对我不起作用,因为 \x98 不存在

解决方法

关于this definition of ANSI encoding

ANSI 编码是一个稍微通用的术语,用于指代系统(通常是 Windows)上的标准代码页。在西方/美国,它更恰当地称为 Windows-1252。系统。

你可以试试:

"\x98".encode("cp1252")

但是,Python 不允许这样做,您可以使用 ISO Latin1 代替:

"\x98".encode("iso_8859_1")
# b'\x98'
,

我不确定你期望它做什么。 U+0098 是不可打印的字符。您可以尝试 bytes([ord("\x98")]) 进行原始翻译。