Re: Printing unicode characters

Nikolay Nikolov via fpc-pascal <[email protected]>
Newsgroups gmane.comp.compilers.free-pascal.general
Message-ID <[email protected]>
On 12/1/24 8:14 AM, Hairy Pixels via fpc-pascal wrote:
> ChatGPT is saying I can  print unicode scalars like that but i don’t 
> see it works and no compiler warnings even. Did it make this up or did 
> I do something wrong?
>
>   Writeln('Unicode scalar 1F496: ', #$1F496); // 💖
This works for me under Linux (Fedora), if I include the unit cwstring. 
Haven't tested other platforms.
>   Writeln('Unicode scalar 1F496: ', WideChar($1F496));  // 💖

This one doesn't work. WideChar is 16-bit, and $1F496 doesn't fit. It 
also produces a warning:

  Warning: range check error while evaluating constants (128150 must be 
between 0 and 65535)


This is because a WideChar represents a single UTF-16 code unit. To 
encode $1F496, you need two UTF-16 code units (high and low surrogate):

   Writeln('Unicode scalar 1F496: ', WideChar((($1F496-$10000) shr 
10)+$D800)+WideChar((($1F496-$10000) and $3FF)+$DC00));  // 💖


More info here:

https://en.wikipedia.org/wiki/UTF-16#Code_points_from_U+010000_to_U+10FFFF


Nikolay

_______________________________________________
fpc-pascal maillist  -  [email protected]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.