如何在C ++中处理Unicode字符 [英] How to deal with the Unicode characters in C++

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

问题描述

我们在引擎中内置了一个注释系统,程序员可以使用该注释系统为各种公开的变量/对象添加注释,然后GUI前端将其用作工具提示和帮助.

We have a commenting system built into our engine that allows programmers to put comments for various exposed variables/objects which are then used by the GUI front-end for tool-tips and help.

最近,某些工具提示开始崩溃,并且在浪费大量时间后,我一直将其跟踪到以下字符:,除非我弄错了,否则它是unicode字符,不能以ASCII格式使用.

Recently, certain tool-tips started crashing, and after much wasted time I tracked it down to the the character: which, unless I am mistaken, is a unicode character and not available in ASCII.

考虑此答案,我认为wstring将解决此问题.在对较大的项目进行更改之前,我创建了一个测试项目,以查看wstring是否可以解决问题.尽管该项目不会崩溃,但是行为与wstring预期的不同.

Taking this answer into consideration, I assumed wstring would fix the problem. Before making changes in the bigger project, I created a test project to see if wstring would solve the issue. Although the project doesn't crash, the behavior is not as expected for wstring.

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string someString = "successive attack that DOESN’T result";
    wstring someWString = L"successive attack that DOESN’T result";

    cout << someString << endl;
    wcout << someWString << endl;

    return 0;
}

//Console Output//
successive attack that DOESNÆT result 
successive attack that DOESNPress any key to continue . . .

我很早以前读过这篇文章,并认为我理解相关的问题使用字符集,但事实并非如此.我希望能找到解决该问题的方法,并很好地说明正在发生的事情以及将来如何避免类似的问题.

I read this article quite some time ago and thought I understood the problems associated with character sets, but that is obviously not the case. I would appreciate a solution to this problem as well as a good explanation of what is happening and how to avoid problems similar to this in the future.

推荐答案

由于您使用的是Visual Studio,因此我假设您使用的是Windows. Windows控制台不支持unicode.它使用OEM字符集.您可以使用CharToOemW/OemToCharW在两者之间进行转换.显然,它不能代表所有的unicode字符.

Since you are using Visual Studio I assume you are using Windows. The Windows console does not support unicode. It uses the OEM char set. You can convert between the two using CharToOemW/OemToCharW. Obviously it will not be able to represent all unicode characters.

Windows使用UTF16作为其系统API.如果您的工具提示使用Windows API,则可能是您要使用的wstring.但是,您可以改用UTF8,并将其转换为UTF16,然后再调用Windows API.可以使用MultiByteToWideChar/WideCharToMultiByte执行此转换.

Windows uses UTF16 for its system API. If your tooltips uses the Windows API it is probably wstring that you want to use. However, you can use UTF8 instead and convert this to UTF16 before calling the Windows API. This conversion can be performed using MultiByteToWideChar/WideCharToMultiByte.

这篇关于如何在C ++中处理Unicode字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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