2012-09-16 72 views
0

在我的应用程序中,我必须使用CORBA :: WChar *(或等效的wchar_t *),但我的程序还需要将一些信息保存到PostgreSQL数据库。在C++中使用PostgreSQL插入数据我使用了SOCI。还有的问题,因为:如何将wchar_t(或wchar_t *或CORBA :: WChar *)转换为字符串?

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) 

所以世界上没有wchar_t的*或WSTRING支持......我需要CORBA :: WCHAR(或wchar_t的wchar_t的或*)到字符串转换。这个怎么做?

我也有宽字符(和字符串)问题,用代码块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; 
} 

显示:

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 ===| 

如何解决呢?

我还需要代码是可移植的,我可以在unix/linux和windows上运行它。

+0

你试过了吗:wcout << val << L“\ n”; ? – marcinj

+0

@luskan:我试过没有结果... –

回答

1

我建议boost::locale::conv::utf_to_utf<char>(wchar_t*)是简单地转换你wchar_t*字符串转换为UTF-8的更多信息,请阅读boost::locale文档

+0

是否可以自己编写这样的函数?我忘了说我不能在这个项目中使用boost:( –

+0

当然有可能,看看utf_to_utf的实现,它很容易通过你自己的实现,你甚至可以从'boost'复制实现或者读取UTF- 8定义,然后实现它,在我知道'boost'之前,我实现了它,并且它非常容易实现!!我确信你可以很容易地处理它:-) – BigBoss

-1

什么样的数据库不会让你存储Unicode字符串?我的建议是使用一个体面的数据库。

+0

它不是关于数据库 - 它关于我用来插入/从数据库中获取数据的SOCI ... –