载入中,请稍候……

GB2312,Unicode,UTF-8占用空间

Admin 于 2009-03-28 14:02:29 发表其它

ANSI(GB2312)编码,半角字符占用一个字节,全角字符占用2个字节。
Unicode编码,无论半角还是全角,都占用2个字节。
UTF-8编码,半角字符占用1个字节,全角字符占用3个字节。

被阅647次, 0票GB2312 Unicode UTF-8 发表评论

在VC中进行Unicode开发

Admin 于 2008-09-30 02:22:54 发表C/C++

1. 添加 UNICODE 和 _UNICODE 预处理定义
位置:Project Settings -> C/C++ -> Preprocessor definitions
添加了这两个定义后,MFC的一些内置类型如 TCHAR、CString 都将转为支持宽字符类型(wchar_t)
2. 使用宽字符相关类型,如:char -> TCHAR、char * -> LPTSTR、const char * -> LPCTSTR
3. 对字符串常量使用 _T() 宏
4. 替换C库中的中字符串操作函数,如 strlen -> _tcslen、strcmp -> _tcscmp 等,类似的还有C库中字符串与数字的转换函数,如 atoi -> _ttoi、itoa -> _itot 等。
5. 将 Project Settings -> Link -> Output -> Entry Point 设为 wWinMainCRTSTartup,否则会有如下错误:msvcrtd.lib(crtexew.obj) : error LNK2001: unresolved external symbol _WinMain@16
6. C++标准库中的string,有对应的宽字符版本wstring,两者均为basic_string的特化版本,可在StdAfx.h中添加:

  1. #ifdef _UNICODE 
  2. #define tstring wstring 
  3. #else 
  4. #define tstring string 
  5. #endif 

然后在代码中使用 tstring 即可,类似的还有 fstream/wfstream、ofstream/wofstream 等
7. 宽字符版本的英文字符仍可直接与整型值进行比较,如:
CString s = _T("ABC");
ASSERT(s[0] == 'A');
8. 对于仍需使用ANSI字符串的地方,如第三方类库的接口,仍可继续使用;如需进行Unicode字符串和ANSI字符串的互转换,可使用 MultiByteToWideChar 和 WideCharToMultiByte

被阅693次, 0票Unicode 发表评论
1 / 1 / 2 | « 1 » |

Powered by MiniBoke v2.0.0.8 Build 0828

Copyright © 2008 迷你博客. All rights reserved.

粤ICP备07500939号