请告诉String操作函数的char16_t
版本是什么
如:
http://www.tutorialspoint.com/ansi_c/c_function_references.htm
我找到了许多引用网站,但没有人提到过。
特别是对于打印功能,这是最重要的,因为它帮助我验证操纵功能是否有效。
#include <stdio.h>
#include <uchar.h>
char16_t *u=u"α";
int main(int argc, char *argv[])
{
printf("%x\n",u[0]); // output 3b1, it is UTF16
wprintf("%s\n",u); //no ouput
_cwprintf("%s\n",u); //incorrect output
return 0;
}
答案 0 :(得分:2)
要打印/读取/打开写入等,您需要使用mbsrtowcs
函数转换为32位字符。
对于所有意图和目的,char16_t是一个多字节表示,因此,需要使用mbr
函数来处理这个整数类型。
一些答案使用了L“前缀”,这是完全错误的。 16位字符串需要u“前缀”。
以下代码为您提供了使用8位,16位和32位字符串表示所需的一切。
#include <string.h>
#include <wchar.h>
#include <uchar.h>
如果您没有手册页(UNIX),则可以使用<wchar.h>
中的步骤进行Google操作。
如果开销不是问题,Gnome.org的GLib会为您提供一些很好的代码。
char16_t
和char32_t
是ISO C11(iso9899:2011
)扩展。
答案 1 :(得分:1)
wprintf
及其wchar同事也需要在wchar中使用格式字符串:
wprintf( L"%s\n", u);
对于wchar L 用作字符串文字的前缀。
修改强>
这是一段代码片段(在Windows上测试):
#include <stdio.h>
#include <io.h>
#include <fcntl.h>
#include <wchar.h>
void main()
{
wchar_t* a = L"α";
fflush(stdout); //must be done before _setmode
_setmode(_fileno(stdout), _O_U16TEXT); // set console mode to unicode
wprintf(L"alpha is:\n\t%s\n", a); // works for me :)
}
控制台无法在unicode中运行并打印“?”对于非ascii字符。在Linux中,您需要删除setmode
和fileno
之前的下划线前缀。
注意:对于Windows GUI打印,已经有适当的支持,因此您可以使用wsprintf格式化unicode字符串。