电子商务网站建设论文,wordpress搭建个人店铺,内网建站工具,网站搜索引擎优化方案文章目录 字符编码问题编码转换问题ANSI转UnicodeUnicode转ANSIUtf8转 ANSIutf8 转UnicodeANSI 转UTF-8Unicode 转 UTF-8 全部代码 字符编码问题 Windows API 函数
MessageBoxA:MessageBox 内部实现#xff0c;字符串编码(ANSI)转换成了Unicode,在调用MessageboxW
MessageBox:… 文章目录 字符编码问题编码转换问题ANSI转UnicodeUnicode转ANSIUtf8转 ANSIutf8 转UnicodeANSI 转UTF-8Unicode 转 UTF-8 全部代码 字符编码问题 Windows API 函数
MessageBoxA:MessageBox 内部实现字符串编码(ANSI)转换成了Unicode,在调用MessageboxW
MessageBox:是一个宏定义
MessageBoxA(NULL,Hello,提示,MB_OK);
MessageBoxW(NULL,LHello,L提示,MB_OK);编码转换问题
ANSI转Unicode
//ANSI转Unicode
wchar_t* CCharset::AnsiToUnicode(const char* str)
{if (m_wstr)//安全{delete m_wstr;m_wstr NULL;}DWORD dwSize::MultiByteToWideChar(CP_ACP,0,str,-1,NULL,0);//求宽字符的大小m_wstr new wchar_t[dwSize];::MultiByteToWideChar(CP_ACP, 0, str, -1, m_wstr, dwSize);return m_wstr;
}Unicode转ANSI
//Unicode转ANSI
char * CCharset::UnicodeToAnsi(const wchar_t * wstr)
{if (m_str){delete m_str;m_str NULL;}DWORD dwSizeWideCharToMultiByte(CP_ACP, 0, wstr, -1,NULL,0,NULL,NULL);m_str new char[dwSize];::WideCharToMultiByte(CP_ACP, 0, wstr, -1, m_str, dwSize, NULL, NULL);return m_str;
}Utf8转 ANSI
char * CCharset::Utf8ToAnsi(const char * str)
{if (m_wstr){delete[] m_wstr;m_wstr NULL;}if (m_str){delete[] m_str;m_str NULL;}//UTF-8 转Unicodem_wstr Utf8ToUnicode(str);//Unicode 转ANSIm_str UnicodeToAnsi(m_wstr);return m_str;
}utf8 转Unicode
wchar_t* CCharset::Utf8ToUnicode(const char * str)
{if (m_wstr){delete m_wstr;m_wstr NULL;}DWORD dwSizeMultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0);m_wstr new wchar_t[dwSize];memset(m_wstr, 0, dwSize*sizeof(wchar_t));//清空内存MultiByteToWideChar(CP_UTF8, 0, str, -1, m_wstr, dwSize);return m_wstr;
}ANSI 转UTF-8
char* CCharset::AnsitoUtf8(const char* str)
{if (m_wstr){delete[] m_wstr;m_wstr NULL;}if (m_utf8){delete[] m_utf8;m_utf8 NULL;}//Ansi 转Unicodem_wstr AnsiToUnicode(str);//Unicode 转UTF-8m_utf8UnicodeToUtf8(m_wstr);return m_utf8;
}Unicode 转 UTF-8
char * CCharset::UnicodeToUtf8(const wchar_t * wstr)
{if (m_utf8){delete[] m_utf8;m_utf8 NULL;}DWORD dwSizeWideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);m_utf8 new char[dwSize];memset(m_utf8,0,dwSize);//清空内存WideCharToMultiByte(CP_UTF8, 0, wstr, -1, m_utf8, dwSize, NULL, NULL);return m_utf8;
}全部代码 CCharset.h #pragma once
class CCharset
{private:wchar_t* m_wstr;char* m_str;char* m_utf8;
public:CCharset();~CCharset();//ANSI转Unicodewchar_t* AnsiToUnicode(const char* str);//Unicode转ANSIchar* UnicodeToAnsi(const wchar_t* wstr);//UTF8 转ANSIchar* Utf8ToAnsi(const char* str);//ANSI转UTF - 8char* AnsitoUtf8(const char* str);//Unicode 转 UTF-8char* UnicodeToUtf8(const wchar_t* wstr);//UTF-8转Unicodewchar_t* Utf8ToUnicode(const char* str);};CCharset.cpp #include pch.h
#include CCharset.hCCharset::CCharset()
{m_wstr NULL;m_str NULL;m_utf8 NULL;
}CCharset::~CCharset()
{if (m_wstr){delete m_wstr;m_wstr NULL;}if (m_str){delete m_str;m_str NULL;}if (m_utf8){delete[] m_utf8;m_utf8 NULL;}
}//ANSI转Unicode
wchar_t* CCharset::AnsiToUnicode(const char* str)
{if (m_wstr)//安全{delete[] m_wstr;m_wstr NULL;}DWORD dwSize::MultiByteToWideChar(CP_ACP,0,str,-1,NULL,0);//求宽字符的大小m_wstr new wchar_t[dwSize];::MultiByteToWideChar(CP_ACP, 0, str, -1, m_wstr, dwSize);return m_wstr;
}//Unicode转ANSI
char * CCharset::UnicodeToAnsi(const wchar_t * wstr)
{if (m_str){delete[] m_str;m_str NULL;}DWORD dwSizeWideCharToMultiByte(CP_ACP, 0, wstr, -1,NULL,0,NULL,NULL);//求字符的大小m_str new char[dwSize];::WideCharToMultiByte(CP_ACP, 0, wstr, -1, m_str, dwSize, NULL, NULL);return m_str;
}char * CCharset::Utf8ToAnsi(const char * str)
{if (m_wstr){delete[] m_wstr;m_wstr NULL;}if (m_str){delete[] m_str;m_str NULL;}//UTF-8 转Unicodem_wstr Utf8ToUnicode(str);//Unicode 转ANSIm_str UnicodeToAnsi(m_wstr);return m_str;
}char* CCharset::AnsitoUtf8(const char* str)
{if (m_wstr){delete[] m_wstr;m_wstr NULL;}if (m_utf8){delete[] m_utf8;m_utf8 NULL;}//Ansi 转Unicodem_wstr AnsiToUnicode(str);//Unicode 转UTF-8m_utf8UnicodeToUtf8(m_wstr);return m_utf8;
}//Unicode 转 UTF-8
char * CCharset::UnicodeToUtf8(const wchar_t * wstr)
{if (m_utf8){delete[] m_utf8;m_utf8 NULL;}DWORD dwSizeWideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);m_utf8 new char[dwSize];memset(m_utf8,0,dwSize);//清空内存WideCharToMultiByte(CP_UTF8, 0, wstr, -1, m_utf8, dwSize, NULL, NULL);return m_utf8;
}//utf8 转Unicode
wchar_t* CCharset::Utf8ToUnicode(const char * str)
{if (m_wstr){delete m_wstr;m_wstr NULL;}DWORD dwSizeMultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0);m_wstr new wchar_t[dwSize];memset(m_wstr, 0, dwSize*sizeof(wchar_t));//清空内存MultiByteToWideChar(CP_UTF8, 0, str, -1, m_wstr, dwSize);return m_wstr;
}