Converting std::wstring to array of UTF8 characters
-
Thursday, July 03, 2008 3:11 PMI 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.
All Replies
-
Thursday, July 03, 2008 3:26 PM
eldiener said: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.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.- Marked As Answer by eldiener Thursday, July 03, 2008 3:27 PM
-
Sunday, July 06, 2008 6:44 PMeldiener said: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.eldiener said: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.
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.

