2

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
  • 192
  • 1
  • 11
  • 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://learn.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

3 Answers3

2

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
  • 21,958
  • 6
  • 58
  • 103
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
  • 48,973
  • 4
  • 21
  • 30
0

You can use this:

def get_ansi_cp():
    import codecs
    import locale
    return codecs.lookup(locale.getpreferredencoding()).name

Which at my machine returns cp1252.

cxxl
  • 4,939
  • 3
  • 31
  • 52