MFC Unicode类型函数转化
来源:互联网 发布:windows有必要更新吗 编辑:程序博客网 时间:2024/05/29 19:08
(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&, 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;
}
0 0
- MFC Unicode类型函数转化
- unicode下类型转化
- Unicode类型的CString转化为char*
- oracle 类型转化函数
- 类型转化函数
- ANSI、Unicode、UTF8相互转化的函数
- 如何将MFC程序改为UNICODE类型
- 如何将MFC程序改为UNICODE类型
- MFC CString类型转换 Unicode意识
- MFC Cstring 和int 类型的转化
- MFC Unicode字符集下 CString类型转换为char*类型
- 字符串类型的Unicode码转化为中文
- 归纳Unicode与ASCII通用类型函数
- C++ UNICODE ANSI 类型转换 常用函数
- utf8编码转化为unicode的一个精彩函数
- UCS,UNICODE和UTF-8(包括转化函数)
- MFC:UNICODE下CString与char* ,string类型
- MFC,unicode下CString类型及UTF8问题
- ReactNative小项目——(一)
- putty无法连接linux虚拟机
- QT创建与调用Dll方法
- Vue 2.0 华丽的音乐搜索播放 Demo
- C#线程间操作无效: 从不是创建控件“dataGridView1”的线程访问它
- MFC Unicode类型函数转化
- 枚举类-自定义与使用enum关键字
- Android valueAnimator和ObjectAnimator浅谈(一)
- 使用php创建一个Rest Api
- net.sf.ezmorph.bean.MorphDynaBean cannotbe cast to xxx
- 关于typedef的用法总结
- Spring MVC之处理器映射器
- 站点收集
- 扩展Robot Framework,实现失败用例自动再执行(失败重跑)