Recent

Author Topic: Character variables and other languages [SOLVED]  (Read 2112 times)

pascal111

  • Sr. Member
  • ****
  • Posts: 282
  • Un trabajo en equipo para programas serias.
Character variables and other languages [SOLVED]
« on: May 07, 2021, 06:18:01 pm »
عند مُحاولتي لتعيين قيمة لمُتغيّر لبنة بحرف من لغةٍ أخرى فإنّ ذلكـ عُدّ خطأً من قِبل المُنقّح فهل مُتغيّرات اللبنة لا تقبل إلّا لبنات الآسكي القياسيّة فقط؟


google translate:

"When trying to assign a value to a block "character" variable with a letter from another language, this was considered a mistake by the reviser "debugger". Are the block "character" variables only accept standard ASCII blocks "characters"?"

Code: Pascal  [Select][+][-]
  1. var
  2.   c:char;
  3.   i:integer;
  4.   s:string;
  5.  
  6. begin
  7.  
  8.   c:='ل';
  9.   i:=ord(c);
  10.   str(i,s);
  11.  
  12.   showmessage(s);
  13.  
  14. end;          
  15.  
  16.  
« Last Edit: May 07, 2021, 07:24:52 pm by pascal111 »

Bart

  • Hero Member
  • *****
  • Posts: 4282
    • Bart en Mariska's Webstek
Re: Character variables and other languages
« Reply #1 on: May 07, 2021, 06:31:47 pm »
Char is just a single byte.
So there are only 256 possible values of a Char.
Depending on your system codepage, Char's with a byte value > 127 have differnet meaning.

The text you have on the top of your message cannot be represented with 1-byte chars.
If you paste 1 "block" as you call it into the source editor, it will consist of multiple bytes.

Therefore if you need a variable to hold such a "block", the variable needs to be of the type String or UnicodeString.

Bart

pascal111

  • Sr. Member
  • ****
  • Posts: 282
  • Un trabajo en equipo para programas serias.
Re: Character variables and other languages
« Reply #2 on: May 07, 2021, 06:44:46 pm »

If you paste 1 "block" as you call it into the source editor, it will consist of multiple bytes.


فهمت أي أنّ مُتغيّر اللبنة تعدّى الواحد بايت المُخصّص لمُحتواه.

google translate:

"I understood that the block "character" variable exceeded the 1-byte assigned to its content."


Google called it "block", not me  8)

I'll use "String" or "UnicodeString".

Thaddy

  • Hero Member
  • *****
  • Posts: 10783
Re: Character variables and other languages
« Reply #3 on: May 07, 2021, 06:47:02 pm »
Char is just a single byte.
No, it isn't. <first tme this year grumpy mode  >:D >:D >:D >

Bart, you are smart and you know that.

wp

  • Hero Member
  • *****
  • Posts: 8353
Re: Character variables and other languages
« Reply #4 on: May 07, 2021, 07:09:25 pm »
Thaddy, run this program and tell us what is displayed on the monitor

Code: Pascal  [Select][+][-]
  1. program Project1;
  2. begin
  3.   WriteLn(SizeOf(char));
  4. end.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

pascal111

  • Sr. Member
  • ****
  • Posts: 282
  • Un trabajo en equipo para programas serias.
Re: Character variables and other languages
« Reply #5 on: May 07, 2021, 07:13:20 pm »
Char is just a single byte.
No, it isn't. <first tme this year grumpy mode  >:D >:D >:D >

Bart, you are smart and you know that.



ماذا؟!!

إنّهُ من المنطقي أنّ مُتغير اللبنة لا يحوي قيمة أكبر من 255 وهي القيمة العظمى ﻷكواد الـ Ascii والتي لا تشغل إلا بايت واحد فقط كما هو المُعتاد منذ أيّام الـ DOS.

هل هناكـ أي تحديثات على ذلكـ في الـ Lazarus freePascal وفي الأنظمة الحديثة الحالية والتي تحوي لغات بشريّة مُتعددة.


google translate:

"What?!!

It is logical that the block variable does not contain a value greater than 255, which is the maximum value for Ascii codes, which only occupy one byte as usual since the days of DOS.

Are there any updates on that - in the Lazarus freePascal and in the current modern systems that contain multiple human languages."

pascal111

  • Sr. Member
  • ****
  • Posts: 282
  • Un trabajo en equipo para programas serias.
Re: Character variables and other languages
« Reply #6 on: May 07, 2021, 07:21:07 pm »

أظنّ أنّ wp@ على صواب حيث نتيجة الكود التالي كما قال ،حجم نوع اللبنة بايت واحد وواحد فقط.



google translate:

"I think wp @ is correct as the result of the following code as it said, the size of the type of block is only one and one byte."


Code: Pascal  [Select][+][-]
  1. showmessage(inttostr(sizeof(char)));
  2.  

Thaddy

  • Hero Member
  • *****
  • Posts: 10783
