Thread View: borland.public.cppbuilder.nativeapi
13 messages
13 total messages
Started by Asger Jorgensen
Sun, 09 Sep 2007 22:11
UTF-8 to curent locale
Author: Asger Jorgensen
Date: Sun, 09 Sep 2007 22:11
Date: Sun, 09 Sep 2007 22:11
14 lines
1318 bytes
1318 bytes
Hi there Is there an API function that can translate a UTF-8 string to an asci string in the curent local codepage ? This situation is that I recieve some files from companies here in my country, and because of an international standard in these files, they are in the UTF-8 format. In other words I will have no problem with these files if I could just traslate them before I start working on them. Thanks in advance Asger
Re: UTF-8 to curent locale
Author: Darko Miletic
Date: Mon, 10 Sep 2007 13:23
Date: Mon, 10 Sep 2007 13:23
20 lines
1839 bytes
1839 bytes
Asger Jorgensen wrote: > Hi there > > Is there an API function that can translate a UTF-8 string > to an asci string in the curent local codepage ? > > This situation is that I recieve some files from > companies here in my country, and because of an international > standard in these files, they are in the UTF-8 format. > There is no need for API. VCL offers function Utf8ToAnsi in system unit (that is in BDS 2006). If you have older version of BCB than you can use two api's to accomplish this conversion: MultiByteToWideChar - this one converts multibyte string to unicode WideCharToMultiByte - this one can convert unicode to ansi So with MultiByteToWideChar convert utf-8 to unicode and than with WideCharToMultiByte convert that unicode to ansi string.
Re: UTF-8 to curent locale
Author: Asger Jorgensen
Date: Mon, 10 Sep 2007 14:58
Date: Mon, 10 Sep 2007 14:58
22 lines
1774 bytes
1774 bytes
Hi Darko Thanks for Your answer In article <46e54543@newsgroups.borland.com>, kiklop@fibertel.com.ar says... > There is no need for API. VCL offers function Utf8ToAnsi in system unit > (that is in BDS 2006). If you have older version of BCB than you can use > two api's to accomplish this conversion: I only have BCB5 pro > > MultiByteToWideChar - this one converts multibyte string to unicode > WideCharToMultiByte - this one can convert unicode to ansi > > So with MultiByteToWideChar convert utf-8 to unicode and than with > WideCharToMultiByte convert that unicode to ansi string. Is there realy no function that translate UTF-8 to good old ascii in one pass ?? I would very much like it to be fast. Thanks in advance Asger
Re: UTF-8 to curent locale
Author: Darko Miletic
Date: Mon, 10 Sep 2007 15:55
Date: Mon, 10 Sep 2007 15:55
7 lines
1491 bytes
1491 bytes
Asger Jorgensen wrote: > Is there realy no function that translate UTF-8 to good old ascii > in one pass ?? I would very much like it to be fast. No. Windows is like that. You have to write your own function using these two API's.
Re: UTF-8 to curent locale
Author: Thomas Maeder [TeamB]
Date: Mon, 10 Sep 2007 15:59
Date: Mon, 10 Sep 2007 15:59
32 lines
2411 bytes
2411 bytes
Asger Jorgensen <Junk@Asger-P.dk> writes: > In article <46e54543@newsgroups.borland.com>, kiklop@fibertel.com.ar says... >> >> There is no need for API. VCL offers function Utf8ToAnsi in system unit >> (that is in BDS 2006). If you have older version of BCB than you can use >> two api's to accomplish this conversion: > > I only have BCB5 pro Have you checked whether you have Utf8ToAnsi()? >> MultiByteToWideChar - this one converts multibyte string to unicode >> WideCharToMultiByte - this one can convert unicode to ansi >> >> So with MultiByteToWideChar convert utf-8 to unicode and than with >> WideCharToMultiByte convert that unicode to ansi string. > > Is there realy no function that translate UTF-8 to good old ascii > in one pass ?? I would very much like it to be fast. You don't need a conversion from UTF-8 to ASCII; all ASCII characters have the same code in UTF-8 and ASCII. In your original post, you wrote "to an asci string in the curent local codepage", which is a contradiction, since code pages have characters with codes >= 128, which ASCII doesn't. If you need support for those non-ASCII characters, Utf8ToAnsi() comes in.
Re: UTF-8 to curent locale
Author: Darko Miletic
Date: Mon, 10 Sep 2007 16:27
Date: Mon, 10 Sep 2007 16:27
15 lines
1751 bytes
1751 bytes
Thomas Maeder [TeamB] wrote: > You don't need a conversion from UTF-8 to ASCII; all ASCII characters > have the same code in UTF-8 and ASCII. You don't need a conversion from UTF-8 to ASCII if all charactes in utf-8 string are ascii characters, otherwise this is not true. He said this in his first message: > This situation is that I recieve some files from > companies here in my country, and because of an international > standard in these files, they are in the UTF-8 format. So these files may contain non ascii characters and thus conversion is needed.
Re: UTF-8 to curent locale
Author: Asger Jorgensen
Date: Mon, 10 Sep 2007 18:11
Date: Mon, 10 Sep 2007 18:11
22 lines
1652 bytes
1652 bytes
Hi Thomas Thanks for Your reply In article <m23axmtstn.fsf@glue.ch>, maeder@glue.ch says... > > Have you checked whether you have Utf8ToAnsi()? Yes I have checked and no I dont have it > You don't need a conversion from UTF-8 to ASCII; all ASCII characters > have the same code in UTF-8 and ASCII. Sorry this is my mistake, I am just so use to call it ascii, but we use codepage 850 and we have 3/6 spcial character here in Denmark, but when it comes to names we have a some foreign characters also. So I need to convert UTF-8 to a string of chars in our codepage. Is there a way to get the Utf8ToAnsi() function, for use in my BCB5 ?? Thanks in advance Asger
Re: UTF-8 to curent locale
Author: 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;
}
Re: UTF-8 to curent locale
Author: Asger Jorgensen
Date: Mon, 10 Sep 2007 21:40
Date: Mon, 10 Sep 2007 21:40
8 lines
1138 bytes
1138 bytes
Hi Darko Thank You for the code! That will sure come in handy. Kind regards Asger
Re: UTF-8 to curent locale
Author: Asger Jorgensen
Date: Tue, 11 Sep 2007 16:16
Date: Tue, 11 Sep 2007 16:16
21 lines
1688 bytes
1688 bytes
Hi Darko Just one question > int buffLen = ::MultiByteToWideChar( CP_UTF8, > 0U, > str.c_str(), > str.size(), > NULL, > 0); What does 0U stand for ?? Everything is working very well though, I just like to know what is happening. Thanks again for the code. Kind regards Asger
Re: UTF-8 to curent locale
Author: Pete Fraser
Date: Tue, 11 Sep 2007 16:30
Date: Tue, 11 Sep 2007 16:30
10 lines
1470 bytes
1470 bytes
0 unsigned similarly 0L = 0 long HTH Pete "Asger Jorgensen" <Junk@Asger-P.dk> wrote in message news:MPG.2150ddcffe0fa6339897e9@newsgroups.borland.com... > Hi Darko > > Just one question > What does 0U stand for ??
Re: UTF-8 to curent locale
Author: Thomas Maeder [TeamB]
Date: Tue, 11 Sep 2007 16:33
Date: Tue, 11 Sep 2007 16:33
8 lines
1574 bytes
1574 bytes
Asger Jorgensen <Junk@Asger-P.dk> writes: > Just one question > > What does 0U stand for ?? It's a literal with type unsigned int and value 0. 0 without the suffix would have type int.
Re: UTF-8 to curent locale
Author: Asger Jorgensen
Date: Tue, 11 Sep 2007 18:51
Date: Tue, 11 Sep 2007 18:51
4 lines
1275 bytes
1275 bytes
Thanks for explaining Kind regards Asger
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads