[fpc-pascal] Printing unicode characters

Nikolay Nikolov nickysn at gmail.com
Sun Dec 1 08:01:24 CET 2024


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



More information about the fpc-pascal mailing list