Winodws 宽字符(UTF16), ANSI(GBK), UTF8 转换

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
#include <atlstr.h>
#include <stdio.h>


//std::string ConvertWideToANSI(const std::wstring& wstr)
//{
// int count = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.length(), NULL, 0, NULL, NULL);
// std::string str(count, 0);
// WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, &str[0], count, NULL, NULL);
// return str;
//}
//
//std::wstring ConvertAnsiToWide(const std::string& str)
//{
// int count = MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.length(), NULL, 0);
// std::wstring wstr(count, 0);
// MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.length(), &wstr[0], count);
// return wstr;
//}
//
//std::string ConvertWideToUtf8(const std::wstring& wstr)
//{
// int count = WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), wstr.length(), NULL, 0, NULL, NULL);
// std::string str(count, 0);
// WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, &str[0], count, NULL, NULL);
// return str;
//}
//
//std::wstring ConvertUtf8ToWide(const std::string& str)
//{
// int count = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), NULL, 0);
// std::wstring wstr(count, 0);
// MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), &wstr[0], count);
// return wstr;
//}

int ConvertWideToANSI(const wchar_t *wchar_data, int wchar_len, char *buff, int buffsize)
{
int count = WideCharToMultiByte(CP_ACP, 0, wchar_data, wchar_len, NULL, 0, NULL, NULL);
WideCharToMultiByte(CP_ACP, 0, wchar_data, wchar_len, buff, count, NULL, NULL);
return count;
}

int main()
{
//报文中的数据序列
unsigned char rawData[] = {
0x00, 0x35, 0x00, 0x38, 0x00, 0x38, 0x53, 0x73, 0x53, 0xEF, 0x83, 0xB7,
0x53, 0xD6, 0x4E, 0xA4, 0x66, 0x13, 0x4E, 0xE3, 0x78, 0x01, 0x54, 0x8C,
0x62, 0x4B, 0x7E, 0xED, 0x8D, 0x39, 0x8B, 0xE6, 0x60, 0xC5, 0x30, 0x02,
0x56, 0xDE, 0x59, 0x0D, 0x20, 0x1C, 0x00, 0x54, 0x00, 0x44, 0x00, 0x46,
0x00, 0x51, 0x00, 0x23, 0x53, 0x61, 0x53, 0xF7, 0x54, 0x0E, 0x56, 0xDB,
0x4F, 0x4D, 0x20, 0x1D, 0x90, 0x00, 0x8B, 0xA2, 0x30, 0x02, 0x7F, 0x16,
0x8F, 0x91, 0x77, 0xED, 0x4F, 0xE1, 0x00, 0x65, 0x00, 0x73, 0x00, 0x68,
0x56, 0xDE, 0x59, 0x0D, 0x81, 0xF3, 0x00, 0x39, 0x00, 0x35, 0x00, 0x35,
0x00, 0x38, 0x00, 0x38, 0x4E, 0x0B, 0x8F, 0x7D, 0x5D, 0xE5, 0x94, 0xF6,
0x4F, 0xE1, 0x75, 0x28, 0x53, 0x61, 0x5B, 0x98, 0x65, 0xB9, 0x00, 0x41,
0x00, 0x50, 0x00, 0x50, 0x20, 0x1C, 0x5D, 0xE5, 0x94, 0xF6, 0x00, 0x65,
0x75, 0x1F, 0x15, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x75, 0x64
};

//网络序 转 本机序
for (int i = 0; i < sizeof(rawData); i += 2)
{
unsigned char t = rawData[i];
rawData[i] = rawData[i + 1];
rawData[i + 1] = t;
}

char buff[1024];
int len = ConvertWideToANSI((const wchar_t*)rawData, sizeof(rawData) / sizeof(wchar_t), buff, sizeof(buff));

printf("转换输出\n");
printf("长度:%d\n", len);
printf("内容:%s\n", buff);
getchar();
return 0;
}