Win32字符串编码格式转化
来源:互联网 发布:海贼王 知乎 编辑:程序博客网 时间:2024/06/14 14:29
1.ascii转unicode
wstring asciiToUnicode(string asciiStr){ int widesize = MultiByteToWideChar(CP_ACP, 0, (char*)asciiStr.c_str(), -1, NULL, 0); if(0 == widesize) { return std::wstring(); } std::vector<wchar_t> resultstring(widesize); int count = MultiByteToWideChar(CP_ACP, 0, (char*)asciiStr.c_str(), -1, &resultstring[0], widesize); if(0 == count) { return std::wstring(); } return std::wstring(&resultstring[0]);}
2.utf8转unicode
std::wstring utf8ToUnicode(const std::string &s){ if (s.length() == 0) { return wstring(); } // compute the length of the buffer we'll need int charcount = MultiByteToWideChar(CP_UTF8, 0, s.c_str(), -1, NULL, 0); if (charcount == 0) { return wstring(); } // convert wchar_t* buf = new wchar_t[charcount]; MultiByteToWideChar(CP_UTF8, 0, s.c_str(), -1, buf, charcount); wstring result(buf); delete[] buf; return result;}
3.unicode 转ascii
string unicodeToAscii(wstring unicodeStr){ // compute the length of the buffer we'll need int charcount = WideCharToMultiByte(CP_ACP, 0, unicodeStr.c_str(), -1, NULL, 0, NULL, NULL); if (charcount == 0) { return string(); } // convert char *buf = new char[charcount]; WideCharToMultiByte(CP_ACP, 0, unicodeStr.c_str(), -1, buf, charcount, NULL, NULL); string result(buf); delete[] buf; return result;}
4.unicode转utf8
string unicodeToUtf8(wstring unicodeStr){ int utf8size = ::WideCharToMultiByte(CP_UTF8, 0, unicodeStr.c_str(), -1, NULL, 0, NULL, NULL); if(0 == utf8size) { return std::string(); } std::vector<char> resultstr(utf8size); int count = ::WideCharToMultiByte(CP_UTF8, 0, unicodeStr.c_str(), -1, &resultstr[0], utf8size, NULL, NULL); if(0 == count) { return std::string(); } return std::string(&resultstr[0]);}
5.ascii转utf8
string PublicFunction::asicToUtf8(string asic){ return unicodeToUtf8(asciiToUnicode(asic));}
6.utf8转ascii
string utf8ToAsic(string utf8){ return unicodeToAscii(utf8ToUnicode(utf8));}
总结:转编码格式转化的时候,如果转化的双方不是unicode编码,那么一定要通过unicode做中转,也就是说一定要先转化成unicode,然后再转化成目标编码。
转化时怎样决定编码(使用CP_UTF8还是CP_ACP),只要是转unicode,那么如果是utf8转unicode(或者unicode转utf8),那么肯定用CP_UTF8;
如果是ascii转unicode(或者unicode转ascii),那么肯定用CP_ACP。
0 0
- Win32字符串编码格式转化
- win32 编码转化函数
- 将字符串转化为指定编码格式的字节
- QT程序中把一个字符串编码格式转化…
- webkit 编码格式转化
- 字符串格式转化
- 字符串格式转化
- iOS 基本编码格式转化
- iOS 基本编码格式转化
- linux文本编码格式转化
- iOS 基本编码格式转化
- iOS 基本编码格式转化
- iOS 基本编码格式转化
- 快速转化文档编码格式
- iOS 基本编码格式转化
- 对字符串进行编码转化
- 字符串转化为unicode编码
- 转化字符串的编码方式
- c语言 可变参数 三个点 ...
- Linux互斥锁和条件变量
- AC自动机模板
- 4Sum、3Num
- Textview设置不同样式
- Win32字符串编码格式转化
- Zendstuido汉化方法
- 如何安装和配置Mantis
- 杭电5620
- Java跨域请求操作---封装类
- 【SQL】 DROP DELATE区别
- 线段树专题#1_蒟蒻训练历程记录_HDU1166
- java中图片简单操作
- JAVA API 自带图片压缩