常用的字符转换类

本文介绍了一个名为Converter的类,该类提供了多种字符编码之间的转换方法,如ANSI到UTF-8、宽字符到多字节字符等。适用于需要进行不同字符集转换的应用场景。

//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';
 }
}
















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

vqt5_qt6

你的鼓励是我们创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值