问题
code:
#include <windows.h>
int main() {
SetConsoleOutputCP(CP_UTF8);
system("echo Ιλιάδα");
}
prints on console: Ιλιάδα
Source is encoded in UTF-8 with BOM.
But if I try: system(L"echo Ιλιάδα");
, I get error: error: cannot convert 'const wchar_t*' to 'const char*' for argument '1' to 'int system(const char*)'
. And of course I didn't expect anything else here. Is there any other function that will accept these characters ?
回答1:
Use _wsystem
one for wide strings.
回答2:
Is this VC++?
If so, your file appears to be using UTF-8 without BOM, which means that VC++ will assume that the source charset and execution charset are the same, so it will not do any encoding conversion when producing the string literal "echo Ιλιάδα". It will simply output the UTF-8 data directly. This means that the compiler believes you wrote system("echo Ιλιάδα");
where that garbage is your UTF-8 string viewed as though it were the system's locale encoding.
By default the system()
function takes a string in the system's locale encoding. The console output codepage has no effect on system()
's operation, and so the above string is exactly what it sees as well.
Because you're using UTF-8 without BOM, you'll have trouble with wide strings. Generating wide strings requires correctly converting the source charset to the wide execution charset. If you're using UTF-8 without BOM, then VC++ doesn't know the correct source encoding and therefore cannot do this conversion correctly.
回答3:
You can try system("cmd /c lambdabath") or system("lambdabath") as in the example:
//Save As UTF-8 withput BOM signature
#include <stdlib.h>
#include <windows.h>
int main() {
SetConsoleOutputCP(CP_UTF8);
//system("cmd /c lambdabath");
system("lambdabath");
}
lambdabath.bat (also save as UTF-8 without BOM signature):
chcp 65001
echo Ιλιάδα
But if the question is: how to send international characters to windows console?
Then you can try different encoding. Using function system() is not necessary.
Windows 1253 encoding:
//Save As Windows 1253
#include <stdio.h>
#include <windows.h>
int main()
{
SetConsoleOutputCP(1253);
char *ansichar_ = "Ιλιάδα";
unsigned char lambda1253char = 'λ';
printf("ansichar_: %s\n", ansichar_);
printf("λ %#x\n", lambda1253char);
}
Result:
ansichar_: Ιλιάδα
λ 0xeb
UTF-16 encoding:
//Save As UTF16 (Unicode)
#include <stdio.h>
#include <io.h>
#include <fcntl.h>
#include <wchar.h>
int main()
{
_setmode(_fileno(stdout), _O_U16TEXT);
wchar_t *wchar_ = L"Ιλιάδα";
wchar_t lambdaWchar = L'λ';
wprintf(L"wchar_: %s\n", wchar_);
wprintf(L"λ %#x\n", lambdaWchar);
}
Result:
wchar_: Ιλιάδα
λ 0x3bb
UTF-8 encoding:
//Save As UTF8 without BOM signature
#include <stdio.h>
#include <windows.h>
int main()
{
SetConsoleOutputCP(65001);
char *utf8char_ = "Ιλιάδα";
int lambdaUTF8char = 'λ';
printf("utf8char_: %s\n", utf8char_);
printf("λ %#x\n", lambdaUTF8char);
}
Result:
utf8char_: Ιλιάδα
λ 0xcebb
In any case, set the default console font: Lucida Console.
来源:https://stackoverflow.com/questions/10900106/how-to-send-international-characters-to-windows-console