【发布时间】:2011-12-03 18:44:56
【问题描述】:
1 wprintf 显示 'Ω' 为 3A9 (UTF16) 真的很奇怪,但是 wctomb convert wchar 到 CEA9 (UTF8),我的语言环境是默认 en_US.utf8。正如手册页所说, 它们应该符合我的语言环境,但 wpritnf 使用 UTF16,为什么?
摘自http://www.fileformat.info/info/unicode/char/3a9/index.htm
UTF 中的Ω
UTF-8(十六进制)0xCE 0xA9(cea9)
UTF-16(十六进制)0x03A9 (03a9)
2 wprintf 和 printf 不能在同一个程序中运行,我有 选择使用 wprintf 或 printf,为什么?
查看我的程序:
#include <stdio.h>
#include <wchar.h>
#include <stdlib.h>
#include <locale.h>
int main() {
setlocale(LC_ALL,""); // inherit locale setting from environment
int r;
char wc_char[4] = {0,0,0,0};
wchar_t myChar1 = L'Ω'; //greek
// should comment out either wprintf or printf, they don't run together
r = wprintf(L"char is %lc (%x)\n", myChar1, myChar1);//On Linux, to UTF16
r = wctomb(wc_char, myChar1); // On Linux, to UTF8
r = printf("r:%d, %x, %x, %x, %x\n", r, wc_char[0], wc_char[1], wc_char[2], wc_char[3]);
}
【问题讨论】:
-
我不确定你在问什么,但我可以告诉你 UTF-16 从未在 Linux 上的
char或wchar_t中使用。 (而且它不能用于任何符合 C 的实现。) -
如果你运行程序,wprintf ("%x", myChar1);打印 3a9(UTF16 中的Ω)但不打印 cea9(UTF8 中的Ω)
-
据我所知
wchar_t在 Linux 中是 32 位的。正如 R.. 所说,它不是 UTF-16。我认为语言环境只影响非宽字符功能。 (如果我错了,请纠正我) -
@Mysticial:反过来。非宽函数是纯字节复制,除了
%ls和%lc与printf和scanf。宽函数将它们输出的所有宽字符转换为语言环境的编码。 -
@R..:谢谢,很高兴知道。 (我显然不会经常更改我的语言环境...XD)
标签: c linux console wchar-t wchar