我尝试解析文本并在其中找到一些字符.我使用下面的代码.它适用于普通字符,abcdef
但它无法使用öç??ü?
.GCC提供编译警告.我该怎么做才能合作öç??ü?
?
代码:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main()
{
char * text = "öç??ü";
int i=0;
text = strdup(text);
while (text[i])
{
if(text[i] == 'ö')
{
printf("ö \n");
}
i++;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
警告 :
warning: multi-character character constant [-Wmultichar]
warning: comparison is always false due to limited range of data type [-Wtype-limits]
Run Code Online (Sandbox Code Playgroud)
在while循环中打印char的地址时有10个地址
printf("%d : %p \n", i, text[i]);
Run Code Online (Sandbox Code Playgroud)
输出:
0 : 0xffffffc3
1 : 0xffffffb6
2 : 0xffffffc3
3 : 0xffffffa7
4 : 0xffffffc5
5 : 0xffffff9f
6 : 0xffffffc4
7 : 0xffffff9f
8 : 0xffffffc3
9 : 0xffffffbc
Run Code Online (Sandbox Code Playgroud)
并且strlen
是10.
但如果我使用abcde
:
0 : 0x61
1 : 0x62
2 : 0x63
3 : 0x64
4 : 0x65
Run Code Online (Sandbox Code Playgroud)
并且strlen
是5.
如果我wchar_t
用于文本输出是
0 : 0xa7c3b6c3
1 : 0x9fc49fc5
2 : 0xbcc3
Run Code Online (Sandbox Code Playgroud)
而且strlen
是10,wcslen
是3.
Top*_*ort -2
处理宽字符的最佳方法就是作为宽字符。
\n\nwchar_t myWord[] = L"Something";\n
Run Code Online (Sandbox Code Playgroud)\n\n这将做到这一点:
\n\n#include <stdio.h>\n#include <ctype.h>\n#include <string.h>\n\nint main()\n{\n wchar_t * text = L"\xc3\xb6\xc3\xa7\xc5\x9f\xc4\x9f\xc3\xbc";\n int i = 0;\n\n while (text[i])\n {\n if (text[i] == L\'\xc3\xb6\')\n {\n wprintf(L"\xc3\xb6 \\n");\n }\n\n i++;\n }\n\n return 0;\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n如果您像我一样使用 Visual Studio,请记住控制台窗口不能很好地处理 Unicode。您可以将其重定向到一个文件并检查该文件,然后查看\xc3\xb6
.