常用的字符转换类
来源:互联网 发布:kafka java获取offset 编辑:程序博客网 时间:2024/05/21 18:40
//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';
}
}
- 常用的字符转换类
- 一些常用的字符转换
- 常用的js,字符截取,时间转换
- 常用的字符编码转换集合
- c++ 常用的一些字符转换
- html常用字符转换
- C常用字符转换函数
- 常用字符编码转换函数
- 字符转换常用函数总结
- MFC常用字符编码转换
- j2me常用的字符,日期,以及转换编码实现
- j2me常用的字符,日期,以及转换编码实现
- j2me常用的字符,日期,以及转换编码实现
- Windows编程中一些常用的字符(串)类型转换
- Windows下常用的几种字符转换
- char字符与wchar_t字符的相互转换,以及wchar_t字符串的常用用法
- 进行字符操作的工具类!转换字符
- string类中字符的大小写转换
- QT生成DLL,调用DLL
- Android之EditText禁止回车键换行
- Java For-each 和 Iterator异同点总结
- ubuntu在桌面添加应用的快捷方式
- Javascript定义基础类
- 常用的字符转换类
- 学习理解实现Servlet的三种方式
- 【caffe学习笔记】caffe训练日志输出
- 如何从esclipe中快速找到web项目的所需文件
- java web 面试一般 问题总结
- 文章标题
- 定位IO瓶颈的一些方法(iotop工具具体查看IO负载主要是落在哪个进程上)
- red5流媒体服务器
- java排序算法(4)—冒泡排序