none
Converting std::wstring to array of UTF8 characters

    Question

  • I need to encrypt a std::wstring and eventually decrypt back to the std::wstring. To do this I need to convert the wstring to an array of UTF8 characters, but in such a way that each wchar_t becomes equivalent to the two characters which comprises the wchar_t. When I decrypt back I need to take my array of UTF8 characters and convert it to the equivalent wstring.

    How do I do that using native C++ and/or the Win32 API ?

    I do not think the various WideCharToMultiByte/MultiByteToWideChar ( wcstombs/mbstowcs in the RTL ) will work properly, as I believe they do conversions rather than equivalents.
    Thursday, July 03, 2008 3:11 PM

Answers

  • eldiener said:

    I need to encrypt a std::wstring and eventually decrypt back to the std::wstring. To do this I need to convert the wstring to an array of UTF8 characters, but in such a way that each wchar_t becomes equivalent to the two characters which comprises the wchar_t. When I decrypt back I need to take my array of UTF8 characters and convert it to the equivalent wstring.

    How do I do that using native C++ and/or the Win32 API ?

    I do not think the various WideCharToMultiByte/MultiByteToWideChar ( wcstombs/mbstowcs in the RTL ) will work properly, as I believe they do conversions rather than equivalents.

    Never mind, I realized the answer myself, which is to use LOBYTE and HIBYTE for a wchar_t, after casting it to a WORD, and then putting it the WORD together again from the individual BYTEs.

    • Marked as answer by eldiener Thursday, July 03, 2008 3:27 PM
    Thursday, July 03, 2008 3:26 PM

All replies

  • eldiener said:

    I need to encrypt a std::wstring and eventually decrypt back to the std::wstring. To do this I need to convert the wstring to an array of UTF8 characters, but in such a way that each wchar_t becomes equivalent to the two characters which comprises the wchar_t. When I decrypt back I need to take my array of UTF8 characters and convert it to the equivalent wstring.

    How do I do that using native C++ and/or the Win32 API ?

    I do not think the various WideCharToMultiByte/MultiByteToWideChar ( wcstombs/mbstowcs in the RTL ) will work properly, as I believe they do conversions rather than equivalents.

    Never mind, I realized the answer myself, which is to use LOBYTE and HIBYTE for a wchar_t, after casting it to a WORD, and then putting it the WORD together again from the individual BYTEs.

    • Marked as answer by eldiener Thursday, July 03, 2008 3:27 PM
    Thursday, July 03, 2008 3:26 PM
  • eldiener said:

    eldiener said:

    I need to encrypt a std::wstring and eventually decrypt back to the std::wstring. To do this I need to convert the wstring to an array of UTF8 characters, but in such a way that each wchar_t becomes equivalent to the two characters which comprises the wchar_t. When I decrypt back I need to take my array of UTF8 characters and convert it to the equivalent wstring.

    How do I do that using native C++ and/or the Win32 API ?

    I do not think the various WideCharToMultiByte/MultiByteToWideChar ( wcstombs/mbstowcs in the RTL ) will work properly, as I believe they do conversions rather than equivalents.

    Never mind, I realized the answer myself, which is to use LOBYTE and HIBYTE for a wchar_t, after casting it to a WORD, and then putting it the WORD together again from the individual BYTEs.

    Actually the LOBYTE, HIBYTE technique is not needed as I can just directly reinterpret_cast my wstring's c_str() const wchar_t * pointer to a const BYTE * pointer to get the exact same affect.

    Sunday, July 06, 2008 6:44 PM