avatar

目录
C++编码转换

内容涉及到 AsciiToUnicode UnicodeToAscii Utf8ToUnicode UnicodeToUtf8

源代码

cpp
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
wstring AsciiToUnicode(const string& str)

{

int unicodeLen = MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, nullptr, 0);

wchar_t *pUnicode = (wchar_t*)malloc(sizeof(wchar_t)*unicodeLen);

MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, pUnicode, unicodeLen);

wstring ret_str = pUnicode;

free(pUnicode);

return ret_str;

}

string UnicodeToAscii(const wstring& wstr)

{

int ansiiLen = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, nullptr, 0, nullptr, nullptr);

char *pAssii = (char*)malloc(sizeof(char)*ansiiLen);

WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, pAssii, ansiiLen, nullptr, nullptr);

string ret_str = pAssii;

free(pAssii);

return ret_str;

}



wstring Utf8ToUnicode(const string& str)

{

int unicodeLen = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, nullptr, 0);

wchar_t *pUnicode = (wchar_t*)malloc(sizeof(wchar_t)*unicodeLen);

MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, pUnicode, unicodeLen);

wstring ret_str = pUnicode;

free(pUnicode);

return ret_str;

}



string UnicodeToUtf8(const wstring& wstr)

{

int ansiiLen = WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, nullptr, 0, nullptr, nullptr);

char *pAssii = (char*)malloc(sizeof(char)*ansiiLen);

WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, pAssii, ansiiLen, nullptr, nullptr);

string ret_str = pAssii;

free(pAssii);

return ret_str;

}
文章作者: Abraverman
文章链接: http://abraverman.gitee.io/2020/05/14/c-conversion/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Abraverman
打赏
  • 微信
    微信
  • 支付宝
    支付宝

评论