- int chr2wch(const char* buffer, wchar_t*
&wBuf)
- {
- size_t len = strlen(buffer);
- size_t wlen = MultiByteToWideChar(CP_ACP, 0,
(const char*)buffer,
int(len),
NULL, 0);
- wBuf = new wchar_t[wlen
+ 1];
- MultiByteToWideChar(CP_ACP, 0,
(const char*)buffer,
int(len), wBuf,
int(wlen));
- wBuf[wlen]
= 0;
- return (int)wlen;
- }
- int chr2utf(const char* buf, char*
&mb_buf)
- {
- WCHAR* wbuf;
- int wlen = chr2wch(buf, wbuf);
- int mb_buf_size
= ::WideCharToMultiByte(CP_UTF8, 0, wbuf, wlen,
NULL, 0,
NULL,
NULL);
- mb_buf = new char[mb_buf_size
+ 1];
- int mb_len =
::WideCharToMultiByte(CP_UTF8, 0, wbuf, wlen,
mb_buf, mb_buf_size,
NULL,
NULL);
- mb_buf[mb_len]
= 0;
- delete[] wbuf;
- return mb_len;
- }
- int utf2wch(const char* buffer, wchar_t*
&wBuf)
- {
- size_t len = strlen(buffer);
- size_t wlen = MultiByteToWideChar(CP_UTF8, 0,
(const char*)buffer,
int(len),
NULL, 0);
- wBuf = new wchar_t[wlen
+ 1];
- MultiByteToWideChar(CP_UTF8, 0,
(const char*)buffer,
int(len), wBuf,
int(wlen));
- wBuf[wlen]
= 0;
- return (int)wlen;
- }
- int utf2chr(const char* buf, char*
&mb_buf)
- {
- WCHAR* wbuf;
- int wlen = utf2wch(buf, wbuf);
- int mb_buf_size
= ::WideCharToMultiByte(CP_ACP, 0, wbuf, wlen,
NULL, 0,
NULL,
NULL);
- mb_buf = new char[mb_buf_size
+ 1];
- int mb_len =
::WideCharToMultiByte(CP_ACP, 0, wbuf, wlen,
mb_buf, mb_buf_size,
NULL,
NULL);
- mb_buf[mb_len]
= 0;
- delete[] wbuf;
- return mb_len;
- }
由於保存新的字符串的內存是堆上創建的,所以用完之後要 delete []out_msg;