[fpc-pascal] converting to UTF8

Mattias Gaertner nc-gaertnma at netcologne.de
Wed Mar 23 10:51:06 CET 2022


On Tue, 22 Mar 2022 04:47:50 -0400
cibersvaa--- via fpc-pascal <fpc-pascal at lists.freepascal.org> wrote:

> Lazarus 2.012
> FPC: 3.2.0
> SVN: 64642
> OS: Windows 10 Pro/win64
> 
> I'm reading from a file with character set win1252, I want to convert
> it to utf8, but I can't.
> 
> procedure TestString;
> var
>    Original:string;
>    Converted:string;
> begin
>    original:='ESPA'#209'A'; //ESPAÑA WIN1252

FPC does not yet understand comments, so maybe it does not know this
literal is cp1252. Add {$codepage cp1252} somewhere at the start of
the unit.

If this is part of a Lazarus application, then String is by
default UTF-8, so your "original" is already converted to UTF-8.

>    Converted:=ansiToUtf8(original);  // converts to
> 'ESPA'#239#191#189'A' // converted Should be 'ESPA'#195#145'A'

If you want to load a string encoded in CP1252, then you
can use from unit lconvencoding:

s:=CP1252ToUTF8(StringFromFile);


> end;
> 
> I've tried playing with strings types, string, rawstring,ansistring,  
> utf8string. No way. Any hint?


Mattias


More information about the fpc-pascal mailing list