Re: Character variables and other languages
« Reply #7 on: May 07, 2021, 07:24:43 pm »

أظنّ أنّ wp@ على صواب حيث نتيجة الكود التالي كما قال ،حجم نوع اللبنة بايت واحد وواحد فقط.



google translate:

"I think wp @ is correct as the result of the following code as it said, the size of the type of block is only one and one byte."


Code: Pascal  [Select][+][-]
  1. showmessage(inttostr(sizeof(char)));
  2.  

No, it is not. It can be anything between 1 and 4 bytes depending on mode....
btw Bart will tell you the same.

pascal111

  • Sr. Member
  • ****
  • Posts: 282
  • Un trabajo en equipo para programas serias.
Re: Character variables and other languages
« Reply #8 on: May 07, 2021, 07:35:33 pm »
No, it is not. It can be anything between 1 and 4 bytes (depending on mode....)
btw Bart will tell you the same.

أعتقد أنّكـ تعني باستخدام بعض مُوجّهات المُترجم directives يُمكن تكييف mode ما.


google translate:

"I think you mean by using some "compiler directives" directives a mode (of something) can be adapted."

wp

  • Hero Member
  • *****
  • Posts: 8353
Re: Character variables and other languages
« Reply #9 on: May 07, 2021, 07:52:10 pm »
No, it is not. It can be anything between 1 and 4 bytes depending on mode....
btw Bart will tell you the same.
OK - {$mode delphiunicode} makes it a widechar with two bytes (I don't know how to invoke a three-byte or four-byte char...).

But Bart's answer exactly clarified the point which the OP was misunderstanding. Your uncommented answer only caused confusion again.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

pascal111

  • Sr. Member
  • ****
  • Posts: 282
  • Un trabajo en equipo para programas serias.
Re: Character variables and other languages [SOLVED]
« Reply #10 on: May 07, 2021, 08:09:02 pm »
لقد جرّبت توجيه المُترجم الـ {$mode delphiunicode} ولكن لم يُسمح به إلا في قسم الـ  implementation وبالرغم من ذلكـ كانت قيمة الكود التالي هي كذلكـ كما كانت مُجرّد بايت واحد وواحد فقط.



google translate:

"I tried the compiler directive {$ mode delphiunicode}, but it was only allowed in the implementation section. However - the value of the following code was the same - just as it was just one (and one only) byte"

Code: Pascal  [Select][+][-]
  1. showmessage(inttostr(sizeof(char)));
  2.  

wp

  • Hero Member
  • *****
  • Posts: 8353
Re: Character variables and other languages [SOLVED]
« Reply #11 on: May 07, 2021, 09:59:55 pm »
Only a single $mode directive is allowed per unit (https://www.freepascal.org/docs-html/user/userse33.html). A Lazarus unit by default usually gets a {$mode objfpc} - replace it by {$mode delphiunicode} and your Showmessage will report "2".
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

pascal111

  • Sr. Member
  • ****
  • Posts: 282
  • Un trabajo en equipo para programas serias.
Re: Character variables and other languages [SOLVED]
« Reply #12 on: May 08, 2021, 01:15:42 am »
Only a single $mode directive is allowed per unit (https://www.freepascal.org/docs-html/user/userse33.html). A Lazarus unit by default usually gets a {$mode objfpc} - replace it by {$mode delphiunicode} and your Showmessage will report "2".

صحيحٌ قولكـ! لقد جرّبتُ ذلكـ الآن ،فلابدّ من مُوجّهٍ واحدٍ فقط للـ mode.

هذه المرّة إثنان من البايتات وقد علمتُ ممّا سبق أنّ هناكـ أكثر من ذلكـ إلى أربعة بايتات كاملة.

google translate:

"It is true that you say! I've tried it - now, only one router "directive" is required for the mode.

This time there are two bytes, and I learned "knew" from the above that there are more than that - up to four full bytes."

Bart

  • Hero Member
  • *****
  • Posts: 4282
    • Bart en Mariska's Webstek
Re: Character variables and other languages
« Reply #13 on: May 08, 2021, 02:08:51 pm »
Char is just a single byte.
No, it isn't. <first tme this year grumpy mode

Bart, you are smart and you know that.

I am not even going to dignify you with an answer.
Paradox intended.

Bart

PascalDragon

  • Hero Member
  • *****
  • Posts: 3057
  • Compiler Developer
Re: Character variables and other languages
« Reply #14 on: May 08, 2021, 03:15:50 pm »
No, it is not. It can be anything between 1 and 4 bytes depending on mode....
btw Bart will tell you the same.

Wrong. It's either 1 or 2, but never 4 (this is not C++ where wchar_t is either 2 or 4 depending on the platform).

It is 1 by default and 2 if either the mode is DelphiUnicode or the modeswitch UnicodeStrings is enabled.

Or more precisely: by default Char is an alias to AnsiChar and otherwise it's an alias to WideChar.

 

TinyPortal © 2005-2018