在中联code ++的问题,但不是C [英] Unicode problems in C++ but not C

查看:122
本文介绍了在中联code ++的问题,但不是C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想UNI code字符串写入到C ++ Windows上的屏幕。我改变了我的控制台字体为龙力控制台和我设置的输出 CP_UTF8 又名65001。

I'm trying to write unicode strings to the screen in C++ on Windows. I changed my console font to Lucida Console and I set the output to CP_UTF8 aka 65001.

我运行下面的code:

I run the following code:

#include <stdio.h>  //notice this header file..
#include <windows.h>
#include <iostream>

int main()
{
    SetConsoleOutputCP(CP_UTF8);
    const char text[] = "Россия";
    printf("%s\n", text);
}

它打印出来就好了!

It prints out just fine!

不过,如果我做的:

#include <cstdio>  //the C++ version of the header..
#include <windows.h>
#include <iostream>

int main()
{
    SetConsoleOutputCP(CP_UTF8);
    const char text[] = "Россия";
    printf("%s\n", text);
}

它打印:

我不知道为什么。

另一件事是,当我做的:

Another thing is when I do:

#include <windows.h>
#include <iostream>

int main()
{
    std::uint32_t oldcodepage = GetConsoleOutputCP();
    SetConsoleOutputCP(CP_UTF8);

    std::string text = u8"Россия";
    std::cout<<text<<"\n";

    SetConsoleOutputCP(oldcodepage);
}

我得到相同的输出如上(非工作输出)。

I get the same output as above (non-working output).

使用的printf 的std ::字符串,它工作正常,但:

Using printf on the std::string, it works fine though:

#include <stdio.h>
#include <windows.h>
#include <iostream>

int main()
{
    std::uint32_t oldcodepage = GetConsoleOutputCP();
    SetConsoleOutputCP(CP_UTF8);

    std::string text = u8"Россия";
    printf("%s\n", text.c_str());

    SetConsoleOutputCP(oldcodepage);
}

但只有当我使用 stdio.h中和NOT cstdio

任何想法如何,我可以使用的std :: COUT ?如何使用 cstdio 呢?
为什么会这样?是不是 cstdio 只是一个C ++版本 stdio.h中

Any ideas how I can use std::cout? How can I use cstdio as well? Why does this happen? Isn't cstdio just a c++ version of stdio.h?

编辑:我刚刚试过:

#include <iostream>
#include <io.h>
#include <fcntl.h>

int main()
{
    _setmode(_fileno(stdout), _O_U8TEXT);
    std::wcout << L"Россия" << std::endl;
}

和是的,它的作品,但只有当我使用的std :: wcout 宽字符串。我真的想避免宽弦,我看到迄今唯一的解决办法是在 C-的printf :L

and yes it works but only if I use std::wcout and wide strings. I would really like to avoid wide-strings and the only solution I see so far is the C-printf :l

所以,问题仍然有效。

推荐答案

虽然你已经设置控制台期望UTF-8输出,我怀疑你的编译器处理字符串文字在一些其它字符集之中。我不知道为什么C编译器的作用是不同的。

Although you've set your console to expect UTF-8 output, I suspect that your compiler is treating string literals as being in some other character set. I don't know why the C compiler acts differently.

好消息是,C ++ 11包含UTF-8的一些支持,而微软已实施标准的相关部分。在code是有点毛茸茸的,但你要考虑的 的std :: wstring_convert (转换和UTF-8)和的 &LT; cuchar方式&gt;

The good news is that C++11 includes some support for UTF-8, and that Microsoft has implemented the relevant portions of the Standard. The code is a little hairy, but you'll want to look into std::wstring_convert (converts to and from UTF-8) and the <cuchar> header.

您可以使用这些功能,将其转换为UTF-8,假设您的控制台期待UTF-8,事情应该正常工作。

You can use those functions to convert to UTF-8, and assuming your console is expecting UTF-8, things should work correctly.

就个人而言,当我需要调试这样的事情,我经常直接输出到一个文本文件中。文本编辑器似乎处理的Uni code比Windows控制台更好。就我而言,我常常输出code点正确,但控制台设置错误,使我最终仍打印垃圾。

Personally, when I need to debug something like this, I often direct the output to a text file. Text editors seem to handle Unicode better than the Windows console. In my case, I often output the code points correctly, but have the console set up incorrectly so that I still end up printing garbage.

我可以告诉你,这在Linux(使用锵)和Windows(使用GCC 4.7.3和3.5锵为我工作,你需要添加STD = C ++ 11到命令行编译GCC或锵):

I can tell you that this worked for me in both Linux (using Clang) and Windows (using GCC 4.7.3 and Clang 3.5; you need to add "std=c++11" to the command line to compile with GCC or Clang):

#include <cstdio>

int main()
{
    const char text[] = u8"Россия";
    std::printf("%s\n", text);
}

使用Visual C ++(2012年,但我相信它也将与2010年的工作),我只好用:

Using Visual C++ (2012, but I believe it would also work with 2010), I had to use:

#include <codecvt>
#include <cstdio>
#include <locale>
#include <string>

int main()
{
    std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
    auto text = converter.to_bytes(L"Россия");
    std::printf("%s\n", text.c_str());
}

这篇关于在中联code ++的问题,但不是C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