我不是很擅长编码,但这是我认为我知道的(尽管它可能是错误的):
以上这些都是正确的吗?
现在,对于问题:
SetWindowTextA
)是否接受 ASCII 字符串?或者“多字节字符串”(下面有更多问题)? LPWSTR
“宽字符串”?我会说它是,但是,那不就意味着它是 UTF-16 吗?难道这不意味着它可以用来显示 4 字节字符吗?如果没有,那么……显示 4 字节字符是不可能的吗? (Windows 似乎没有这些 API。)WideCharToMultiByte
的功能wcstombs
的超集, 它们都处理相同类型的字符串吗?或者说,一个在 UTF-16 上工作,而另一个在 UCS-2 上工作? fwprintf
等函数,有没有标准化的编码? 这些问题很多,因此任何有关所有这些如何连接的解释的链接(除了阅读 Unicode 标准,无论如何这对 Windows API 没有帮助)也将不胜感激。
谢谢!
最佳答案
Are those above all correct?
是的,如果您不假设存在未以 Unicode 编码的字符(对于大多数实际应用程序,此假设很好)。
Do the Windows "A" functions (like SetWindowTextA) take in ASCII strings? Or "multi-byte strings" (more questions on this below)?
它们采用以当前“ANSI”/MBCS/legacy 编码方式编码的字节字符串(即,其代码单元为字节的字符串,在 Windows 上始终为八位字节)。 “ANSI”是这些编码的历史术语,但不正确。对于西方的 Windows 系统,这种编码通常是 Windows-1252。
Do the Windows "W" functions take in UTF-16 strings or UCS-2 strings? I thought they take in UCS-2, but the names confuse me.
从 Windows 2000 开始,大多数都支持 UTF-16。在现代 Unicode 标准统一术语之前,选择了名称“宽”和其他 Microsoft 术语(例如,“Unicode”表示“UTF-16”或“UCS”)。
In WideCharToMultiByte, Microsoft uses the word "wide-character string" to mean UTF-16. In that context, then what is considered a "multi-byte string"? UTF-8?
WideCharToMultiByte
的所有其他编码在此上下文中支持是“多字节编码”,包括 Windows-1251 和 UTF-8。Is LPWSTR a "wide-character string"? I would say it is, but then, wouldn't that mean it's UTF-16? And wouldn't that mean that it could be used to display, say, 4-byte characters? If not, then... is displaying 4-byte characters impossible? (Windows doesn't seem to have APIs for those.)
LPWSTR
是指向 wchar_t
的指针在 Windows 上它总是一个 16 位无符号整数。可以显示哪些字符与编码无关,只要该编码可以编码所有 Unicode 字符即可。 Windows 通常能够显示非 BMP 字符,但不能在任何地方显示(例如,控制台不能)。Is the functionality of WideCharToMultiByte a superset of that of wcstombs, and do they both work on the same type of string? Or does one, say, work on UTF-16 while the other works on UCS-2?
不知道,但我不认为他们有太大区别。我想您只是尝试将一些非 BMP 字符转换为 UTF-8 并查看结果是否正确。
Are file paths in UTF-16 or UCS-2? I know Windows treats it as an "opaque array of characters" from Microsoft's documentation, but per the C standard for functions like fwprintf, is there any standardized encoding?
文件路径确实是 UTF-16 字符的不透明数组,这意味着 Windows 在存储或读取文件名时不执行任何类型的转换(如 Linux,与 Mac OS X 不同)。但是 Windows 仍然有其奇怪的大部分未定义的不区分大小写的行为,这会导致很多麻烦,因为被视为等效的文件名不一定相等。这打破了许多不变量;例如在Linux上不受其他线程干扰,如果成功创建了两个文件
A
和 a
在某些目录中,您最终会得到两个不同的文件,而在 Windows 上,您只会得到一个文件(通常,文件数量不可预测)。What is "ANSI" encoding? Is that even a correct term? And how does it relate to ASCII?
ANSI 是美国的标准化组织。在提到编码时使用这个词是用词不当,但很常见,所以你应该意识到这一点。我更喜欢旧版 8 位编码这个术语,因为我认为它本质上是这样的:一种非 Unicode 编码,仅用于与旧版 (Windows 9x) 应用程序兼容。在西方系统上,这通常是 Windows-1252,它是 ASCII 的正确超集。
关于winapi - Windows API : ANSI and Wide-Character Strings -- Is it UTF8 or ASCII? UTF-16 还是 UCS-2 LE?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4592261/