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

时间:2022-10-17 15:34:08

代码地址

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。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// 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
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//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
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//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++实现编码转换的资料请关注服务器之家其它相关文章!

原文链接:https://cloud.tencent.com/developer/article/1604693