Maps a UTF-16 (wide character) string to a UTF-8-encoded string.
No VBA/VB6 equivalent.
long __stdcall CNV_Utf8FromWide(char *szOut, long nOutChars, const wchar_t* wstr);
Number of characters (bytes) in or required for the output string; otherwise it returns a negative error code.
No .NET equivalent.
static std::string dipki::Cnv::Utf8FromWide (const std::wstring &wstr)
Use this function to convert a C/C++ string of "Unicode" UTF-16 wchar_t
characters to a UTF-8-encoded string of type char
.
For the "raw" ANSI function, pass a NULL szOut or zero nOutChars to find the required output length in bytes. Allocate one extra byte for the terminating null.
This function is for C/C++ programmers only. There is no VBA or .NET equivalent.
#include <wchar.h> long nchars; char *buf = NULL; wchar_t *wstr = L"áéÍñóü"; // Find required output length (12 in this case) nchars = CNV_Utf8FromWide(NULL, 0, wstr); printf("CNV_Utf8FromWide returns %ld\n", nchars); assert(nchars >= 0); buf = malloc(nchars + 1); // NB one extra byte nchars = CNV_Utf8FromWide(buf, nchars, wstr); assert(nchars >= 0); // Assumes console codepage is set to UTF-8 printf("[%s]\n", buf); free(buf);
#include <string> std::string s; std::wstring wstr; wstr = L"áéÍñóü"; s = dipki::Cnv::Utf8FromWide(wstr); cout << "Cnv::Utf8FromWide=[" << s << "]" << endl; cout << "s-->utf-8 bytes: " << dipki::Cnv::ToHex(dipki::str2bvec(s)) << endl;
Cnv::Utf8FromWide=[áéÍñóü] s-->utf-8 bytes: C3A1C3A9C3ADC3B1C3B3C3BC