如何在程序中使用unicode字符集?

lizzoe 2009-01-24 05:16:45
我正在移植一段代码,程序原来是先弄了个utf-8.ini文件,在里面放的其实就是utf-8格式的码表,但只有英文字符和常用标点,没有中文,所以导致在程序中无法输入和显示中文。
现在有两种方案,
1、去找一个完整的unicode的码表来代替这个utf-8.ini文件,
这样的好处是不用去改动代码逻辑,把字符集拿来后就能直接使用了。但是这个完整的码表上哪弄去?就是找到了也必然非常的大,因为汉字还是很多的,O(∩_∩)O~
2、更改代码中获取字符集的方法
这样看起来会好很多,直接在代码中让他获取操作系统的字符集,这样就能省很多事。
我个人现在很倾向第2种方法,但不知道如何去做,菜鸟一个,还望路过的大侠们出手相助啊,小弟十分感谢。
...全文
255 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
wwwxxb40000 2009-02-02
  • 打赏
  • 举报
回复
学习
waizqfor 2009-01-24
  • 打赏
  • 举报
回复
MSDN上的介绍
Using Generic-Text Mappings
Microsoft Specific —>

To simplify code development for various international markets, the Microsoft run-time library provides Microsoft-specific “generic-text” mappings for many data types, routines, and other objects. These mappings are defined in TCHAR.H. You can use these name mappings to write generic code that can be compiled for any of the three kinds of character sets: ASCII (SBCS), MBCS, or Unicode, depending on a manifest constant you define using a #define statement. Generic-text mappings are Microsoft extensions that are not ANSI compatible.

Preprocessor Directives for Generic-Text Mappings

#define Compiled Version Example
_UNICODE Unicode (wide-character) _tcsrev maps to _wcsrev
_MBCS Multibyte-character _tcsrev maps to _mbsrev
None (the default: neither _UNICODE nor _MBCS defined) SBCS (ASCII) _tcsrev maps to strrev


For example, the generic-text function _tcsrev, defined in TCHAR.H, maps to mbsrev if MBCS has been defined in your program, or to _wcsrev if _UNICODE has been defined. Otherwise _tcsrev maps to strrev.

The generic-text data type _TCHAR, also defined in TCHAR.H, maps to type char if _MBCS is defined, to type wchar_t if _UNICODE is defined, and to type char if neither constant is defined. Other data type mappings are provided in TCHAR.H for programming convenience, but _TCHAR is the type that is most useful.

Generic-Text Data Type Mappings

Generic-Text Data Type Name SBCS (_UNICODE, _MBCS Not Defined)
_MBCS Defined
_UNICODE Defined
_TCHAR char char wchar_t
_TINT int int wint_t
_TSCHAR signed char signed char wchar_t
_TUCHAR unsigned char unsigned char wchar_t
_TXCHAR char unsigned char wchar_t
_T or _TEXT No effect (removed by preprocessor) No effect (removed by preprocessor) L (converts following character or string to its Unicode counterpart)


For a complete list of generic-text mappings of routines, variables, and other objects, see Appendix B, Generic-Text Mappings.

The following code fragments illustrate the use of _TCHAR and _tcsrev for mapping to the MBCS, Unicode, and SBCS models.

_TCHAR *RetVal, *szString;
RetVal = _tcsrev(szString);

If MBCS has been defined, the preprocessor maps the preceding fragment to the following code:

char *RetVal, *szString;
RetVal = _mbsrev(szString);

If _UNICODE has been defined, the preprocessor maps the same fragment to the following code:

wchar_t *RetVal, *szString;
RetVal = _wcsrev(szString);

If neither _MBCS nor _UNICODE has been defined, the preprocessor maps the fragment to single-byte ASCII code, as follows:

char *RetVal, *szString;
RetVal = strrev(szString);

Thus you can write, maintain, and compile a single source code file to run with routines that are specific to any of the three kinds of character sets.

See Also Generic-text mappings, Data type mappings, Constants and global variable mappings, Routine mappings, A sample generic-text propgram

END Microsoft Specific
http://blog.csdn.net/ice197983/archive/2007/03/27/1542466.aspx来个 _MBCS与_UNICODE环境编译系统配置
waizqfor 2009-01-24
  • 打赏
  • 举报
回复
[Quote=引用楼主 lizzoe 的帖子:]
我正在移植一段代码,程序原来是先弄了个utf-8.ini文件,在里面放的其实就是utf-8格式的码表,但只有英文字符和常用标点,没有中文,所以导致在程序中无法输入和显示中文。
现在有两种方案,
1、去找一个完整的unicode的码表来代替这个utf-8.ini文件,
这样的好处是不用去改动代码逻辑,把字符集拿来后就能直接使用了。但是这个完整的码表上哪弄去?就是找到了也必然非常的大,因为汉字还是很多的,O(∩_∩)O~
2、更改…
[/Quote]
unicode代码用宽字符表示 所以定义字符串的是要用wstring wchar_t [] 这样就可以表示unicode编码了
Notepad2用这个软件也可以在utf-8和unicode之间转换

xiaoyisnail 2009-01-24
  • 打赏
  • 举报
回复
要注意的是vc6默认为_MBCS,要改成_UNICODE
vs2005/2008都默认为UNICODE
xiaoyisnail 2009-01-24
  • 打赏
  • 举报
回复
如果想同时支持unicode和mbcs的话可以用<tchar.h>中的宏:

#include <iostream>
#include <string>
#include <tchar.h>
using namespace std;

int main()
{
TCHAR str[] = _T("新年好");//这里就是unicode
wcout.imbue(locale("chs"));
wcout<<str<<endl;

return 0;
}
xiaoyisnail 2009-01-24
  • 打赏
  • 举报
回复
或者:

#include <iostream>
#include <string>

using namespace std;

int main()
{
wchar_t str[] = L"新年好";//这里就是unicode
wcout.imbue(locale("chs"));
wcout<<str<<endl;

return 0;
}
xiaoyisnail 2009-01-24
  • 打赏
  • 举报
回复

#include <iostream>
#include <string>

using namespace std;

int main()
{
wstring str = L"新年好";//这里就是unicode
wcout.imbue(locale("chs"));
wcout<<str<<endl;

return 0;
}
xiaoyisnail 2009-01-24
  • 打赏
  • 举报
回复
编译选项加上UNICODE或_UNICODE,然后代码中用宽字符,wstring,wchar_t这些就可以支持unicode了啊

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