-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCPConv.cpp
66 lines (60 loc) · 1.46 KB
/
CPConv.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
/*
* CPConv.cpp
*
* Implements the Codepage Convertions in win32
* Copyright (C) 2018-2019 khjxiaogu
*
* Author: khjxiaogu
* Web: http://www.khjxiaogu.com
*/
#include "CPConv.h"
#include <windows.h>
//from char to wchar
//deprecated
wchar_t* AnsiToUnicode(const char* szStr) {
int nLen = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, szStr, -1, NULL, 0);
if (nLen == 0)
{
return NULL;
}
wchar_t* pResult = new wchar_t[nLen + 1];
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, szStr, -1, pResult, nLen);
return pResult;
}
/*
from wchar(TJS_W) to char
*/
char* UnicodeToAnsi(const wchar_t* szStr)
{
int nLen = WideCharToMultiByte(CP_ACP, 0, szStr, -1, NULL, 0, NULL, NULL);
if (nLen == 0)
{
return NULL;
}
char* pResult = new char[nLen + 1];
WideCharToMultiByte(CP_ACP, 0, szStr, -1, pResult, nLen, NULL, NULL);
return pResult;
}
//from wchar to UTF8
char* WCSToUTF8(const wchar_t* szStr)
{
int nLen = WideCharToMultiByte(CP_UTF8, 0, szStr, -1, NULL, 0, NULL, NULL);
if (nLen == 0)
{
return NULL;
}
char* pResult = new char[nLen + 1];
WideCharToMultiByte(CP_UTF8, 0, szStr, -1, pResult, nLen, NULL, NULL);
return pResult;
}
//from UTF8 to wchar
wchar_t* UTF82WCS(const char* szU8)
{
int wcsLen = ::MultiByteToWideChar(CP_UTF8, NULL, szU8, strlen(szU8), NULL, 0);
wchar_t* wszString = new wchar_t[wcsLen + 1];
//ת»»
::MultiByteToWideChar(CP_UTF8, NULL, szU8, strlen(szU8), wszString, wcsLen);
//×îºó¼ÓÉÏ'\0'
wszString[wcsLen] = '\0';
return wszString;
}