UNICODE和ASCII
来源:互联网 发布:算法塞奇威克 编辑:程序博客网 时间:2024/06/05 08:48
软件中的字符采用UNICODE方式编码是软件走向国际化的重要一个因素,而且现代很多操作系统默认的字符编码方式都是采用UNICODE的,但是很多的库函数都是采用ASCII编码方式,以及一些第三方库也采用ASCII编码,所以字符需要在UNICODE编码以及ASCII编码之间转换,转换采用sprintf以及windows API实现。
//%s String When used with sprintf functions, specifies a single-byte–character string; when used with swprintf functions, specifies a wide-character string. Characters are printed up to the first null character or until the precision value is reached.
//%S String When used with sprintf functions, specifies a wide-character string; when used with swprintf functions, specifies a single-byte–character string. Characters are printed up to the first null character or until the precision value is reached.
//ascii to unicode 一
char szbuf[100];
strcpy(szbuf, "abcdefg");
wchar_t szw[100];
swprintf(szw, L"%S", szbuf);
//acii to unicode 二
int MultiByteToWideChar(
UINT CodePage, // code page
DWORD dwFlags, // character-type options
LPCSTR lpMultiByteStr, // address of string to map
int cchMultiByte, // number of bytes in string
LPWSTR lpWideCharStr, // address of wide-character buffer
int cchWideChar // size of buffer
);
::MultiByteToWideChar(CP_ACP, 0, szbuf, -1, szw, 100);
//unicode to ascii 一
sprintf(szbuf, "%S", szw);
//unicode to ascii 二
int WideCharToMultiByte(
UINT CodePage, // code page
DWORD dwFlags, // performance and mapping flags
LPCWSTR lpWideCharStr, // address of wide-character string
int cchWideChar, // number of characters in string
LPSTR lpMultiByteStr, // address of buffer for new string
int cchMultiByte, // size of buffer
LPCSTR lpDefaultChar, // address of default for unmappable
// characters
LPBOOL lpUsedDefaultChar // address of flag set when default
// char. used
);
::WideCharToMultiByte(CP_ACP, 0, szw, -1, szbuf, 100, NULL, NULL);
知道了字符怎样在两种编码之间转换,那怎样编写出既支持unicode编译又支持ascii编译的程序呢?
可以采用_T宏,必须包含TCHAR.H头文件
//TCHAR.H Routine
TCHAR szt[100];
TCHAR sz[] = _T("abcdefg");
_stprintf(szt, _T("%s"), sz);
#ifdef _UNICODE
std::wcout << szt << std::endl;
#else
std::cout << szt << std::endl;
#endif
如果要把TCHAR类型的字符转换成UNICODE编码或者ASCII编码可以采用条件编译,这样代码就既支持unicode编译又支持ascii编译(必须确认是否定义UNICODE宏和_UNICODE宏)
注意:_UNICODE宏用于C运行期头文件,而UNICODE宏则用于Windows头文件
- ASCII码 和 Unicode
- UNICODE和ASCII
- ANSI、ASCII和UNICODE
- ASCII和Unicode区别
- ASCII、Unicode和ISO10646
- ASCII、GB2312和Unicode
- ASCII和UNICODE
- ASCII和Unicode编码
- Unicode和ascii
- ASCII和UNICODE
- Unicode 和ASCII码
- Unicode 和 ASCII
- unicode和ascII
- ascii和unicode表
- unicode和Ascii的转换
- unicode和Ascii的转换
- ASCII、Unicode和UTF-8
- Unicode和Ascii互换.js
- 新年新计划~~
- 浅析大型门户网站的架构 [转]
- How to create a user in PostgreSQL
- 国内大型门户网站架构分析-大型门户网站的cdn部署(服务器负载均衡)
- Java技术回顾之JNDI:JNDI API
- UNICODE和ASCII
- Pebrowse Professional Interactive V 8.6.2
- [转贴]看电影总结出来的靠不住之终结版
- MMPP--物料替换
- 网人网语
- 咽喉肿痛
- VC 手把手教你手工添加消息的方法
- VC++实现窗口的任意分割
- 电脑开机就打开小键盘