1

On Windows i can do like this:

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

On Linux it throws an error:

>>> "\x98".encode("ANSI")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
LookupError: unknown encoding: ANSI

How can i get b"\x98" from "\x98" on linux? CPXXXX encodings don't work for me, because \x98 doesnt exist

kin4stat
  • 68
  • 4
  • I cannot reproduce this. It definitely depends on more than just whether you are using "Windows" or "Linux". – Karl Knechtel Apr 01 '21 at 21:55
  • Python said ["Encode the operand according to the ANSI codepage (CP_ACP)."](https://docs.python.org/3.9/library/codecs.html?highlight=ansi). and Microsoft said CP_ACP is [The system default Windows ANSI code page.](https://docs.microsoft.com/en-us/windows/win32/api/stringapiset/nf-stringapiset-widechartomultibyte). It seems that CP_ACP is locale-dependent. – Aaron Apr 01 '21 at 22:01
  • docs.python.org says that ANSI is windows only (CP_ACP) – kin4stat Apr 01 '21 at 22:01

2 Answers2

1

Regarding this definition of ANSI encoding:

ANSI encoding is a slightly generic term used to refer to the standard code page on a system, usually Windows. It is more properly referred to as Windows-1252 on Western/U.S. systems.

You can try:

"\x98".encode("cp1252")

But, Python doesn’t allow it so, you can use ISO Latin1 instead:

"\x98".encode("iso_8859_1")
# b'\x98'
Laurent LAPORTE
  • 20,141
  • 5
  • 53
  • 92
0

I'm not sure what you're expecting this to do. U+0098 is an unprintable character. You can try bytes([ord("\x98")]) to do a raw translation.

Tim Roberts
  • 34,376
  • 3
  • 17
  • 24