2012-09-16 75 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 ... –