Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2015-11

FPC 3.x for Windows and code page conversion

5 messages · 4 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Lubos Pintes

Hello,
I am developing a console application which receives an UTF-8 encoded text through stdin.
The text is, after possible modification, sent to SAPI5. The SAPI5 generated interface wrappers have parameters of type string. In FPC 2.6.4, I used UTF8toANSI on various places and that worked well. Now after reading about FPC 3.x's unicode support, I thought that adding this line say after begin in the main program could be enough: SetMultiByteConversionCodePage(CP_UTF8);
It however doesn't work.
How can I solve this without conversion to ANSI?
Thanks

Reply by email

Mattias Gaertner
to Lubos Pintes
Show quoted text 9 lines
Hello, I am developing a console application which receives an UTF-8 encoded text through stdin. The text is, after possible modification, sent to SAPI5. The SAPI5 generated interface wrappers have parameters of type string. In FPC 2.6.4, I used UTF8toANSI on various places and that worked well. Now after reading about FPC 3.x's unicode support, I thought that adding this line say after begin in the main program could be enough: SetMultiByteConversionCodePage(CP_UTF8);

The above setting makes UTF8ToAnsi to convert the string to UTF-16 and back to UTF-8.

It however doesn't work. How can I solve this without conversion to ANSI?

Do you want to convert an UTF8 string to the system codepage? Are you using any Lazarus units?

Mattias

Reply by email

Jonas Maebe
to Lubos Pintes: I am developing a console application which receives an UTF-8 encoded text through stdin.…

Lubos Pintes wrote on Mon, 23 Nov 2015:

Show quoted text 10 lines
I am developing a console application which receives an UTF-8 encoded text through stdin. The text is, after possible modification, sent to SAPI5. The SAPI5 generated interface wrappers have parameters of type string. In FPC 2.6.4, I used UTF8toANSI on various places and that worked well. Now after reading about FPC 3.x's unicode support, I thought that adding this line say after begin in the main program could be enough: SetMultiByteConversionCodePage(CP_UTF8); It however doesn't work.

That routine sets the default code page for ansistrings. It does not change I/O.

How can I solve this without conversion to ANSI?

The code page can be set per I/O channel:
http://www.freepascal.org/docs-html/3.0.0/rtl/system/settextcodepage.html (and Michael: I just noticed a typo on that page, under "Description" it says GetTextCodePage instead of SetTextCodePage)

Jonas

Reply by email

Lubos Pintes
to Jonas Maebe

Hello,
Thanks for replies, I will try to explain this in other way: My system code page is windows-1250. When a string is sent to a COM component, it is somehow converted to UTF-16. Thus FPC knows how to do the conversion. Now I know that I am receiving text in UTF-8. Thus I called UTF8ToANSI to uselessly convert the UTF-8 to ANSI and then FPC converted it to UTF-16 to make COM happy.
Although it worked well in many cases, a data loss may occur. So to reformulate this question, how to avoid this useless conversion step? I thought that setting the default code page to UTF-8 just do the trick.

Dňa 23. 11. 2015 o 17:29 Jonas Maebe napísal(a):

Show quoted text 31 lines
Lubos Pintes wrote on Mon, 23 Nov 2015: > I am developing a console application which receives an UTF-8 encoded > text through stdin. > The text is, after possible modification, sent to SAPI5. The SAPI5 > generated interface wrappers have parameters of type string. > In FPC 2.6.4, I used UTF8toANSI on various places and that worked > well. Now after reading about FPC 3.x's unicode support, I thought > that adding this line say after begin in the main program could be > enough: > SetMultiByteConversionCodePage(CP_UTF8); > It however doesn't work. That routine sets the default code page for ansistrings. It does not change I/O. > How can I solve this without conversion to ANSI? The code page can be set per I/O channel: http://www.freepascal.org/docs-html/3.0.0/rtl/system/settextcodepage.html (and Michael: I just noticed a typo on that page, under "Description" it says GetTextCodePage instead of SetTextCodePage) Jonas _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply by email

Michael Van Canneyt
to Lubos Pintes
Lubos Pintes wrote 7 lines
Hello, Thanks for replies, I will try to explain this in other way: My system code page is windows-1250. When a string is sent to a COM component, it is somehow converted to UTF-16. Thus FPC knows how to do the conversion. Now I know that I am receiving text in UTF-8. Thus I called UTF8ToANSI to uselessly convert the UTF-8 to ANSI and then FPC converted it to UTF-16 to make COM happy.

Do not convert to Ansi.
You should use unicodestring or widestring variables, and send that to COM.

Although it worked well in many cases, a data loss may occur. So to reformulate this question, how to avoid this useless conversion step? I thought that setting the default code page to UTF-8 just do the trick.

COM only works with UTF16. The best way is to declare all strings that you intend to send to COM to Widestring or UnicodeString.

Michael.

Reply by email