Article View: borland.public.cppbuilder.nativeapi
Article #1379Re: UTF-8 to curent locale
From: Darko Miletic
Date: Mon, 10 Sep 2007 19:50
Date: Mon, 10 Sep 2007 19:50
53 lines
3128 bytes
3128 bytes
Asger Jorgensen wrote:
>
> Is there a way to get the Utf8ToAnsi() function, for use in my BCB5 ??
Not unless you write it yourself.
But here is a STL implementation I wrote
#include <windows.h>
#include <string>
#include <vector>
#include <iostream>
std::string utf82ansi (const std::string &str, UINT codepage = CP_ACP) {
std::string result;
if (!str.empty()) {
int buffLen = ::MultiByteToWideChar( CP_UTF8,
0U,
str.c_str(),
str.size(),
NULL,
0);
if (buffLen > 0) {
std::vector<wchar_t> buffer(buffLen);
std::size_t res = ::MultiByteToWideChar( CP_UTF8,
0U,
str.c_str(),
str.size(),
&buffer[0],
buffer.size());
if (res > 0) {
std::vector<char> sbuffer (buffLen);
res = ::WideCharToMultiByte( codepage,
0U,
&buffer[0],
buffer.size(),
&sbuffer[0],
sbuffer.size(),
NULL,
NULL);
if (res > 0) {
result.assign(sbuffer.begin(),sbuffer.end());
}
}
}
}
return result;
}
int main() {
std::cout << utf82ansi("<some utf-8 text>") << std::endl;
return 0;
}
Technical Details
Message-ID:
<46e5a024@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!news1.google.com!news.glorb.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mailReferences:
<MPG.214e8e1e8b10f7259897e4@newsgroups.borland.com> <46e54543@newsgroups.borland.com> <MPG.214f7a08942d25809897e5@newsgroups.borland.com> <m23axmtstn.fsf@glue.ch> <MPG.214fa756a89c2c9e9897e6@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,1467a6fdb7f4c516 X-Google-Attributes: gid10cc67,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Date: Mon, 10 Sep 2007 16:50:58 -0300 From: Darko Miletic <kiklop@fibertel.com.ar> Reply-To: Darko Miletic <kiklop@fibertel.com.ar> User-Agent: Thunderbird 2.0.0.6 (Windows/20070728) MIME-Version: 1.0 Newsgroups: borland.public.cppbuilder.nativeapi Subject: Re: UTF-8 to curent locale References: <MPG.214e8e1e8b10f7259897e4@newsgroups.borland.com> <46e54543@newsgroups.borland.com> <MPG.214f7a08942d25809897e5@newsgroups.borland.com> <m23axmtstn.fsf@glue.ch> <MPG.214fa756a89c2c9e9897e6@newsgroups.borland.com> In-Reply-To: <MPG.214fa756a89c2c9e9897e6@newsgroups.borland.com> Content-Type: text/plain; charset=ISO-8859-15; format=flowed Content-Transfer-Encoding: 7bit X-Antivirus: avast! (VPS 000774-1, 10/09/2007), Outbound message X-Antivirus-Status: Clean NNTP-Posting-Host: 200.49.157.163 Message-ID: <46e5a024@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1189453860 200.49.157.163 (10 Sep 2007 12:51:00 -0700) Lines: 53 Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!news1.google.com!news.glorb.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail