Windows平台下字符串转换(不全)
来源:互联网 发布:mac系统如何回到桌面 编辑:程序博客网 时间:2024/05/01 20:41
鄙人在工作中整理的字符转换方法,分享给大家
头文件:MyCharsConvert.h
#pragma once#include <string>class MyCharsConvert{public:MyCharsConvert(void);public:~MyCharsConvert(void);public:// ansi characters convert to unicode 16static std::wstring ANSIToUTF16(std::string AnsiChars);// utf16 to ansistatic std::string UTF16ToANSI(std::wstring Utf16Chars);// utf-8 to ansistatic std::string UTF8ToANSI(std::string utf8Chars);// ansi to utf-8static std::string ANSIToUTF8(std::string ansiChars);private:char* Convert(const char* strIn, int sourceCodepage, int targetCodepage);};
#include "StdAfx.h"#include <windows.h>#include "MyCharsConvert.h"MyCharsConvert::MyCharsConvert(void){}MyCharsConvert::~MyCharsConvert(void){}// ansi characters convert to unicode 16std::wstring MyCharsConvert::ANSIToUTF16(std::string ansiChars){//入参检查if (ansiChars.empty()) return std::wstring();wchar_t *pszBuf = NULL;//利用WideCharToMultiByte方法获取需要的大小int needWChar = MultiByteToWideChar(CP_ACP, 0, ansiChars.c_str(), -1, NULL, 0);if (needWChar > 0){pszBuf = new wchar_t[needWChar+1];if (!pszBuf){return std::wstring();}ZeroMemory(pszBuf, (needWChar+1)*sizeof(wchar_t));//窄字符到宽字符的转化MultiByteToWideChar(CP_ACP, 0, ansiChars.c_str(), -1, pszBuf, needWChar);}std::wstring wstrRet(pszBuf);delete[] pszBuf;pszBuf = NULL;return wstrRet;}std::string MyCharsConvert::UTF16ToANSI(std::wstring utf16Chars){//入参检查if (utf16Chars.empty()) return std::string();char *pszBuf = NULL;//利用WideCharToMultiByte方法获取需要的大小int needBytes = WideCharToMultiByte(CP_ACP, 0, utf16Chars.c_str(), -1, NULL, 0, NULL, NULL);if (needBytes > 0){pszBuf = new char[needBytes+1];if (!pszBuf){return std::string("");}ZeroMemory(pszBuf, (needBytes+1)*sizeof(char));//宽字符到窄字符的转化WideCharToMultiByte(CP_ACP, 0, utf16Chars.c_str(), -1, pszBuf, needBytes, NULL, NULL);}std::string strRet(pszBuf);delete[] pszBuf;pszBuf = NULL;return strRet;}// utf-8 to ansistd::string MyCharsConvert::UTF8ToANSI(std::string utf8Chars){char* pszRet = Convert(utf8Chars.c_str(), CP_UTF8, CP_ACP); if (NULL == pszRet){return std::string("");}std::string strRet(pszRet);delete[] pszRet;pszRet = NULL;return strRet;}// ansi to utf-8std::string MyCharsConvert::ANSIToUTF8(std::string ansiChars){char* pszRet = Convert(ansiChars.c_str(),CP_ACP,CP_UTF8); if (NULL == pszRet){return std::string("");}std::string strRet(pszRet);delete[] pszRet;pszRet = NULL;return strRet;}/***@brief 字符转换*利用线程ThreadFunc来获取POST请求返回的数据*@param [in] sourceCodepage输入strIn的字符集*@param [in] targetCodepage输出目标的字符集*@return strIn对应的targetCodepage字符串*@warning Convert返回值是char*,需要调用者用完返回的结果后,手动delete Convert的返回值*/char* MyCharsConvert::Convert(const char* strIn, int sourceCodepage, int targetCodepage){///1.获取strIn的字符长度int len=strlen(strIn);///2.获取strIn字符到unicode字符集所需要的长度int unicodeLen=MultiByteToWideChar(sourceCodepage,0,strIn,-1,NULL,0);wchar_t* pUnicode = NULL;///3.申请对应长度的wchar_t字符串pUnicode=new wchar_t[unicodeLen+1];if (NULL == pUnicode){return NULL;}memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));///4.将strIn转换成Unicode字符集MultiByteToWideChar(sourceCodepage,0,strIn,-1,(LPWSTR)pUnicode,unicodeLen);BYTE * pTargetData = NULL;///5.获取从Unicode字符集到targetCodepage所需要的长度int targetLen=WideCharToMultiByte(targetCodepage,0,(LPWSTR)pUnicode,-1,(char *)pTargetData,0,NULL,NULL);///6.申请对应长度的内存pTargetData=new BYTE[targetLen+1];if (NULL == pTargetData){return NULL;}memset(pTargetData,0,targetLen+1);///7.实现到targetCodepage字符集的转换WideCharToMultiByte(targetCodepage,0,(LPWSTR)pUnicode,-1,(char *)pTargetData,targetLen,NULL,NULL);//strcpy(strOut,(char*)pTargetData);delete[] pUnicode;///8.返回转换的结果return (char*)pTargetData;}
- Windows平台下字符串转换(不全)
- C++ Windows平台字符串编码转换
- c++多字节与宽字节字符串转换(windows平台)
- c++多字节与宽字节字符串转换(windows平台)
- c++多字节与宽字节字符串转换(windows平台)
- 在windows平台下php使用imagick库把pdf转换成(合并)图片
- 在windows平台下UTF8和GB232之间的转换
- windows平台下GBK与UTF8的编码转换
- Windows/Linux双平台下OpenGL字符串输出的实现(使用QuesoGLC库)
- Windows Sockets:转换字符串
- Windows平台下安装Hadoop(转)
- Memcached(1)------windows平台下安装
- WINDOWS下的ANSI字符串和UTF8字符串之间的相互转换
- Windows平台下另一种将char*转换为wchar_t*的方法
- Windows平台下另一种将wchar_t*转换为char*的方法
- Windows平台下搭建Obcject-C平台
- windows平台下编译stunnel
- WIndows平台下使用CTex
- Hadoop源码包结构<转>
- JS报表控件highcharts应用
- 黑马程序员 String字符串
- Android中的ImageView的scaleType属性详解
- Ext.MessageBox.show()方法的使用
- Windows平台下字符串转换(不全)
- 使用 awk&cut 进行字符文本处理
- Qt4.8.4编译X86出错:/usr/include/linux/futex.h:96: 错误:‘u32’ 在此作用域中尚未声明
- FTP服务器和客户端源代码编写问题(ftp server client source)
- mount ubifs
- 面试必备的9大成功秘诀
- C 语言内存对齐
- 监听总结之4.基本概念的区别和联系
- 对textView设置placeholder