wxwidgets mysql_wxWidgets中wxString各类型转换

附加(数据库ADO类型):

文本

A literal is a string written in code with "quotes around it". A literal is not a wxString, and (in wxWidgets 2.8) will not be implicitly converted to one. This means that you can never pass in a raw literal into a wxWidget function or method (unless you don't care about your app not building with Unicode-enabled wxWidgets builds)

文本是一个在代码中被引号包围的串,文本它不是一个单纯的wxString类型,并且(在wxWidgets 2.8中)不能被隐含的转换为一个wxString类型。这意味着你不能试图将光秃秃的将一段文本放到wxWidget函数或方法中通过编译(除非你不在意你的应用程序是需要在Unicode编码环境中通过编译的)。

MessageBox("I'm a mistake!") // WRONG in WxWidgets 2.8 (OK in 2.9)

1

MessageBox("I'm a mistake!")  // WRONG in WxWidgets 2.8 (OK in 2.9)

Instead, wxWidgets (prior to wxWidgets 2.9) requires you to use one of these macros to turn literals into wxString-compatible characters:

_("text that can be translated")

wxT("text that won't be translated")

_T("same as wxT")

char* c = "sometext";

wxT(c) // WRONG, not a literal

1

2

3

4

5

6

_("textthatcanbetranslated")

wxT("textthatwon't be translated")

_T("sameaswxT")

char*c="sometext";

wxT(c)// WRONG, not a literal

Rather than being a nuisance, the _(), wxT(), and _T() macros take care of some unicode issues and help with internationalization.

char* to wxString

const char* chars = "Hello world";

// assuming your string is encoded as UTF-8, change the wxConv* parameter as needed

wxString mystring(chars, wxConvUTF8);

1

2

3

constchar*chars="Helloworld";

// assuming your string is encoded as UTF-8, change the wxConv* parameter as needed

wxStringmystring(chars,wxConvUTF8);

wxString to char*

void my_function(const char* foo)

{

}

...

wxString mystring(wxT("HelloWorld"));

// you could give the encoding you want as a parameter to mb_str(), e.g. mb_str(wxConvUTF8)

my_function( mystring.mb_str() );

1

2

3

4

5

6

7

voidmy_function(constchar*foo)

{

}

...

wxStringmystring(wxT("HelloWorld"));

// you could give the encoding you want as a parameter to mb_str(), e.g. mb_str(wxConvUTF8)

my_function(mystring.mb_str());

mb_str() 返回一个临时的指针,如果你需要通过函数得到更多的返回结果(就和上面的情况一样),你可以临时保存一下这个字符数据流:

wxString s( wxT("some string") );

wxCharBuffer buffer=s.ToUTF8();

foo( buffer.data() ); // data() returns const char *

bar( buffer.data(), strlen(buffer.data()) ); // in case you need the length of the data

1

2

3

4

wxStrings(wxT("somestring"));

wxCharBufferbuffer=s.ToUTF8();

foo(buffer.data());// data() returns const char *

bar(buffer.data(),strlen(buffer.data()));// in case you need the length of the data

当你真的需要将它复制为char*类型时:

wxString mystring(wxT("HelloWorld"));

char cstring[1024];

// assuming you want UTF-8, change the wxConv* parameter as needed

strncpy(cstring, (const char*)mystring.mb_str(wxConvUTF8), 1023);

1

2

3

4

wxStringmystring(wxT("HelloWorld"));

charcstring[1024];

// assuming you want UTF-8, change the wxConv* parameter as needed

strncpy(cstring,(constchar*)mystring.mb_str(wxConvUTF8),1023);

你也可以用ToUTF8(), 因为你得到的编码比用mb_str()函数从const char*转换成char*更加清楚。

wxString mystring(wxT("HelloWorld"));

(const_cast<char*>((const char*)mystring.mb_str()))

1

2

wxStringmystring(wxT("HelloWorld"));

(const_cast<char*>((constchar*)mystring.mb_str()))

在可变参数的函数 (如printf)中用mb_str()函数将无效,但按以下的方法是有效的:

wxString mystring(wxT("HelloWorld"));

printf("%s",mystring.mb_str().data());

1

2

wxStringmystring(wxT("HelloWorld"));

printf("%s",mystring.mb_str().data());

printf("%s", (const char*)mystring.mb_str())

1

printf("%s",(constchar*)mystring.mb_str())

wchar_t* to wxString

const wchar_t* chars = L"Hello world";

wxString mystring(chars);

1

2

constwchar_t*chars=L"Helloworld";

wxStringmystring(chars);

wxString to wchar_t*

请翻阅官方文档的以下方法:

wxString::wc_str()

wxString::wchar_str()

1

2

wxString::wc_str()

wxString::wchar_str()

wxString to TCHAR

TCHAR tCharString[255];

wxString myString(_T("Hello World"));

const wxChar* myStringChars = myString.c_str();

for (int i = 0; i < myString.Len(); i++) {

tCharString[i] = myStringChars [i];

}

tCharString[myString.Len()] = _T('\0');

1

2

3

4

5

6

7

TCHARtCharString[255];

wxStringmyString(_T("HelloWorld"));

constwxChar*myStringChars=myString.c_str();

for(inti=0;i<myString.Len();i++){

tCharString[i]=myStringChars[i];

}

tCharString[myString.Len()]=_T('\0');

int to wxString

wxString mystring = wxString::Format(wxT("%i"),myint);

1

wxStringmystring=wxString::Format(wxT("%i"),myint);

或者

wxString mystring;

mystring << myint;

1

2

wxStringmystring;

mystring<<myint;

float to wxString

wxString mystring = wxString::Format(wxT("%f"), myfloat);

1

wxStringmystring=wxString::Format(wxT("%f"),myfloat);

或者

wxString mystring;

mystring << myfloat;

1

2

wxStringmystring;

mystring<<myfloat;

wxString to integer number

wxString number(wxT("145"));

long value;

if(!number.ToLong(&value)) { /* error! */ }

1

2

3

wxStringnumber(wxT("145"));

longvalue;

if(!number.ToLong(&value)){/* error! */}

或者

wxString str = _T("123");

int num;

num = wxAtoi(str);

1

2

3

4

wxStringstr=_T("123");

intnum;

num=wxAtoi(str);

wxString to floating-point number

wxString number(wxT("3.14159"));

double value;

if(!number.ToDouble(&value)){ /* error! */ }

1

2

3

wxStringnumber(wxT("3.14159"));

doublevalue;

if(!number.ToDouble(&value)){/* error! */}

std::string to wxString

std::string stlstring = "Hello world";

// assuming your string is encoded as UTF-8, change the wxConv* parameter as needed

wxString mystring(stlstring.c_str(), wxConvUTF8);

1

2

3

std::stringstlstring="Helloworld";

// assuming your string is encoded as UTF-8, change the wxConv* parameter as needed

wxStringmystring(stlstring.c_str(),wxConvUTF8);

从wxWidgets 2.9开始, 你可以用适当的构造函数:

std::string stlstring = "Hello world";

// assuming your string is encoded as the current locale encoding (wxConvLibc)

wxString mystring(stlstring);

1

2

3

std::stringstlstring="Helloworld";

// assuming your string is encoded as the current locale encoding (wxConvLibc)

wxStringmystring(stlstring);

wxString to std::string

在wxWidgets 2.8 :

wxString mystring(wxT("HelloWorld"));

std::string stlstring = std::string(mystring.mb_str());

1

2

wxStringmystring(wxT("HelloWorld"));

std::stringstlstring=std::string(mystring.mb_str());

在wxWidgets 2.9, 你可以用这个方法

wxString::ToStdString()

1

wxString::ToStdString()

std::wstring to wxString

从wxWidgets 2.9开始, 你可以用适当的构造函数:

std::sstring stlstring = L"Hello world";

// assuming your string is encoded as the current locale encoding (wxConvLibc)

wxString mystring(stlstring);

1

2

3

std::sstringstlstring=L"Helloworld";

// assuming your string is encoded as the current locale encoding (wxConvLibc)

wxStringmystring(stlstring);

wxString to std::wstring

在wxWidgets 2.9, 你可以用这个方法

wxString::ToStdWstring()

1

wxString::ToStdWstring()

附:(数据库类型)

wxString 转 _bstr_t

wxString str(wxT("Hello"));

_bstr_t mystring=_bstr_t(str.wc_str());

1

2

wxStringstr(wxT("Hello"));

_bstr_tmystring=_bstr_t(str.wc_str());

_bstr_ 转 wxString

_bstr_t bstr="hello";

wxString mystring = wxString(static_cast<const wchar_t *>(bstr));

1

2

_bstr_tbstr="hello";

wxStringmystring=wxString(static_cast<constwchar_t*>(bstr));

_variant_t 转 wxString

_variant_t  varstr=_variant_t("Hello");

wxString mystring=wxString(static_cast<const wchar_t *>(_bstr_t(varstr)));

1

2

_variant_t varstr=_variant_t("Hello");

wxStringmystring=wxString(static_cast<constwchar_t*>(_bstr_t(varstr)));

wxString 转 _variant_t

wxString str(wxT("Hello"));

_variant_t myvar=_variant_t(str.wc_str());

1

2

wxStringstr(wxT("Hello"));

_variant_tmyvar=_variant_t(str.wc_str());

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值