C++ Programming
05-25-2009, 01:50 PM
Hi there,
I found a code snippet on the web which converts an ANSI string to UTF-8 format. I implemented it in my own and it works.
////////////
// to UTF-8
char text[1024]={0};
WCHAR w[1024]={0};
int erg=0;
strcpy(text, m_pData);
erg=MultiByteToWideChar(CP_ACP, 0, text, -1, w, sizeof(w) / sizeof(WCHAR)); // ANSI to UNICODE
erg=WideCharToMultiByte(CP_UTF8, 0, w, -1, text, sizeof(text), 0, 0); // UNICODE to UTF-8
//
////////////
After that 'text' is UTF-8 formatted just nicely.
Now I was wondering, why doesn´t the following (slightly altered) code not work? I just created a char* instead of char[];
////////////
// to UTF-8
char* text = new char[1024];
WCHAR w[1024]={0};
int erg=0;
strcpy(text, m_pData);
erg=MultiByteToWideChar(CP_ACP, 0, text, -1, w, sizeof(w) / sizeof(WCHAR)); // ANSI to UNICODE
erg=WideCharToMultiByte(CP_UTF8, 0, w, -1, text, sizeof(text), 0, 0); // UNICODE to UTF-8
//
////////////
Thanks,
Souldrift
I found a code snippet on the web which converts an ANSI string to UTF-8 format. I implemented it in my own and it works.
////////////
// to UTF-8
char text[1024]={0};
WCHAR w[1024]={0};
int erg=0;
strcpy(text, m_pData);
erg=MultiByteToWideChar(CP_ACP, 0, text, -1, w, sizeof(w) / sizeof(WCHAR)); // ANSI to UNICODE
erg=WideCharToMultiByte(CP_UTF8, 0, w, -1, text, sizeof(text), 0, 0); // UNICODE to UTF-8
//
////////////
After that 'text' is UTF-8 formatted just nicely.
Now I was wondering, why doesn´t the following (slightly altered) code not work? I just created a char* instead of char[];
////////////
// to UTF-8
char* text = new char[1024];
WCHAR w[1024]={0};
int erg=0;
strcpy(text, m_pData);
erg=MultiByteToWideChar(CP_ACP, 0, text, -1, w, sizeof(w) / sizeof(WCHAR)); // ANSI to UNICODE
erg=WideCharToMultiByte(CP_UTF8, 0, w, -1, text, sizeof(text), 0, 0); // UNICODE to UTF-8
//
////////////
Thanks,
Souldrift