77

WideCharToMultiByteに関するドキュメントを読みましたが、このパラメーターにこだわっています。

lpMultiByteStr
[out] Pointer to a buffer that receives the converted string.

変数を適切に初期化して関数にフィードする方法がよくわかりません

4

4 に答える 4

147

WideCharToMultiByte と MultiByteToWideChar を使用して、utf8 を使用して std::wstring と std::string の間でデータを失わないように変換する (Brian Bondy の例に基づく) いくつかの関数を次に示します。

// Convert a wide Unicode string to an UTF8 string
std::string utf8_encode(const std::wstring &wstr)
{
    if( wstr.empty() ) return std::string();
    int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL);
    std::string strTo( size_needed, 0 );
    WideCharToMultiByte                  (CP_UTF8, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, NULL, NULL);
    return strTo;
}

// Convert an UTF8 string to a wide Unicode String
std::wstring utf8_decode(const std::string &str)
{
    if( str.empty() ) return std::wstring();
    int size_needed = MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), NULL, 0);
    std::wstring wstrTo( size_needed, 0 );
    MultiByteToWideChar                  (CP_UTF8, 0, &str[0], (int)str.size(), &wstrTo[0], size_needed);
    return wstrTo;
}
于 2010-10-22T17:59:31.263 に答える
37

ブライアンR.ボンディによって提供された答えを詳しく説明します。これは、出力バッファをソース文字列のワイド文字の数に単純にサイズ変更できない理由を示す例です。

#include <windows.h>
#include <stdio.h>
#include <wchar.h>
#include <string.h>

/* string consisting of several Asian characters */
wchar_t wcsString[] = L"\u9580\u961c\u9640\u963f\u963b\u9644";

int main() 
{

    size_t wcsChars = wcslen( wcsString);

    size_t sizeRequired = WideCharToMultiByte( 950, 0, wcsString, -1, 
                                               NULL, 0,  NULL, NULL);

    printf( "Wide chars in wcsString: %u\n", wcsChars);
    printf( "Bytes required for CP950 encoding (excluding NUL terminator): %u\n",
             sizeRequired-1);

    sizeRequired = WideCharToMultiByte( CP_UTF8, 0, wcsString, -1,
                                        NULL, 0,  NULL, NULL);
    printf( "Bytes required for UTF8 encoding (excluding NUL terminator): %u\n",
             sizeRequired-1);
}

そして出力:

Wide chars in wcsString: 6
Bytes required for CP950 encoding (excluding NUL terminator): 12
Bytes required for UTF8 encoding (excluding NUL terminator): 18
于 2008-10-19T19:52:00.927 に答える
20

新しい char 配列を作成して、lpMultiByteStr [out] パラメータを使用します。次に、この char 配列を渡して埋めます。文字列の長さ + 1 を初期化するだけで、変換後に null で終了する文字列を使用できます。

ここにいくつかの便利なヘルパー関数があります。これらはすべてのパラメーターの使用法を示しています。

#include <string>

std::string wstrtostr(const std::wstring &wstr)
{
    // Convert a Unicode string to an ASCII string
    std::string strTo;
    char *szTo = new char[wstr.length() + 1];
    szTo[wstr.size()] = '\0';
    WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, szTo, (int)wstr.length(), NULL, NULL);
    strTo = szTo;
    delete[] szTo;
    return strTo;
}

std::wstring strtowstr(const std::string &str)
{
    // Convert an ASCII string to a Unicode String
    std::wstring wstrTo;
    wchar_t *wszTo = new wchar_t[str.length() + 1];
    wszTo[str.size()] = L'\0';
    MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, wszTo, (int)str.length());
    wstrTo = wszTo;
    delete[] wszTo;
    return wstrTo;
}

--

ドキュメントで、型へのポインターであるパラメーターがあり、それが出力変数であることがわかった場合はいつでも、その型を作成してから、その型へのポインターを渡したいと思うでしょう。関数はそのポインタを使用して変数を埋めます。

したがって、これをよりよく理解できます。

//pX is an out parameter, it fills your variable with 10.
void fillXWith10(int *pX)
{
  *pX = 10;
}

int main(int argc, char ** argv)
{
  int X;
  fillXWith10(&X);
  return 0;
}
于 2008-10-19T03:41:27.217 に答える