Page 1 of 1

convert std::wstring to CEGUI::String?

Posted: Sat Jul 30, 2005 09:03
by thelONE
is there a easy way to convert std::wstring to CEGUI::String? do i have to write a conversion function?
thanks!

Re: convert std::wstring to CEGUI::String?

Posted: Sat Jul 30, 2005 12:30
by CrazyEddie
Short answer, no.

The very fact that there is no easy conversion highlights the reason that std::wstring was not used in the first place; it's basically non-portable.

While it's all very nice when you just access wstring itself, there are major issues when you need to do anything like conversions of the wchars - since what exactly a wchar represents is not specified. So, any conversion functions will be platform (actually, compiler) specific.

Re: convert std::wstring to CEGUI::String?

Posted: Sat Jul 30, 2005 13:37
by thelONE
I think that is why you implement your own string class rather than use std::wstring, isn't it?

i'm using msvc7.1(do not consider cross-platform issue currently), and when i display a chinese string on hud, i may do these:

std::wstring wstr = L"昨天曼联输给一个日本球队了,不可思议。";
CEGUI::String ce_str = ENCODING_CONVERSION(wstr);
window->setText(ce_str);

that ENCODING_CONVERSION function may be a easy solution, but i really could not implement this, could you help me? thnaks a lot ;)

Re: convert std::wstring to CEGUI::String?

Posted: Sun Jul 31, 2005 06:10
by gcarlton
Here is a group of C functions which apparently convert between utf-8, utf-16, and utf-32.
http://www.unicode.org/Public/PROGRAMS/CVTUTF

As far as I understand it, utf-16 is wchar_t, and so you should be able to convert between that and utf-8 using these functions.

Re: convert std::wstring to CEGUI::String?

Posted: Mon Aug 01, 2005 06:11
by Sachin
well i used the follwing func to convert from wstring to std::string..& an std::string is directly convertable to CEGUI::string as one of the constructors of CEGUI::string takes std::string as an argument.

string W2S( wstring wstr )
{
std::string res;
int len = wstr.length();
char* c = new char[len+1];

for ( int i=0;i<len;i++)
{
c[i] = wstr[i];
}
c[i] = 0;
res = c;
res[len]=0;

delete[] c;
return res;
}

u may wonder y so much hassle like declaring a new char array etc.......but try w/o it u will know the probs.