<p>How to convert Unicode string into a utf-8 or utf-16 string?
My VS2005 project is using Unicode char set, while sqlite in cpp provide </p>

<pre><code>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 */
);
</code></pre>

<p>for opening a folder.
How can I convert string, CString, or wstring into UTF-8 or UTF-16 charset?</p>

<p>Thanks very much!</p>

Use the
WideCharToMultiByte
function. Specify
```
CP_UTF8
```
for the
```
CodePage
```
parameter.
```
```
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
);
```
```
Also, the default encoding for unicode apps in windows is UTF-16LE, so you might not need to perform any translation and just use the second version
```
sqlite3_open16
```
.