How to convert wchar_t (or wchar_t* or CORBA::WChar*) to string?
-
02-07-2021 - |
题
In my app I have to use CORBA::WChar* (or equivalent wchar_t*) but my program also needs to save some information to the PostgreSQL database. To insert data to PostgreSQL in C++ I use SOCI. And there's the problem because :
The following types are currently supported for use with into and use expressions:
char (for character values)
short, int, unsigned long, long long, double (for numeric values)
char*, char[], std::string (for string values)
std::tm (for datetime values)
soci::statement (for nested statements and PL/SQL cursors)
soci::blob (for Binary Large OBjects)
soci::row_id (for row identifiers)
so theres no wchar_t* or wstring supported ... and I need to convert CORBA::WChar (or wchar_t or wchar_t*) to string. How to do this?
I also have problem with wide chars (and strings), using CodeBlocks 10.5 :
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char **argv)
{
const wchar_t *val = L"ąśżźćłóń";
wcout << val << "\n";
return 0;
}
shows:
E:\Temp\Untitled1.cpp||In function 'int main(int, char**)':|
E:\Temp\Untitled1.cpp|7|error: converting to execution character set: Invalid argument|
||=== Build finished: 1 errors, 0 warnings ===|
how to fix it?
I also need the code to be portable, that I can run it both on unix/linux and windows.
解决方案
I would suggest boost::locale::conv::utf_to_utf<char>(wchar_t*)
that simply convert you wchar_t*
string into UTF-8 for more information please read boost::locale
documentation
其他提示
What kind of database doesn't let you store Unicode strings? My suggestion would be to use a decent database.