Hi,
I'm trying to convert the Unicode (non-english) string to multibyte. Just to be safe, I'm converting the string again to Unicode form multibyte and comparing the strings (input string and input string(Unicode)--> multibyte --> Unicode ) I'm using wcscmp API to compare the string, but comparison fails. This works fine for English strings. Below is sample program.
#include <iostream>
#include<Windows.h>
#include <atlstr.h>
#include <string>
using namespace std;
typedef wchar_t MYUCHAR;
namespace conversion
{
static HRESULT UniToChar(char** ppDstBuf, wchar_t const* pUniBuf, int& iDstBytes, UINT cp = CP_ACP)
{
HRESULT hRet = S_OK;
if (nullptr == ppDstBuf || nullptr == pUniBuf)
return HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER);
int iMultiBufSize = WideCharToMultiByte(cp, 0, pUniBuf, -1, nullptr, 0, nullptr, nullptr);
if (0 == iMultiBufSize)
{
hRet = HRESULT_FROM_WIN32(GetLastError());
return hRet;
}
try
{
*ppDstBuf = new char[static_cast<size_t>(iMultiBufSize)];
}
catch (std::bad_alloc&)
{
hRet = HRESULT_FROM_WIN32(ERROR_OUTOFMEMORY);
return hRet;
}
if (nullptr == *ppDstBuf)
{
hRet = HRESULT_FROM_WIN32(ERROR_OUTOFMEMORY);
return hRet;
}
if (0 == WideCharToMultiByte(cp, 0, pUniBuf, -1, *ppDstBuf, iMultiBufSize, nullptr, nullptr))
{
hRet = HRESULT_FROM_WIN32(GetLastError());
delete[] * ppDstBuf;
*ppDstBuf = nullptr;
return hRet;
}
iDstBytes = iMultiBufSize;
return hRet;
}
static HRESULT CharToUni(MYUCHAR** ppDstUniBuf, char const* pBuf, int& iDstUniChBufSz, UINT cp = CP_ACP)
{
HRESULT hRet = S_OK;
iDstUniChBufSz = 0;
if (nullptr == pBuf || nullptr == ppDstUniBuf)
return HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER);
int iUniBufSize = MultiByteToWideChar(cp, 0, pBuf, -1, nullptr, 0);
if (0 == iUniBufSize)
{
hRet = HRESULT_FROM_WIN32(GetLastError());
return hRet;
}
try
{
*ppDstUniBuf = new MYUCHAR[static_cast<size_t>(iUniBufSize)];
}
catch (std::bad_alloc&)
{
hRet = HRESULT_FROM_WIN32(ERROR_OUTOFMEMORY);
return hRet;
}
if (nullptr == *ppDstUniBuf)
{
hRet = HRESULT_FROM_WIN32(ERROR_OUTOFMEMORY);
return hRet;
}
if (0 == MultiByteToWideChar(cp, 0, pBuf, -1, *ppDstUniBuf, iUniBufSize))
{
hRet = HRESULT_FROM_WIN32(GetLastError());
delete[] * ppDstUniBuf;
*ppDstUniBuf = nullptr;
}
iDstUniChBufSz = iUniBufSize;
return hRet;
}
};
static HRESULT UnicodeToChar(char** ppDstBuf, MYUCHAR const* pUniBuf, int& iDstBytes, UINT cp = CP_ACP)
{
HRESULT hRet = S_OK;
hRet = conversion::UniToChar(ppDstBuf, pUniBuf, iDstBytes, cp);
if (FAILED(hRet))
return hRet;
MYUCHAR* pUChar = nullptr;
int iUniBufSz = 0;
hRet = conversion::CharToUni(&pUChar, *ppDstBuf, iUniBufSz, cp);
if (FAILED(hRet))
return hRet;
if (pUChar)
{
if (wcscmp(pUChar, pUniBuf))
hRet = E_FAIL;
delete[] pUChar;
}
return hRet;
}
int main()
{
char* pChar;
std::wstring unifilePath = L"E:\\第三期中期.txt";
int iLen = 0;
HRESULT hRet = UnicodeToChar(&pChar, unifilePath.c_str(), iLen, CP_ACP);
if(FAILED(hr))
{
\\ cout
}
return 0;
}
Any idea, what could have gone wrong.
Thanks