常用的字符转换类

100 阅读2分钟

//converter.h文件

/* / */
/*!
@file    Converter.h
@author  chen
@date    2017/2
@brief   The file statement   the Converter class
@par     History
@verbatim
<author>   <time>   <version>  <desc>
chen     2017/02   0.1.0     build this module
@endverbatim
*/
/* / */
#ifndef  CONVERTER__HXX_
#define  CONVERTER__HXX_
namespace FcDicom
{
class Converter
{
public:
Converter();
virtual~Converter();
void wcharToChar(const wchar_t *wchr, char *chr);
void charToWchar(const char *chr, wchar_t *wchr);
char *ansiToUtf8(const char* buf);
wchar_t *ansiToUnicode(const char* buf);
char *unicodeToUtf8(const wchar_t *buf);
char *unicodeToAnsi(const wchar_t* buf);
wchar_t *utf8ToUnicode(const char* buf);
void utf8ToUnicode(const char* m_src, wchar_t *des);
protected:
void release();
private:
char *m_Ansi;
char *m_Utf8;
wchar_t *m_Unicode;
};
}
#endif

//converter.cpp文件

/* / */
/*!
@file    Converter.cpp
@author  chen
@date    2017/2
@brief   The file define   the Converter class
@par     History
@verbatim
<author>   <time>   <version>  <desc>
chen     2017/02   0.1.0     build this module
@endverbatim
*/
/* / */
#include "stdafx.h"
#include "Converter.h"
namespace FcDicom
{
Converter::Converter()
{
this->m_Ansi = NULL;
this->m_Utf8 = NULL;
this->m_Unicode = NULL;
}
Converter::~Converter()
{
this->release();
}
void Converter::release()
{
if (this->m_Ansi)
{
delete this->m_Ansi;
this->m_Ansi = NULL;
}
if (this->m_Utf8)
{
delete this->m_Utf8;
this->m_Utf8 = NULL;
}
if (this->m_Unicode)
{
delete this->m_Unicode;
this->m_Unicode = NULL;
}
}

 void Converter::wcharToChar(const wchar_t *wchr, char *chr)
{
// WideCharToMultiByte 返回长度不包括字符串终止符('\0')
int len = WideCharToMultiByte(CP_ACP, 0, wchr, wcslen(wchr), NULL, 0, NULL, NULL);
if (0 == len)
{
chr[len] = '\0';
return;
}
WideCharToMultiByte(CP_ACP, 0, wchr, wcslen(wchr), chr, len, NULL, NULL);
chr[len] = '\0';
}
// char* 转换为 wchar_t*
void Converter::charToWchar(const char *chr, wchar_t *wchr)
{
// MultiByteToWideChar 返回长度不包括字符串终止符('\0')
int len = MultiByteToWideChar(CP_ACP, 0, chr, strlen(chr), NULL, 0);
if (0 == len)
{
wchr[len] = '\0';
return;
}
MultiByteToWideChar(CP_ACP, 0, chr, strlen(chr), wchr, len);
wchr[len] = '\0';
}

 //-------------------------------------------------------------------------
char* Converter::ansiToUtf8(const char* buf)
{
this->release();
int len = ::MultiByteToWideChar(CP_ACP, 0, buf, -1, NULL, 0);
if (len == 0) { return ""; }
this->m_Unicode = new wchar_t[len];
::MultiByteToWideChar(CP_ACP, 0, buf, -1, this->m_Unicode, len);
len = ::WideCharToMultiByte(CP_UTF8, 0, this->m_Unicode, -1, NULL, 0, NULL, NULL);
if (len == 0) { return ""; }
this->m_Utf8 = new char[len + 1];
::WideCharToMultiByte(CP_UTF8, 0, this->m_Unicode, -1, this->m_Utf8, len, NULL, NULL);
this->m_Utf8[len] = '\0';
return this->m_Utf8;
}
//-------------------------------------------------------------------------
wchar_t* Converter::ansiToUnicode(const char* buf)
{
this->release();
int len = ::MultiByteToWideChar(CP_ACP, 0, buf, -1, NULL, 0);
if (len == 0) { return L""; }
this->m_Unicode = new wchar_t[len + 1];
::MultiByteToWideChar(CP_ACP, 0, buf, -1, this->m_Unicode, len);
this->m_Unicode[len] = '\0';
return this->m_Unicode;
}
//-------------------------------------------------------------------------
char * Converter::unicodeToUtf8(const wchar_t *buf)
{
this->release();
int len = ::WideCharToMultiByte(CP_UTF8, 0, buf, -1, NULL, 0, NULL, NULL);
if (len == 0) { return ""; }
this->m_Utf8 = new char[len + 1];
::WideCharToMultiByte(CP_UTF8, 0, buf, -1, this->m_Utf8, len, NULL, NULL);
this->m_Utf8[len] = '\0';
return this->m_Utf8;
}
//-------------------------------------------------------------------------
char * Converter::unicodeToAnsi(const wchar_t* buf)
{
this->release();
int len = ::WideCharToMultiByte(CP_ACP, 0, buf, -1, NULL, 0, NULL, NULL);
if (len == 0) { return ""; }
this->m_Ansi = new char[len + 1];
::WideCharToMultiByte(CP_ACP, 0, buf, -1, this->m_Ansi, len, NULL, NULL);
this->m_Ansi[len] = '\0';
return this->m_Ansi;
}
//-------------------------------------------------------------------------
wchar_t * Converter::utf8ToUnicode(const char* buf)
{
this->release();
int len = ::MultiByteToWideChar(CP_UTF8, 0, buf, -1, NULL, 0);
if (len == 0) { return L""; }
this->m_Unicode = new wchar_t[len + 1];
::MultiByteToWideChar(CP_UTF8, 0, buf, -1, this->m_Unicode, len);
this->m_Unicode[len] = '\0';
return this->m_Unicode;
}
//-------------------------------------------------------------------------
void Converter::utf8ToUnicode(const char* m_src, wchar_t *des)
{
int len = ::MultiByteToWideChar(CP_UTF8, 0, m_src, -1, NULL, 0);
if (len == 0) { return; }
::MultiByteToWideChar(CP_UTF8, 0, m_src, -1, des, len);
des[len] = '\0';
}
}

\

\

\

\

\

\

\

\

\

\

\

\

\

\