直接上代码
// Convert.h
#pragma once
LPSTR MYW2UTF(LPCWSTR _W, LPSTR _A);
LPWSTR MYUTF2W(LPCSTR _A, LPWSTR _W);
LPSTR MYW2A(LPCWSTR _W, LPSTR _A);
LPWSTR MYA2W(LPCSTR _A, LPWSTR _W);
LPSTR MYT2A(LPCTSTR _T, LPSTR _A);
LPTSTR MYA2T(LPCSTR _A, LPTSTR _T);
LPWSTR MYT2W(LPCTSTR _T, LPWSTR _W);
LPTSTR MYW2T(LPCWSTR _W, LPTSTR _T);
//Convert.cpp
#include "stdafx.h"
#include "Convert.h"
LPSTR MYW2A(LPCWSTR _W, LPSTR _A)
{
int nOutputStrLen = WideCharToMultiByte(CP_ACP, 0, _W, wcslen(_W), NULL, 0, 0, 0);
if (nOutputStrLen != 0)
{
nOutputStrLen = WideCharToMultiByte(CP_ACP, 0, _W, wcslen(_W), _A, nOutputStrLen, 0, 0);
}
if (nOutputStrLen > 0)
{
_A[nOutputStrLen] = '\0';
}
else
{
_A[0] = '\0';
}
return _A;
}
LPWSTR MYA2W(LPCSTR _A, LPWSTR _W)
{
int nOutputStrLen = MultiByteToWideChar(CP_ACP, 0, _A, strlen(_A), NULL, 0);
if (nOutputStrLen != 0)
{
nOutputStrLen = MultiByteToWideChar(CP_ACP, 0, _A, strlen(_A), _W, nOutputStrLen);
}
if (nOutputStrLen > 0)
{
_W[nOutputStrLen] = '\0';
}
else
{
_W[0] = '\0';
}
return _W;
}
LPSTR MYW2UTF(LPCWSTR _W, LPSTR _A)
{
int nOutputStrLen = WideCharToMultiByte(CP_UTF8, 0, _W, wcslen(_W), NULL, 0, 0, 0);
if (nOutputStrLen != 0)
{
nOutputStrLen = WideCharToMultiByte(CP_UTF8, 0, _W, wcslen(_W), _A, nOutputStrLen, 0, 0);
}
if (nOutputStrLen > 0)
{
_A[nOutputStrLen] = '\0';
}
else
{
_A[0] = '\0';
}
return _A;
}
LPWSTR MYUTF2W(LPCSTR _A, LPWSTR _W)
{
int nOutputStrLen = MultiByteToWideChar(CP_UTF8, 0, _A, strlen(_A), NULL, 0);
if (nOutputStrLen != 0)
{
nOutputStrLen = MultiByteToWideChar(CP_UTF8, 0, _A, strlen(_A), _W, nOutputStrLen);
}
if (nOutputStrLen > 0)
{
_W[nOutputStrLen] = '\0';
}
else
{
_W[0] = '\0';
}
return _W;
}
LPSTR MYT2A(LPCTSTR _T, LPSTR _A)
{
#if defined(_UNICODE)
return MYW2A(_T, _A);
#else
strcpy(_A, _T);
return _A;
#endif
}
LPTSTR MYA2T(LPCSTR _A, LPTSTR _T)
{
#if defined(_UNICODE)
return MYA2W(_A, _T);
#else
strcpy(_T, _A);
return _A;
#endif
}
LPWSTR MYT2W(LPCTSTR _T, LPWSTR _W)
{
#if defined(_UNICODE)
wcscpy(_W, _T);
return _W;
#else
return MYA2W(_T, _W);
#endif
}
LPTSTR MYW2T(LPCWSTR _W, LPTSTR _T)
{
#if defined(_UNICODE)
wcscpy(_T, _W);
return _T;
#else
return MYW2A(_W, _T);
#endif
}