función para convertir wstring a const char * para SQlite

as the question says, what would be a suitable template function to convert wstring to const char *? My program is written entirely in Unicode, however, SQlite requires const char * for most of their functions.

I found the method of doing this on msdn, here:, where name is a wstring.

// Convert to a char*
    size_t origsize = wcslen(name.c_str()) + 1;
    const size_t newsize = 100;
    size_t convertedChars = 0;
    char nstring[newsize];
    wcstombs_s(&convertedChars, nstring, origsize, name.c_str(), _TRUNCATE);

preguntado el 08 de enero de 11 a las 17:01

1 Respuestas

You should use the *16 sqlite functions (e.g. sqlite3_prepare16) where you4 can give UTF-16 (i.e. wstring) as parameters. Don_t forget to use 2*wcslen as length of the string. If you insist on the const char* functions, you have to convert to UTF-8 first.

Respondido el 08 de enero de 11 a las 21:01

If you're writing a cross-platform application, be aware that wchar_t is not necessarily a UTF-16 code unit. Of course, it is on Windows. - dan04

Why do I need to multiply the length of the string by two? - rem45acp

Because wcslen gives the length in number of wchar_t characters, which are 2 bytes, and sqlite wants the length in bytes. - hmuelner

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.