C++实现编码转换的示例代码

 更新时间:2020年08月14日 11:45:59   作者:gongluck  
这篇文章主要介绍了C++实现编码转换的示例代码,帮助大家快捷的实现编码转换,感兴趣的朋友可以了解下

代码地址

https://github.com/gongluck/Code-snippet/tree/master/cpp/code%20conversion

需求

编码转换在实际开发中经常遇到,通常是ANSI、Unicode和Utf-8之间相互转换。实现也有很多种,有查表法、使用C++11、使用boost、使用系统API。C++11和boost几乎可以实现一套代码,在linux和windows都能使用,但实际会有很多坑,相当于代码几乎不改,但是要改一下系统环境。所以有一种实现就是判断系统的版本,然后选择不同的系统api进行编码转换。

实现

目前只实现Windows下的编码转换,以后需要在linux下使用编码转换再做补充。windows下的编码转换基本围绕unicode做处理。例如ANSI->UTF-8,就是先将ANSI->unicode,再将unicode->UTF-8。

// convert.h
/*
 * @Author: gongluck 
 * @Date: 2020-03-23 16:06:23 
 * @Last Modified by: gongluck
 * @Last Modified time: 2020-03-23 16:09:30
 */

// Character encoding conversion

#pragma once

#include <string>

namespace gconvert
{
// ANSI->Unicode
int ansi2uni(const std::string& ansi, std::wstring& uni);

// Unicode->ANSI
int uni2ansi(const std::wstring& uni, std::string& ansi);

// UTF8->Unicode
int utf82uni(const std::string& utf8, std::wstring& uni);

// Unicode->UTF8
int uni2utf8(const std::wstring& uni, std::string& utf8);

// ANSI->UTF8
int ansi2utf8(const std::string& ansi, std::string& utf8);

// UTF8->ANSI
int utf82ansi(const std::string& utf8, std::string& ansi);
} // namespace gconvert
//convert.cpp
/*
 * @Author: gongluck 
 * @Date: 2020-03-23 16:13:01 
 * @Last Modified by: gongluck
 * @Last Modified time: 2020-03-23 16:34:50
 */

#include "convert.h"

#include <iostream>

#ifdef _WIN32
#include <windows.h>
#endif

namespace gconvert
{
#ifdef _WIN32
 static int multi2uni(const std::string& multi, std::wstring& uni, UINT code)
 {
  auto len = MultiByteToWideChar(code, 0, multi.c_str(), -1, nullptr, 0);
  if (len <= 0)
  {
   std::cerr << __FILE__ << " : " << __LINE__ << " : " << GetLastError() << std::endl;
   return -1;
  }
  WCHAR* buf = new WCHAR[len];
  if (buf == nullptr)
  {
   std::cerr << __FILE__ << " : " << __LINE__ << " : " << "can not new buf, size : " << len << std::endl;
   return -2;
  }
  len = MultiByteToWideChar(code, 0, multi.c_str(), -1, buf, len);
  uni.assign(buf);
  delete[]buf;
  buf = nullptr;
  return len;
 }

 static int uni2multi(const std::wstring& uni, std::string& multi, UINT code)
 {
  auto len = WideCharToMultiByte(code, 0, uni.c_str(), -1, nullptr, 0, nullptr, nullptr);
  if (len <= 0)
  {
   std::cerr << __FILE__ << " : " << __LINE__ << " : " << GetLastError() << std::endl;
   return -1;
  }
  CHAR* buf = new CHAR[len];
  if (buf == nullptr)
  {
   std::cerr << __FILE__ << " : " << __LINE__ << " : " << "can not new buf, size : " << len << std::endl;
   return -2;
  }
  len = WideCharToMultiByte(code, 0, uni.c_str(), -1, buf, len, nullptr, nullptr);
  multi.assign(buf);
  delete[]buf;
  buf = nullptr;
  return len;
 }
#endif

// ANSI->Unicode
int ansi2uni(const std::string& ansi, std::wstring& uni)
{
#ifdef _WIN32
 return multi2uni(ansi, uni, CP_ACP);
#endif
 return 0;
}

// Unicode->ANSI
int uni2ansi(const std::wstring &uni, std::string &ansi)
{
#ifdef _WIN32
 return uni2multi(uni, ansi, CP_ACP);
#endif
 return 0;
}

// UTF8->Unicode
int utf82uni(const std::string& utf8, std::wstring& uni)
{
#ifdef _WIN32
 return multi2uni(utf8, uni, CP_UTF8);
#endif
 return 0;
}

// Unicode->UTF8
int uni2utf8(const std::wstring& uni, std::string& utf8)
{
#ifdef _WIN32
 return uni2multi(uni, utf8, CP_UTF8);
#endif
 return 0;
}

// ANSI->UTF8
int ansi2utf8(const std::string &ansi, std::string &utf8)
{
 std::wstring uni;
 auto len = ansi2uni(ansi, uni);
 if (len <= 0)
 {
  return -3;
 }
 return uni2utf8(uni, utf8);
}

// UTF8->ANSI
int utf82ansi(const std::string &utf8, std::string &ansi)
{
 std::wstring uni;
 auto len = utf82uni(utf8, uni);
 if (len <= 0)
 {
  return -3;
 }
 return uni2ansi(uni, ansi);
}
} // namespace gconvert
//testcode
#include <iostream>

