Lazarus

Programming => General => Topic started by: GypsyPrince on April 05, 2020, 09:39:24 pm

Title: [SOLVED] Assigning a literal value to a Char typed constant...
Post by: GypsyPrince on April 05, 2020, 09:39:24 pm
This constant declaration as String type using a literal value works (for me).
 
Code: Pascal  [Select][+][-]
  1. Const DWG_ALT198 : String = '╞';

However, this declaration as Char type using a literal does not work.

Code: Pascal  [Select][+][-]
  1. Const DWG_ALT198 : Char = '╞';

Now... this decalaration without typing using a literal does work.

Code: Pascal  [Select][+][-]
  1. Const DWG_ALT198 = '╞';

So, am I to surmise from this experiment that I can assign literal values to String typed constants, but not to Char typed constants? Or, am I just declaring this totally wrong?

The following method works (partially)...

Code: Pascal  [Select][+][-]
  1. Const DWG_ALT198 = #198;

This last declaration does not produce an error, but it does not give me the same wide character (╞) as does using the literal. It gives me a character which equates to Chr(198) when I need the one which equates to ChrW(198).

Any thoughts on what I might possibly be doing incorrect here?
Title: Re: Assigning literal values to constants...
Post by: winni on April 05, 2020, 09:43:19 pm
Hi!



is   Codepoint  U+255E
Hex = E2959E
Decimal = 14849438

Lazarus is UTF8!

Winni
Title: Re: Assigning a literal value to a Char typed constant...
Post by: GypsyPrince on April 05, 2020, 10:04:43 pm
@winni

Quote


is   Codepoint  U+255E
Hex = E2959E
Decimal = 14849438

Lazarus is UTF8!

Ah, that makes complete sense.

I think I remember from an earlier discussion about assigning hexadecimal values to constants.
Am I remembering this correctly?

Using "Const DWG_ALT198 = '╞';" works well enough for me.  But just for curiosity's sake, can I assign this value to a Char typed constant using a hexadecimal value? Something similar to:

Quote
Const DWG_ALT198 : Char = Hex(198);

This last statement won't work, but is just a visual example to clarify what I am asking. Again, this is only experimentation and testing Pascal's boundaries. So, it may not actually be worth putting a lot of thought into.
Title: Re: Assigning a literal value to a Char typed constant...
Post by: winni on April 05, 2020, 10:40:28 pm
Hi!

The correct way to assign a hex value to a byte /integer /word/ ....

is since decades

Code: Pascal  [Select][+][-]
  1. myByte = $FF;

which means 255 decimal.

You should not try to assign an UTF8-char to a char:
In the most cases it is longer than one byte.

I don't want to repeat the endless UTF8 discussions from some years ago, so here is a introduction about UTF8 and fpc/Lazarus:

https://wiki.freepascal.org/Unicode_Support_in_Lazarus (https://wiki.freepascal.org/Unicode_Support_in_Lazarus)

Winni

Title: Re: Assigning a literal value to a Char typed constant...
Post by: GypsyPrince on April 05, 2020, 11:04:33 pm
Thanks for that info!!

Your are correct. The information at that link said I need to use a String type constant (or variable) with extended ASCII codes such as that character. It also said that a Char type could be forced to use them, but required a bit of effort and only works with MS Windows, which would not work with my goal of being cross-platform.

So... String type it is.
Title: Re: [SOLVED] Assigning a literal value to a Char typed constant...
Post by: winni on April 05, 2020, 11:16:16 pm
If you want to have a look a the different code blocks of UTF8 then look at
https://www.utf8-chartable.de/unicode-utf8-table.pl (https://www.utf8-chartable.de/unicode-utf8-table.pl)

In the third row you got a comboBox with the different codeblocks - which is not so obvious due to bad HTML design.

Your "beloved " '╞' is in the block BoxDrawing, which is placed between U+2500 and U+257F.


Winni


Title: Re: Assigning a literal value to a Char typed constant...
Post by: lucamar on April 06, 2020, 09:12:33 am
But just for curiosity's sake, can I assign this value to a Char typed constant using a hexadecimal value? Something similar to:

Quote
Const DWG_ALT198 : Char = Hex(198);

Yes, you can:
Code: Pascal  [Select][+][-]
  1. Const DWG_ALT198 : Char = #$C6;
or you can use the intrinsic Chr():
Code: Pascal  [Select][+][-]
  1. Const DWG_ALT198 : Char = Chr($C6);
TinyPortal © 2005-2018