Search code examples
character-encodingpascallazarusfreepascal

Using the # symbol in Pascal to type characters past 127 range


I know that in pascal you can use the # character to display certain characters like tab(#9), carriage return line feed(#13#10), ect.
When I try to do other characters out of the 127 range like #169 it replaces it with a question mark.
How do I change the character encoding in lazarus so I can use this character?


Solution

  • According to Free Pascal:

    The # indicates the control string

    The control string can be used to specify characters which cannot be typed on a keyboard, such as #27 for the escape character.

    See free pascal character strings.

    The above page also mentions:

    It is possible to use other character sets in strings: in that case the codepage of the source file must be specified with the {$CODEPAGE XXX} directive or with the -Fc command line option for the compiler. In that case the characters in a string will be interpreted as characters from the specified codepage.

    For example, If you wanted to encode with UTF-8 you could use (see this page):

    {$CODEPAGE UTF8}
    {$Mode ObjFPC}{$H+}
    

    (If you are on windows you can choose from these code pages.)

    I tested the following example on my mac with the fpc (free pascal compiler):

    program Example;
    {$CODEPAGE utf8}
    {$Mode ObjFPC}{$H+}
    Begin
       writeln('Hello World'#13#10);
       writeln('carriage return line');
       writeln('Example: '#$C3#$A4);
    End.
    

    This example prints out the ä character and is a modification of the examples on the dealing with UTF8 in free pascal page, which will help you a lot with this issue. You may have to adjust the solutions on that page to fit your problem.

    Note, that I was able to access the ä character because I used it's hex value C3A4.