-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrCoder.h
182 lines (152 loc) · 4.05 KB
/
strCoder.h
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//strCoder.h
#pragma once
#include "Winsock2.h"
#include "windows.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <locale>
#include <codecvt>
#include <string>
void string2num(std::string str, double& num) {
std::stringstream ss; ss << str; ss >> num;
}
void string2num(std::string str, int& num) {
std::stringstream ss; ss << str; ss >> num;
}
std::string StringToUTF8(const std::string& gbkData)
{
const char* GBK_LOCALE_NAME = "CHS";
//GBKÔÚwindowsϵÄlocale name(.936, CHS ), linuxϵÄlocaleÃû¿ÉÄÜÊÇ"zh_CN.GBK"
std::wstring_convert<std::codecvt<wchar_t, char, mbstate_t>>
conv(new std::codecvt<wchar_t, char, mbstate_t>(GBK_LOCALE_NAME));
std::wstring wString = conv.from_bytes(gbkData); // string => wstring
std::wstring_convert<std::codecvt_utf8<wchar_t>> convert;
std::string utf8str = convert.to_bytes(wString); // wstring => utf-8
return utf8str;
}
std::string UTF8ToString(const std::string& utf8Data)
{
std::wstring_convert<std::codecvt_utf8<wchar_t>> conv;
std::wstring wString = conv.from_bytes(utf8Data); // utf-8 => wstring
std::wstring_convert<std::codecvt< wchar_t, char, std::mbstate_t>>
convert(new std::codecvt< wchar_t, char, std::mbstate_t>("CHS"));
std::string str = convert.to_bytes(wString); // wstring => string
return str;
}
std::string UnicodeToUTF8(const std::wstring& wstr)
{
std::string ret;
try {
std::wstring_convert< std::codecvt_utf8<wchar_t> > wcv;
ret = wcv.to_bytes(wstr);
}
catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
}
return ret;
}
std::wstring UTF8ToUnicode(const std::string& str)
{
std::wstring ret;
try {
std::wstring_convert< std::codecvt_utf8<wchar_t> > wcv;
ret = wcv.from_bytes(str);
}
catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
}
return ret;
}
std::string UnicodeToANSI(const std::wstring& wstr)
{
std::string ret;
std::mbstate_t state = {};
const wchar_t* src = wstr.data();
size_t len = std::wcsrtombs(nullptr, &src, 0, &state);
if (static_cast<size_t>(-1) != len) {
std::unique_ptr< char[] > buff(new char[len + 1]);
len = std::wcsrtombs(buff.get(), &src, len, &state);
if (static_cast<size_t>(-1) != len) {
ret.assign(buff.get(), len);
}
}
return ret;
}
std::wstring ANSIToUnicode(const std::string& str)
{
std::wstring ret;
std::mbstate_t state = {};
const char* src = str.data();
size_t len = std::mbsrtowcs(nullptr, &src, 0, &state);
if (static_cast<size_t>(-1) != len) {
std::unique_ptr< wchar_t[] > buff(new wchar_t[len + 1]);
len = std::mbsrtowcs(buff.get(), &src, len, &state);
if (static_cast<size_t>(-1) != len) {
ret.assign(buff.get(), len);
}
}
return ret;
}
std::string UTF8ToANSI(const std::string& str)
{
return UnicodeToANSI(UTF8ToUnicode(str));
}
std::string ANSIToUTF8(const std::string& str)
{
return UnicodeToUTF8(ANSIToUnicode(str));
}
unsigned char ToHex(unsigned char x)
{
return x > 9 ? x + 55 : x + 48;
}
unsigned char FromHex(unsigned char x)
{
unsigned char y;
if (x >= 'A' && x <= 'Z') y = x - 'A' + 10;
else if (x >= 'a' && x <= 'z') y = x - 'a' + 10;
else if (x >= '0' && x <= '9') y = x - '0';
else assert(0);
return y;
}
std::string UrlEncode(const std::string& str)
{
std::string strTemp = "";
size_t length = str.length();
for (size_t i = 0; i < length; i++)
{
if (isalnum((unsigned char)str[i]) ||
(str[i] == '-') ||
(str[i] == '_') ||
(str[i] == '.') ||
(str[i] == '~'))
strTemp += str[i];
else if (str[i] == ' ')
strTemp += "+";
else
{
strTemp += '%';
strTemp += ToHex((unsigned char)str[i] >> 4);
strTemp += ToHex((unsigned char)str[i] % 16);
}
}
return strTemp;
}
std::string UrlDecode(const std::string& str)
{
std::string strTemp = "";
size_t length = str.length();
for (size_t i = 0; i < length; i++)
{
if (str[i] == '+') strTemp += ' ';
else if (str[i] == '%')
{
assert(i + 2 < length);
unsigned char high = FromHex((unsigned char)str[++i]);
unsigned char low = FromHex((unsigned char)str[++i]);
strTemp += high * 16 + low;
}
else strTemp += str[i];
}
return strTemp;
}