-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchartowchar.cpp
100 lines (81 loc) · 2.86 KB
/
chartowchar.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
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
#include <iostream>
#include <string>
void printcharbuf(char * buf, unsigned int bufsize) {
// print buf content
std::cout << "buf = { ";
for (unsigned int i = 0; i < bufsize; i++) {
std::cout << std::hex << (int) buf[i];
if (i+1 != bufsize)
std::cout << ", ";
}
std::cout << " }\n";
}
void printwcharbuf(wchar_t * buf, unsigned int bufsize) {
// print buf content
std::cout << "buf = { ";
for (unsigned int i = 0; i < bufsize; i++) {
std::cout << std::hex << (int) buf[i];
if (i+1 != bufsize)
std::cout << ", ";
}
std::cout << " }\n";
}
void char2wchar(std::wstring & ws) {
std::cout << "\n\n--\nchar2wchar\n\n";
const unsigned int length = 3;
char wstr[(length+1)*sizeof(wchar_t)] = { 0x41, 0x00, 0x00, 0x00, 0x42, 0x00, 0x00, 0x00, 0x43, 0x00, 0x00, 0x00, 0x7f, 0x7f, 0x7f, 0x7f };
// Create a buffer of wchar_t's
// The buffer size is (length+1) * sizeof(wchar_t). The +1 is for the terminating null.
char * buf = NULL;
unsigned int bufsize = (length + 1) * sizeof(wchar_t); // +1 for terminating null
std::cout << "bufsize = (length + 1 ) * sizeof(wchar_t) = " << bufsize << "\n";
buf = new char[(length + 1 ) * sizeof(wchar_t)];
std::cout << "buf = " << static_cast<void*>(buf) << "\n";
// Fill buf with known chars
for (unsigned int i = 0; i < bufsize; i++) {
buf[i] = wstr[i];
}
printcharbuf(buf, bufsize);
std::cout << "Set buf NULL terminator\n";
*(wchar_t*)(buf+length*sizeof(wchar_t)) = L'\0';
printcharbuf(buf, bufsize);
// assign string
ws = (wchar_t*) buf;
// Free temp buffer
delete [] buf;
}
void wchar2char(std::wstring & ws) {
std::cout << "\n\n--\nwchar2char\n\n";
const unsigned int length = 3;
wchar_t wstr[length+1] = { 0x00000041, 0x00000042, 0x00000043, 0x7f7f7f7f };
// Create a buffer of wchar_t's
// The buffer size is (length+1). The +1 is for the terminating null.
wchar_t * buf = NULL;
unsigned int bufsize = length + 1; // +1 for terminating null
std::cout << "bufsize = length + 1 = " << bufsize << "\n";
buf = new wchar_t[length + 1];
std::cout << "buf = " << static_cast<void*>(buf) << "\n";
// Fill buf with known wchar_t
for (unsigned int i = 0; i < bufsize; i++) {
buf[i] = wstr[i];
}
printwcharbuf(buf, bufsize);
std::cout << "Set buf NULL terminator\n";
buf[length] = L'\0';
printwcharbuf(buf, bufsize);
// assign string
ws = buf;
// Free buffer
delete [] buf;
}
int main() {
std::cout << "sizeof(char) is " << sizeof(char) << "\n";
std::cout << "sizeof(wchar_t) is " << sizeof(wchar_t) << "\n";
std::wstring ws1;
char2wchar(ws1);
printf("string = %S\n", ws1.c_str());
std::wstring ws2;
wchar2char(ws2);
printf("string = %S\n", ws2.c_str());
return 0;
}