How to convert Unicode string to utf-8 or utf-16 string?

How to convert Unicode string to utf-8 or utf-16 string? My VS2005 project uses a Unicode char, and sqlite in cpp provides

int sqlite3_open(
  const char *filename,   /* Database filename (UTF-8) */
  sqlite3 **ppDb          /* OUT: SQLite db handle */
);
int sqlite3_open16(
  const void *filename,   /* Database filename (UTF-16) */
  sqlite3 **ppDb          /* OUT: SQLite db handle */
);

to open a folder. How to convert string, CString or wstring to UTF-8 or UTF-16 encoding?

Many thanks!

+5
source share
5 answers

Short answer:

, Unicode, CString wstring. sqlite3_open16(). , WCHAR ( void *). , ! -, , char, void *) API. , CString: (void*)(LPCWSTR)strFilename

:

Unicode, UTF8 UTF16. Unicode, : Unicode . , ( ) ( ). UTF8 UTF16 . , .

VS "Unicode charset", , " UTF16". sqlite3_open16(). . WCHAR ( char), 16 (Fallsback C- wchar_t, 16 Win32. . , Checkers).

, : UTF16 : Big Endian Little Endian. 16 . , UTF16, , . , , sqlite endian-ness, Windows (Little Endian IIRC. , :-)).

EDIT: :

UTF16 16- . Win32 ( Win32) wchar_t. , Unicode 16- . .

UTF8 1 , 1 4 . UTF8 char.

+6

WideCharToMultiByte. CP_UTF8 CodePage.

CHAR buf[256]; // or whatever
WideCharToMultiByte(
  CP_UTF8, 
  0, 
  StringToConvert, // the string you have
  -1, // length of the string - set -1 to indicate it is null terminated
  buf, // output
  __countof(buf), // size of the buffer in bytes - if you leave it zero the return value is the length required for the output buffer
  NULL,    
  NULL
);

, - UTF-16LE, - sqlite3_open16.

+7

++ . . Wstring 16- Windows, utf-16, , . Wstring , , , utf16. Windows utf16, UNICODE, , , utf16, .

WideCharToMultiByte, ( ) utf16 utf8. sqlite utf16, .

+3

utf-8 utf-16 - "unicode". , , , utf-32, . ,

"Convert utf-32 into utf-8 or utf-16"

.

0

- CStringA. CString typedef CStringA ( ASCII) CStringW ( char). . :

sqlite3_open(CStringA(L"MyWideCharFileName"), ...);
0

All Articles