#include "../code conversion/convert.h"

int main()
{
 std::string ansi = "你好,世界!";
 std::wstring uni;
 std::string utf8;
 ret = gconvert::ansi2uni(ansi, uni);
 ret = gconvert::ansi2utf8(ansi, utf8);
 ret = gconvert::uni2ansi(uni, ansi);
 ret = gconvert::uni2utf8(uni, utf8);
 ret = gconvert::utf82ansi(utf8, ansi);
 ret = gconvert::utf82uni(utf8, uni);
 return 0;
}

以上就是C++实现编码转换的示例代码的详细内容,更多关于C++实现编码转换的资料请关注脚本之家其它相关文章!

相关文章

  • 利用C++和QT实现Log自定义日志系统

    利用C++和QT实现Log自定义日志系统

    这篇文章主要为大家详细介绍了如何利用C++和QT实现Log自定义日志系统,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以参考下
    2023-12-12
  • C语言编程数据在内存中的存储详解

    C语言编程数据在内存中的存储详解

    本篇文章是C语言编程篇,主要为大家介绍C语言编程中数据在内存中存储解析,有需要的朋友可以借鉴参考下,希望可以有所帮助
    2021-09-09
  • C语言的Struct Hack笔记

    C语言的Struct Hack笔记

    这篇文章主要介绍了C语言的Struct Hack例子,个人的一篇笔记,需要的朋友可以参考下吧
    2014-04-04
  • c++制作的时间函数类

    c++制作的时间函数类

    本文给大家分享的是一个个人使用C++编写的时间函数类,主要是实现了类的定义和调用,相比较来说还算比较复杂的时间类了,推荐给小伙伴们,有需要的朋友可以参考下。
    2015-03-03
  • 使用boost读取XML文件详细介绍

    使用boost读取XML文件详细介绍

    这篇文章主要介绍了使用boost读取XML文件详细介绍的相关资料,需要的朋友可以参考下
    2016-11-11
  • 详解设计模式中的模板方法模式及在C++中的使用

    详解设计模式中的模板方法模式及在C++中的使用

    这篇文章主要介绍了设计模式中的模板方法模式及在C++中的使用,模板方法将逻辑封装到一个类中,并采取组合(委托)的方式解决这个问题,需要的朋友可以参考下
    2016-03-03
  • 带你分分钟玩转C语言指针

    带你分分钟玩转C语言指针

    c语言指针其实是一个整形变量,与其它数据不同的是,它的作用是用来存储其它变量的地址,下面这篇文章主要给大家介绍了关于C语言指针的相关资料,需要的朋友可以参考下
    2022-06-06
  • vc提示unexpected end of file found的原因分析

    vc提示unexpected end of file found的原因分析

    这篇文章主要介绍了vc提示unexpected end of file found的原因分析,给出了几点常见错误原因的分析,需要的朋友可以参考下
    2015-05-05
  • C++ sdl实现渲染旋转视频的方法分享

    C++ sdl实现渲染旋转视频的方法分享

    一般情况下播放视频时不需要旋转,但是如果是移动端录制的视频有时会出现rotate参数,且视频宽高也是互换的。所以本文为大家准备了利用sdl实现渲染旋转视频的方法,需要的可以参考一下
    2022-12-12
  • C++中二叉堆排序详解

    C++中二叉堆排序详解

    这篇文章主要介绍了C++中二叉堆排序详解,主要介绍了二叉堆排序(递归和非递归实现上沉、下沉算法),需要的朋友可以参考下
    2023-01-01

最新评论