What characters do not directly map from Cp1252 to UTF-8?

跟風遠走 提交于 2019-12-03 00:05:04

This is how Windows 1252 codepage looks like.

As you can see, bytes 0x81, 0x8D, 0x8F, 0x90, 0x9D do not have anything assigned to them.

If your input file contains those bytes, and you treat it as if it was in Windows 1252 encoding, those bytes will be treated as invalid characters. In normal circumstances, this means that the input file was not in Windows 1252.

All other bytes encode either printable characters or control characters, and all those characters are present in Unicode and therefore can unambiguously be encoded in UTF-8.

I have no idea what the linked answer is trying to claim, its last paragraph sounds like nonsense.

Several more remarks, which may shine some light on what you are trying to get to know:

  • UTF-8 and Windows 1252 are totally incompatible with each other outside ASCII

  • both of those encodings will never encode text to certain byte values, different ones in each case

  • moreover, certain byte sequences are also invalid in UTF-8

  • in general, if you treat a file as if it contained text encoded in UTF-8 or Windows 1252, but it doesn't, you will lose and corrupt data

You can select the encoding of your files in your IDE or editor. It's recommended to go UTF-8 only. You will have to convert existing Windows 1252 files.

Can someone please shed some more light on this?

The cp1252 decoding function is mostly an identity function.

cp1252    UCP       (UCP = Unicode Code Point)
--------  --------
21        21 (!)    (All numbers in hex)
31        31 (1)
41        41 (A)

This makes it seem like something expecting UCP (not UTF-8) will also accept cp1252. The author of the linked Answer is pointing out that this is not the case.

cp1252    UCP
--------  --------
80        20AC (€)
85        2026 (…)
99        2122 (™)

The exceptions are all found between 80 and 9F, inclusive.

Something that accepts UCP will also accept iso-8859-1, but not cp1252.


Does that mean that if I batch/mass convert source code from cp1252 to utf-8 I'll get some characters that will end up as garbage?

No. Every character in cp1252 maps to a Unicode Code, so it can successfully be converted to UTF-8.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!