Unicode to ANSI conversion problem

Unicode to ANSI conversion problem

Post by Harr » Wed, 29 Sep 2004 01:18:22


Hi I have a BSTR which contains

28,640

the memory contains

0xc200a3003100320038002c00360034003000

I would like to convert this to "standard" ANSI ie

0xa331323832c363430

I tried the following routine which I hacked together from an example, but
get *very* occasional memory faults but I can't trace why.

I am running in a threaded environment but I don't think that is anything to
do with it maybe there's another problem somewhere else.

Could someone post a bullet-proof routine which does the conversion so that
I can
be sure?

Thanks.



/* this version is passed pszW both in CP_UTF8 format and wide.
EG 0xc2a3, so 0xc200a300 is passed in.
*/

HRESULT __fastcall MyUnicodeToAnsi(LPCOLESTR pszW,CString* pString)
{
ULONG cbAnsi, cCharacters, cbPreAnsi;
DWORD dwError;


LPSTR ppszA=NULL;
LPSTR ppszB=NULL;

// If input is null then just return the same.

if (pszW == NULL)
{
*ppszA = NULL;
return NOERROR;
}

cCharacters = wcslen(pszW)+1;

// Determine number of bytes to be allocated for ANSI string. An
// ANSI string can have at most 2 bytes per character (for Double
// Byte Character Strings.)

cbPreAnsi = cCharacters+1;
ppszB = (LPSTR) CoTaskMemAlloc(cbPreAnsi);

if (NULL == ppszB)
return E_OUTOFMEMORY;

//This version we must get rid of the nulls

if (0 == WideCharToMultiByte(CP_ACP, 0, pszW, wcslen(pszW)+1, ppszB,
cbPreAnsi, 0, NULL))
{
dwError = GetLastError();
CoTaskMemFree(ppszB);
ppszB = NULL;

return HRESULT_FROM_WIN32(dwError);
}

CComBSTR bstrB;
bstrB.AppendBytes(ppszB, strlen(ppszB) );

//Finshed with ppszB so delete it
CoTaskMemFree(ppszB);


//Now we are where we were in the original version (below)- so
//Just repoint the pointer

pszW=bstrB;

cCharacters = wcslen(pszW)+1;

// Determine number of bytes to be allocated for ANSI string. An
// ANSI string can have at most 2 bytes per character (for Double
// Byte Character Strings.)

cbAnsi = cCharacters*2;

// Use of the OLE allocator is not required because the resultant
// ANSI string will never be passed to another COM component. You

// can use your own allocator.

ppszA = (LPSTR) CoTaskMemAlloc(cbAnsi);
if (NULL == ppszA)
return E_OUTOFMEMORY;

// strcpy(ppszA,(LPCTSTR)pszW);
strcpy(ppszA,(const char *)pszW);
wchar_t * lijn4 = new wchar_t[cbAnsi];

if (NULL == lijn4)
{ CoTaskMemFree(ppszA);
return E_OUTOFMEMORY;}

//nulls the leading byte

if (0 == MultiByteToWideChar(CP_UTF8, 0, ppszA, cbAnsi+1, lijn4, cbAnsi))
{
dwError = GetLastError();
CoTaskMemFree(ppszA);
delete []lijn4;
ppszA = NULL;
return HRESULT_FROM_WIN32(dwError);
}

//does the final conversion - removing the leading byte.

*pString=lijn4;
delete []lijn4;
CoTaskMemFree(ppszA);
return NOERROR;

}
 
 
 

Unicode to ANSI conversion problem

Post by Harr » Wed, 29 Sep 2004 02:30:56


to >> do with it maybe there's another problem somewhere else. >> >> Could someone post a bullet-proof routine which does the conversion so
that >> I can >> be sure?

I found this page which looks OK
http://www.yqcomputer.com/