[WinAPI] Cum trec de la wide char la multi byte si viceversa

Folosire Windows API in programe C/C++ (forum moderat)
Post Reply
Dragos Cojocari
Membru++
Membru++
Posts: 789
Joined: 11 Jul 2007, 14:11

[WinAPI] Cum trec de la wide char la multi byte si viceversa

Post by Dragos Cojocari » 25 Jul 2007, 11:38

Metodele de mai jos nu au nimic special in ele, doar indica modul sigur de a face aceste conversii. Aceste metode calculeaza dimensiunea reszultatului si nu se bazeaza pe estimari de genul multibyte = wide char * 2

wide char ->multi byte

Code: Select all

std::string wideCharToMultiByte( const wchar_t *lpwstr)
{
	int size = WideCharToMultiByte( CP_ACP, 0, lpwstr, -1, NULL, 0, NULL, NULL );

	char *buffer = new char[ size+1];
	WideCharToMultiByte( CP_ACP, 0, lpwstr, -1, buffer, size, NULL, NULL );

	std::string str( buffer);
	delete []buffer;

	return str;
}
multibyte -> wide char

Code: Select all

std::wstring multiByteToWideChar( const char *lpstr)
{
	int size = MultiByteToWideChar( CP_ACP, 0, lpstr, -1, NULL, 0);

	WCHAR *buffer = new WCHAR[ size+1];
	MultiByteToWideChar( CP_ACP, 0, lpstr, -1, buffer, size);

	std::wstring str( buffer);
	delete []buffer;

	return str;
}

<< Back to Windows API Index



Post Reply