文章目录
- 字符编码问题
- 编码转换问题
- ANSI转Unicode
- Unicode转ANSI
- Utf8转 ANSI
- utf8 转Unicode
- ANSI 转UTF-8
- Unicode 转 UTF-8
- 全部代码
字符编码问题
Windows API 函数
MessageBoxA:MessageBox 内部实现,字符串编码(ANSI)转换成了Unicode,在调用MessageboxW
MessageBox:是一个宏定义
MessageBoxA(NULL,"Hello","提示",MB_OK);
MessageBoxW(NULL,L"Hello",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 dwSize=WideCharToMultiByte(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 转Unicode
m_wstr= Utf8ToUnicode(str);
//Unicode 转ANSI
m_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 dwSize=MultiByteToWideChar(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 转Unicode
m_wstr= AnsiToUnicode(str);
//Unicode 转UTF-8
m_utf8=UnicodeToUtf8(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 dwSize=WideCharToMultiByte(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转Unicode
wchar_t* AnsiToUnicode(const char* str);
//Unicode转ANSI
char* UnicodeToAnsi(const wchar_t* wstr);
//UTF8 转ANSI
char* Utf8ToAnsi(const char* str);
//ANSI转UTF - 8
char* AnsitoUtf8(const char* str);
//Unicode 转 UTF-8
char* UnicodeToUtf8(const wchar_t* wstr);
//UTF-8转Unicode
wchar_t* Utf8ToUnicode(const char* str);
};
CCharset.cpp
#include "pch.h"
#include "CCharset.h"
CCharset::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 dwSize=WideCharToMultiByte(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 转Unicode
m_wstr= Utf8ToUnicode(str);
//Unicode 转ANSI
m_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 转Unicode
m_wstr= AnsiToUnicode(str);
//Unicode 转UTF-8
m_utf8=UnicodeToUtf8(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 dwSize=WideCharToMultiByte(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 dwSize=MultiByteToWideChar(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;
}