1/2 #include "encoding/encoding.h" // using for raw convert, using bytes #include "encoding/encoding_std.h" // using for stl strings, std::string, std::wstring, std::u16string #include "encoding/u16string_bytes.h"// using for lower than c++11, that not have std::u16string 2/2 add in CMakeLists.txt if (NOT TARGET encoding) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../../cpp_cross_platform_encoding build_encoding) endif () target_link_libraries(${BINARY_NAME} encoding) // c language's helper functionstaticintutf8_2_utf16le_windows(constchar*src, size_tsrc_size, wchar_t*dst, size_t*dst_size){intr=MultiByteToWideChar(CP_UTF8, 0, src, (int)(src_size), dst, (int)(*dst_size)); if (r <= 0){return-1} else{*dst_size= (size_t)(r); return0} } staticintutf8_2_utf16le_windows2(constchar*src, size_tsrc_size, wchar_t**out_dst, size_t*out_dst_size){intret; wchar_t*dst=0; size_tdst_size=0; size_trsize=0; ret=utf8_2_utf16le_windows(src, src_size, 0, &dst_size); if (0==ret&&dst_size<0x10000){// remember to call free()dst_size+=1; dst= (wchar_t*)calloc(1, sizeof(wchar_t)*dst_size); if (!dst) return-ENOMEM; rsize=dst_size; utf8_2_utf16le_windows(src, src_size, dst, &rsize); if(rsize>=dst_size){free(dst); return-1} *out_dst=dst; dst[rsize]=0; *out_dst_size=rsize; return0} return-1} staticintutf16le_2_utf8_windows(constwchar_t*src, size_tsrc_size, char*dst, size_t*dst_size){intr=WideCharToMultiByte(CP_UTF8, 0, src, (int)(src_size), dst, (int)(*dst_size), NULL, NULL); if (r <= 0){return-1} else{*dst_size= (size_t)(r); return0} } staticintutf16le_2_utf8_windows2(constwchar_t*src, size_tsrc_size, char**out_dst, size_t*out_dst_size){intret; char*dst=0; size_tdst_size=0; size_trsize=0; ret=utf16le_2_utf8_windows(src, src_size, 0, &dst_size); if (0==ret&&dst_size<0x10000){// remember to call free()dst_size+=1; dst= (char*)calloc(1, dst_size); if (!dst) return-ENOMEM; rsize=dst_size; utf16le_2_utf8_windows(src, src_size, dst, &rsize); if(rsize>=dst_size){free(dst); return-1} *out_dst=dst; dst[rsize]=0; *out_dst_size=rsize; return0} return-1}[UTF-8 遍地开花] http://utf8everywhere.org/zh-cn