Char and Chr in Delphi

The difference between Chr and Char when used in conversion types is that it is a function and the other is cast

So: Char(66) = Chr(66)

I don’t think there is a difference in performance (at least I’ve never noticed it, probably another) ... I’m sure that someone will correct me about this!

EDIT Thanks to Ulrich for the test proving that they are actually identical.
EDIT 2 Can someone think of a case where they may not be identical, for example. Are you forced to use one of them due to context?

What do you use in your code and why?

+4
source share
6 answers

In D2007, I did a little test:

 program CharChr; {$APPTYPE CONSOLE} uses Windows; function GetSomeByte: Byte; begin Result := Random(26) + 65; end; procedure DoTests; var b: Byte; c: Char; begin b := GetSomeByte; IsCharAlpha(Chr(b)); b := GetSomeByte; IsCharAlpha(Char(b)); b := GetSomeByte; c := Chr(b); b := GetSomeByte; c := Char(b); end; begin Randomize; DoTests; end. 

Both calls create the same assembly code:

 CharChr.dpr.19: IsCharAlpha(Chr(b)); 00403AE0 8A45FF mov al,[ebp-$01] 00403AE3 50 push eax 00403AE4 E86FFFFFFF call IsCharAlpha CharChr.dpr.21: IsCharAlpha(Char(b)); 00403AF1 8A45FF mov al,[ebp-$01] 00403AF4 50 push eax 00403AF5 E85EFFFFFF call IsCharAlpha CharChr.dpr.24: c := Chr(b); 00403B02 8A45FF mov al,[ebp-$01] 00403B05 8845FE mov [ebp-$02],al CharChr.dpr.26: c := Char(b); 00403B10 8A45FF mov al,[ebp-$01] 00403B13 8845FE mov [ebp-$02],al 

Edit: Modified sample to alleviate Nick's concerns.

Edit 2: Nick is my team .; -)

+5
source

Help says: Chr returns a character with an ordinal value (ASCII value) byte type expression, X. *

So, how is a character represented in computer memory? Guess what, like a byte *. In fact, the functions Chr and Ord exist only where Pascal is a strongly typed language that prohibits the use of bytes *, where characters are requested. For a computer, the resulting char is still represented as a byte * - what will it be converted to then? Actually, there is no code for this function call, just as there is no code omitted for the type. Ergo: no difference.

You may prefer chr to avoid typecasting.

Note. Type casting should not be confused with explicit type conversions! In Delphi 2010, something like Char(a) is written, and an - AnsiChar, actually does something.

** For Unicode, replace the byte with an integer *

Edit:

Just an example to make it clear (assuming non-Unicode):

 var a: Byte; c: char; b: Byte; begin a := 60; c := Chr(60); c := Chr(a); b := a; end; 

creates a similar code

 ftest.pas.46: a := 60; 0045836D C645FB3C mov byte ptr [ebp-$05],$3c ftest.pas.47: c := Chr(60); 00458371 C645FA3C mov byte ptr [ebp-$06],$3c ftest.pas.48: c := Chr(a); 00458375 8A45FB mov al,[ebp-$05] 00458378 8845FA mov [ebp-$06],al ftest.pas.49: b := a; 0045837B 8A45FB mov al,[ebp-$05] 0045837E 8845F9 mov [ebp-$07],al 

Assigning a byte to byte actually matches the assignment of a char byte via CHR ().

+3
source

chr is a function, so it returns a new value of type char .

char(x) is a cast, which means that the actual object x , but as a different type.

Many system functions, such as inc, dec, chr, ord, are built-in.

Both char and chr are fast. Use the one that is most suitable every time,
and better reflects what you want to do.

+2
source

Chr is a function call, it is a bit (tiny tiny) more expensive than a type. But I think Chr is built in by the compiler.

0
source

They are identical, but they do not have to be the same. There is no requirement that the internal representation of characters display 1-to-1 with their ordinal values. Nothing says that the Char variable containing the value 'A' must contain the numeric value 65. The requirement is that when Ord called for this variable, the result must be 65, because the code point indicated by the letter A encoded by your program code.

Of course, the simplest implementation of this requirement is that the variable must also contain a numeric value of 65. Because of this, function calls and role types are always identical.

If the implementation was different, then when you called Chr(65) , the compiler will understand which character is at point 65 of the code and use it as the result. When you write Char(65) , the compiler will not worry about what character it actually represents if the numerical result stored in memory is 65.

Is it splitting hair? Yes, absolutely, because in all current implementations they are identical. I liken this to the fact that the null pointer is necessarily zero. This is not the case, but with all implementations it ends anyway.

0
source

chr typeafe, char no: try the chr(256) code and you will get a compiler error. Try entering the char(256) code and you will either get a character with an ordinal value of 0 or 1, depending on the internal representation of your integers.

I will suffix above by saying that this applies to pre-unicode Delphi. I don't know if chr and char have been updated to account for unicode.

0
source

Source: https://habr.com/ru/post/1304556/


All Articles