Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
codecvt wide string conversion with multibyte chars and locale + concatenation
//Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23506 for x64 #include <iostream> #include <string.h> #include <codecvt> #include <Windows.h> // ===================================================================================================================== // Returns the length of a wchar_t based string. This function is necessary when specifying the -fshort-wchar option size_t PalWcslen( const wchar_t* pWideStr) ///< [in] wide string to query { size_t len = 0; if (pWideStr != nullptr) { const wchar_t* pWchar = pWideStr; while (*pWchar != 0) { ++pWchar; ++len; } } return len; } // ===================================================================================================================== // Convert UTf-16 string to UTF-8 bool ConvertUtf16StringToUtf8( char* pDst, ///< [out] dst string const wchar_t* pSrc, ///< [in] src string size_t dstSizeInBytes) ///< size of the destination buffer in bytes { std::codecvt_utf8_utf16<char16_t> converter; mbstate_t state = {0}; char* pCharNext = nullptr; const char16_t* pUtf16Next = nullptr; std::codecvt_base::result retCode = converter.out(state, reinterpret_cast<const char16_t*>(pSrc), reinterpret_cast<const char16_t*>(pSrc)+PalWcslen(pSrc)+1, pUtf16Next, pDst, pDst + dstSizeInBytes, pCharNext); return (retCode == std::codecvt_base::ok); } // ===================================================================================================================== // Convert UTF-16 or UTF-32 string (depending on wchar_t size) to UTF-8 bool ConvertWcharStringToUtf8( char* pDst, ///< [out] dst string const wchar_t* pSrc, ///< [in] src string size_t dstSizeInBytes) ///< size of the destination buffer in bytes { bool result = false; if (sizeof(wchar_t) == 2) { result = ConvertUtf16StringToUtf8(pDst, pSrc, dstSizeInBytes); } else { std::codecvt_utf8<char32_t> converter; mbstate_t state = {0}; char* pCharNext = nullptr; const char32_t* pUtf32Next = nullptr; std::codecvt_base::result retCode = converter.out(state, reinterpret_cast<const char32_t*>(pSrc), reinterpret_cast<const char32_t*>(pSrc) + PalWcslen(pSrc) + 1, pUtf32Next, pDst, pDst + dstSizeInBytes, pCharNext); result = (retCode == std::codecvt_base::ok); } return result; } int main() { wchar_t pLocale[256]; GetSystemDefaultLocaleName(pLocale, sizeof(pLocale)); std::wcout << L"Current locale: " << pLocale << std::endl; const wchar_t* ppInput[] = { L"測試", L"αβ", L"Środa" }; for(size_t i = 0; i < _countof(ppInput); ++i) { std::wcout << L"Original string: " << ppInput[i] << std::endl; char pOutput[256] = { '\0' }; bool ret = ConvertWcharStringToUtf8(pOutput, ppInput[i], sizeof(pOutput) - 1); std::cout << "\tret = " << ret << ", output = " << pOutput << std::endl; strncat_s(pOutput, sizeof(pOutput), "-concatenated", _TRUNCATE); std::cout << "\tconcatenated = " << pOutput << std::endl; } }
run
|
edit
|
history
|
help
0
Computing factorial of an integer with recursion and iteration [EDIT]
Runner-mt
Use std::is_base_of to subset STL container contents.
hangman
MSVCStatic
Error with move capture of a const ref in a lambda function
Error log b is an undeclared identifier...
reference
Dices by GOOSE
hgh