当前位置: 移动技术网 > IT编程>开发语言>C/C++ > C++实现编码转换的示例代码

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

2020年08月15日  | 移动技术网IT编程  | 我要评论
代码地址https://github.com/gongluck/code-snippet/tree/master/cpp/code%20conversion需求编码转换在实际开发中经常遇到,通常是an

代码地址

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++实现编码转换的资料请关注移动技术网其它相关文章!

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网