当前位置:   article > 正文

MFC Unicode类型函数转化_mfc unicode转gbk

mfc unicode转gbk

(1)头文件

#pragma once


class CStringUtil
{
public:


/*
*  int 转化为string
*/
static string IntToString(int iSour);


/*
*  string 转化为 wstring
*/
static std::wstring StringToWString(const std::string &str);


/*
*  UTF8 转化为 Encode
*/
static char* UTF8ToEncode(const char* mbcsStr);


/*
*  Encode 转化为 UTF8
*/
static char* EncodeToUTF8(const char* mbcsStr);


static bool WStringToString(const wstring &wstr, string &str);


/*
*  char* 转化为 TCHAR*
*/
static TCHAR *CStringUtil::CHAR2TCHAR(char *ch);


static void ConvertUtf8ToGBK(std::string&, std::string strUtf8);
static void ConvertGBKToUtf8(std::string& amp, std::string strGBK);
};



(2)实现文件

#include "stdafx.h"

#include "CStringUtil.h"
#include <sstream>
using namespace std;




string CStringUtil::IntToString(int iSour)
{
std::stringstream ss;
ss << iSour;
string s = ss.str();
return s;
}


//wstring高字节不为0,返回FALSE
std::wstring CStringUtil::StringToWString(const std::string &str)
 {    
wstring wstr;
     int nLen = (int)str.length();    
     wstr.resize(nLen,L'');
     MultiByteToWideChar(CP_ACP,0,(LPCSTR)str.c_str(),nLen,(LPWSTR)wstr.c_str(),nLen);
return wstr;
 }


char* CStringUtil::EncodeToUTF8(const char* mbcsStr) 

wchar_t*  wideStr; 
char*   utf8Str; 
int   charLen;


charLen = MultiByteToWideChar(CP_UTF8, 0, mbcsStr, -1, NULL, 0); 
wideStr = (wchar_t*) malloc(sizeof(wchar_t)*charLen); 
MultiByteToWideChar(CP_ACP, 0, mbcsStr, -1, wideStr, charLen);


charLen = WideCharToMultiByte(CP_UTF8, 0, wideStr, -1, NULL, 0, NULL, NULL);


utf8Str = (char*) malloc(charLen);


WideCharToMultiByte(CP_UTF8, 0, wideStr, -1, utf8Str, charLen, NULL, NULL);


free(wideStr); 
return utf8Str;



char* CStringUtil::UTF8ToEncode(const char* mbcsStr)
{
wchar_t*  wideStr; 
char*   unicodeStr; 
int   charLen;


charLen = MultiByteToWideChar(CP_UTF8, 0, mbcsStr, -1, NULL, 0);   
wideStr = (wchar_t*) malloc(sizeof(wchar_t)*charLen); 
MultiByteToWideChar(CP_UTF8, 0, mbcsStr, -1, wideStr, charLen); 


charLen =WideCharToMultiByte(CP_ACP, 0, wideStr, -1, NULL, 0, NULL, NULL);  
unicodeStr = (char*)malloc(charLen);
WideCharToMultiByte(CP_ACP, 0, wideStr, -1, unicodeStr, charLen, NULL, NULL); 


free(wideStr); 
return unicodeStr;
}




bool CStringUtil::WStringToString(const wstring &wstr, string &str)
{    
int nLen = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)wstr.c_str(), -1, NULL, 0, NULL, NULL);
if (nLen<= 0) return false;


char* pszDst = new char[nLen];
if (NULL == pszDst) return false;


WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)wstr.c_str(), -1, pszDst, nLen, NULL, NULL);
pszDst[nLen -1] = 0;
str = pszDst;
delete [] pszDst;


return true; 
}


TCHAR *CStringUtil::CHAR2TCHAR(char *ch)
{
TCHAR Name[100];
#ifdef UNICODE
MultiByteToWideChar(CP_ACP, 0, ch, -1, Name, 100);
#else
strcpy(Name, ch);
#endif
return Name;
}


void CStringUtil::ConvertGBKToUtf8(std::string& amp, std::string strGBK)

int len=MultiByteToWideChar(CP_ACP, 0, (LPCSTR)strGBK.c_str(), -1, NULL,0); 
unsigned short * wszUtf8 = new unsigned short[len+1]; 
memset(wszUtf8, 0, len * 2 + 2); 
MultiByteToWideChar(CP_ACP, 0, (LPCSTR)strGBK.c_str(), -1, (LPWSTR)wszUtf8, len); 
len = WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR)wszUtf8, -1, NULL, 0, NULL, NULL); 
char *szUtf8=new char[len + 1]; 
memset(szUtf8, 0, len + 1); 
WideCharToMultiByte (CP_UTF8, 0, (LPCWSTR)wszUtf8, -1, szUtf8, len, NULL,NULL); 
amp=szUtf8;
delete[] szUtf8; 
delete[] wszUtf8; 
}


void CStringUtil::ConvertUtf8ToGBK(std::string&amp, std::string strUtf8)

int len=MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)strUtf8.c_str(), -1, NULL,0); 
unsigned short * wszGBK = new unsigned short[len+1]; 
memset(wszGBK, 0, len * 2 + 2); 
MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)strUtf8.c_str(), -1, (LPWSTR)wszGBK, len); 
len = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)wszGBK, -1, NULL, 0, NULL, NULL); 
char *szGBK=new char[len + 1]; 
memset(szGBK, 0, len + 1); 
WideCharToMultiByte (CP_ACP, 0, (LPCWSTR)wszGBK, -1, szGBK, len, NULL,NULL); 
//strUtf8 = szGBK; 
amp=szGBK;
delete[] szGBK; 
delete[] wszGBK; 
}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/123106
推荐阅读
相关标签
  

闽ICP备14008679号