25 Eylül 2017 Pazartesi

MultiByteToWideChar

Giriş
Windows bir çok API'de UTF-16 kullanıyor. Bu yüzden UTF-8'in UTF-16'ye çevrilmesi ve std::string içinde saklanması gerekiyor.

Örnek
Şöyle yaparız.
#include <windows.h>

std::string get_filename_token( const std::string& filename )
{
  // Convert the UTF-8 argument path to a Windows-friendly UTF-16 path
  wchar_t* widepath = new wchar_t[ filename.length() + 1 ];
  MultiByteToWideChar( CP_UTF8, 0, filename.c_str(), -1, widepath, filename.length() + 1);

  // Now get the 8.5 version of the name
  DWORD n = GetShortPathNameW( widepath, NULL, 0 );
  wchar_t* shortpath = new wchar_t[ n ];
  GetShortPathNameW( widepath, shortpath, n );

  // Convert the short version back to a C++-friendly char version
  n = WideCharToMultiByte( CP_UTF8, 0, shortpath, -1, NULL, 0, NULL, NULL );
  char* ansipath = new char[ n ];
  WideCharToMultiByte( CP_UTF8, 0, shortpath, -1, ansipath, n, NULL, NULL );

  std::string result( ansipath );

  delete [] ansipath;
  delete [] shortpath;
  delete [] widepath;

  return result;
}


Hiç yorum yok:

Yorum Gönder