Recent

Author Topic: ansitoutf8 problem  (Read 7646 times)

cd

  • Jr. Member
  • **
  • Posts: 54
ansitoutf8 problem
« on: July 09, 2010, 10:30:44 pm »
here is the code
Code: [Select]
  s:=#$F4#$FB#$E2#$E0;  // prepare an ansi string: 4 chars that are >#128
  showmessage(inttostr(length(s))); // outputs 4 - which is expected
  s:=ansitoutf8(s); // must convert non-latin chars to utf8 => string length should change
  showmessage(inttostr(length(s))); // still outputs 4
  showmessage(s); // outputs string incorrectly: ???? instead of the expected 4-non-latin-characters

  // this works just like expected
  s:='фыва'; // same as #$F4#$FB#$E2#$E0, but utf8 at compile time because of IDE
  showmessage(inttostr(length(s))); // ouputs 8, because these are non-latin chars which are 2 bytes each
  showmessage(s); // ouputs 'фыва' correctly
as i remember such conversion worked well a while ago. has smth changed since that or i've forgotten smth here?
i'm on linux.
« Last Edit: July 09, 2010, 10:33:21 pm by cd »

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1946
Re: ansitoutf8 problem
« Reply #1 on: July 09, 2010, 10:50:55 pm »
No, this means nothing to a UTF-8 system.
A UTF-8 system does not know that these bytes mean a russian encoding.

It you want to convert it, you have to do it explicitely:

Code: [Select]
uses LConvEncoding;    
...
  s:=#$F4#$FB#$E2#$E0;  // prepare an ansi string: 4 chars that are >#128
  s:=ConvertEncoding(s,'cp1251','utf8');
  showmessage(s);

It may work on Windows, when the system encoding is cp1251, but not when it's UTF-8
« Last Edit: July 09, 2010, 10:55:07 pm by theo »

cd

  • Jr. Member
  • **
  • Posts: 54
Re: ansitoutf8 problem
« Reply #2 on: July 10, 2010, 05:53:32 pm »
thanks man!
and shame on me for being so supidly naive!

asdf

  • Sr. Member
  • ****
  • Posts: 310
Re: ansitoutf8 problem
« Reply #3 on: December 23, 2010, 11:44:24 am »
Quote
s:=ConvertEncoding(s,'cp1251','utf8');

Is it equivalent to s:= cp1251toutf8(s); ?
Lazarus 1.2.4 / Win 32 / THAILAND

 

TinyPortal © 2005-2018