开发者

function to convert wstring to const char * for SQlite

开发者 https://www.devze.com 2023-02-02 23:59 出处:网络
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 r开发者_StackOverflowequires const char

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 r开发者_StackOverflowequires const char * for most of their functions.

I found the method of doing this on msdn, here: http://msdn.microsoft.com/en-us/library/ms235631%28v=vs.80%29.aspx, 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);


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消