Overview
Features
Download
Documentation
Community
Add-Ons & Services

How to convert an ANSI string to UTF8 string?

A general discussion forum.

How to convert an ANSI string to UTF8 string?

Postby yanweigps » 18 Dec 2010, 21:22

Hi All,

An utf-8 string to initialize is needed to initialized the "Poco::File" object. and my question is how to convert an ANSI string to UTF8 string?

std::string ansiString = "我是中国人"; // In chinese

ASCIIEncoding ascii;
UTF8Encoding utf8;
TextConverter TC(ascii,utf8);

string utf8String;
TC.convert(ansiString,utf8String);

The above converting method doesn't work. I find some code from the internet, but I want to know how can I do it with POCO?

Thank you in advance.

Regards,

Wei Yan
yanweigps
 
Posts: 3
Joined: 18 Dec 2010, 21:03

Re: How to convert an ANSI string to UTF8 string?

Postby lilinghui » 20 Dec 2010, 05:58

use class UnicodeConverter
lilinghui
 
Posts: 7
Joined: 11 Jan 2010, 06:00

Re: How to convert an ANSI string to UTF8 string?

Postby yanweigps » 20 Dec 2010, 08:40

Are you kiding? UnicodeConverter only can do convertion between utf-8 and utf-16.
What I want to know is how to convert an ansi string to utf8 or utf-16 string.

In other words, how to convert ''std::string" to "std::wstring" in Poco?
yanweigps
 
Posts: 3
Joined: 18 Dec 2010, 21:03

Re: How to convert an ANSI string to UTF8 string?

Postby guenter » 20 Dec 2010, 08:50

First, the string you want to convert is not an ANSI string as ANSI strings can, due to their encoding, not contain Chinese characters. So you're probably using some kind of Chinese encoding. What you need to do is to use the Poco::TextConverter and Poco::TextEncoding classes. Unfortunately, the current POCO release does not contain a TextEncoding for Chinese encodings. So you either have to write one yourself (or search the feature requests on Sourceforge - if I remember correctly, some time ago a patch was posted that implements one of the Chinese GB... encodings), or make sure you use UTF-8 encoded source files.
guenter
 
Posts: 1135
Joined: 11 Jul 2006, 16:27
Location: Austria

Re: How to convert an ANSI string to UTF8 string?

Postby yanweigps » 03 Jan 2011, 03:58

Thank you very much.

All my source code were stored in ansi file. And I found the code from the internet, and it worked.

static std::wstring ANSIToUTF16(std::string s)
{
std::string curLocale = setlocale(LC_ALL, NULL);
setlocale(LC_ALL, "chs");

const char* _Source = s.c_str();
size_t _Dsize = s.size() + 1;
wchar_t *_Dest = new wchar_t[_Dsize];
wmemset(_Dest, 0, _Dsize);
mbstowcs(_Dest,_Source,_Dsize);

std::wstring ws = _Dest;
delete []_Dest;

setlocale(LC_ALL, curLocale.c_str());

return ws;
}
yanweigps
 
Posts: 3
Joined: 18 Dec 2010, 21:03


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 2 guests