VC下Unicode 到UTF-8的转换
来源:互联网 发布:ug8.5数控编程实例 编辑:程序博客网 时间:2024/05/17 21:49
Ansi字符串英文占一个字节,汉字2个字节,以一个/0结尾,常用于txt文本文件。
Unicode字符串,每个字符(汉字、英文字母)都占2个字节,以2个连续的/0结尾,NT操作系统内核用的是这种字符串,常被定义为typedef unsigned short wchar_t;所以我们有时常会见到什么char*无法转换为unsigned short*之类的错误,其实就是unicode。
UTF8是Unicode一种压缩形式,英文A在unicode中表示为0x0041,浪费了50%的空间,于是英文压缩成1个字节,成了utf8编码,但是汉字在utf8中占3个字节,显然用做中文不如ansi合算。所以中文的网页用作ansi编码而老外的网页常用utf8编码。
首先介绍两个函数:
MultiByteToWideChar
函数功能:该函数映射一个字符串到一个宽字符(unicode)的字符串。由该函数映射的字符串没必要是多字节字符组。
函数原型:int MultiByteToWideChar(UINT CodePage, DWORD dwFlags, LPCSTR lpMultiByteStr, int cchMultiByte, LPWSTR lpWideCharStr, int cchWideChar);
WideCharToMultiByte
函数功能:该函数映射一个unicode字符串到一个多字节字符串。
函数原型:int WideCharToMultiByte(UINT CodePage, DWORD dwFlags, LPWSTR lpWideCharStr, intcchWideChar, LPCSTR lpMultiByteStr, int cchMultiByte, LPCSTR lpDefaultChar, PBOOLpfUsedDefaultChar );
Unicode 在VC转换为UTF-8如下:
wchar_t *wcs0=L"Hello 李磊!";
int wcsLen0 = ::WideCharToMultiByte(CP_UTF8, NULL, wcs0, wcslen(wcs0), NULL, 0, NULL,NULL);
char* sz0 = new char[wcsLen0 + 1];
::WideCharToMultiByte(CP_UTF8, NULL, wcs0, wcslen(wcs0), sz0, wcsLen0, NULL, NULL);
sz0 [wcsLen0] = '/0';
转贴:http://blog.csdn.net/kesur/article/details/5432473
- VC下Unicode 到UTF-8的转换
- VC下Unicode 到UTF-8的转换
- VC下Unicode 到UTF-8的转换
- VC UTF-8转换到Unicode的函数
- MFC下Unicode到UTF-8格式的转换
- UTF-8到Unicode的编码转换
- UTF-8到Unicode的编码转换
- UTF-8到Unicode的编码转换
- Unicode到UTF-8的转换详解
- unicode到utf-8的转换
- Linux下从unicode到UTF-8的转换,含实例
- UTF-8到Unicode的编码转换(转贴)
- VC下utf8到UNICODE的编码转换
- VC下unicode与utf-8互转
- windows下utf-8和unicode的相互转换
- vc中gbk到utf-8的相互转换
- VC++版 Unicode ANSI UTF-8 GB2312 相互转换代码
- Unicode和UTF-8之间的转换
- c#上传大文件
- java web项目不能编译
- Android中Textview显示带html文本-------【HTML标签】
- c#操作excel
- Android源码目录结构
- VC下Unicode 到UTF-8的转换
- 自定义光标移动
- POJ 1039 Pipe
- asp截字符串html
- 内核中的HZ 及延迟等
- Windows下MemCache多端口安装配置
- POCO C++库学习和分析 -- 线程 (四)
- ASP操作XML数据小结
- java中的异或非运算