/************************************************************************/
/* Project: 宽字节与多字节互相转换 */
/* Author: LandyTan */
/* Time: 2017/11/04 */
/************************************************************************/
#include <iostream>
#include <Windows.h>
using namespace std;
char* _WTA(__in wchar_t* pszInBufBuf, __in int nInSize, __out char** pszOutBuf, __out int* pnOutSize);
wchar_t* _ATW(__in char* pszInBuf, __in int nInSize, __out wchar_t** pszOutBuf, __out int* pnOutSize);
HANDLE hFileA1,hFileW2;
DWORD dwWriteSize1,dwWriteSize2;
int main()
{
hFileA1 = CreateFile(L"C:\\Users\\Public\\Music\\acode.txt",GENERIC_WRITE,FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
hFileW2 = CreateFile(L"C:\\Users\\Public\\Music\\wcode.txt",GENERIC_WRITE,FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
{
char* pszIn = "大利嘉";
wchar_t* pszOut = NULL;
int nOutSize = 0;
if (!_ATW(pszIn, strlen(pszIn), &pszOut, &nOutSize))
cerr << "多字节转宽字节失败" << endl;
else cout << "多字节:" << pszIn << "\t宽字节:" << pszOut << endl;
delete[] pszOut;
pszOut = NULL;
}
{
wchar_t* pszIn = L"大利嘉";
char* pszOut = NULL;
int nOutSize = 0;
if (!_WTA(pszIn, wcslen(pszIn), &pszOut, &nOutSize))
cerr << "宽字节转多字节失败" << endl;
else cout << "宽字节:" << pszIn << "\t多字节:" << pszOut << endl;
delete[] pszOut;
pszOut = NULL;
}
CloseHandle(hFileA1);
CloseHandle(hFileW2);
system("pause");
return 0;
}
/************************************************************************/
/* Name: _ATW */
/* Function: 多字节转宽字节 */
/* Parameter list: */
/* pszInBuf 被转换的字符串 */
/* nInSize 字符串长度 */
/* pszOutBuf 接收转换的字符串 */
/* pnOutSize 接收字符串的长度 */
/************************************************************************/
wchar_t* _ATW(__in char* pszInBuf, __in int nInSize, __out wchar_t** pszOutBuf, __out int* pnOutSize)
{
if(!pszInBuf || !pszOutBuf || !pnOutSize || nInSize <= 0)return NULL;
*pnOutSize = MultiByteToWideChar(NULL, NULL, pszInBuf, nInSize, *pszOutBuf, 0);// 获取待转换字符串的缓冲区所需大小
if(*pnOutSize == 0)return NULL;
(*pnOutSize)++;
*pszOutBuf = new wchar_t[*pnOutSize];
memset((void*)*pszOutBuf, 0, sizeof(wchar_t) * (*pnOutSize));
if(MultiByteToWideChar(NULL, NULL, pszInBuf, nInSize, *pszOutBuf, *pnOutSize) == 0)
return NULL;
else
{
char mb[1024];
WideCharToMultiByte(CP_UTF8, 0, *pszOutBuf, *pnOutSize, mb, 1024, NULL, NULL);
int ret = WriteFile(hFileW2,mb,strlen(mb),&dwWriteSize2,NULL);
printf("GetError: ret:%d --- err:%d\n",ret,GetLastError());
return *pszOutBuf;
}
}
/************************************************************************/
/* Name: _WTA */
/* Function: 宽字节转多字节 */
/* Parameter list: */
/* pszInBuf 被转换的字符串 */
/* nInSize 字符串长度 */
/* pszOutBuf 接收转换的字符串 */
/* pnOutSize 接收字符串的长度 */
/************************************************************************/
char* _WTA(__in wchar_t* pszInBuf, __in int nInSize, __out char** pszOutBuf, __out int* pnOutSize)
{
if(!pszInBuf || !pszOutBuf || !pnOutSize || nInSize <= 0)return NULL;
*pnOutSize = WideCharToMultiByte(NULL, NULL, pszInBuf, nInSize, *pszOutBuf, 0, NULL, NULL);// 获取待转换字符串的缓冲区所需大小
if(*pnOutSize == 0)return NULL;
(*pnOutSize)++;
*pszOutBuf = new char[*pnOutSize];
memset((void*)*pszOutBuf, 0, sizeof(char)* (*pnOutSize));
if (WideCharToMultiByte(NULL, NULL, pszInBuf, nInSize, *pszOutBuf, *pnOutSize, NULL, NULL) == 0) // 这里才是转换
return NULL;
else
{
int ret = WriteFile(hFileA1,*pszOutBuf,*pnOutSize,&dwWriteSize1,NULL);
printf("GetError: ret:%d --- err:%d\n",ret,GetLastError());
return *pszOutBuf;
}
}
多字节、宽字节转换代码--MutiByteToWideChar & WideCharToMutiByte
最新推荐文章于 2024-05-10 11:53:31 发布