Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2002-06

I don't understand value typecast

3 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Luis Del Aguila

In the manual explain that the value typecast doesn't work, when the type size of the expression and the size of the type cast not must be the same. (page 58. ref.pdf)
But this program compile.

Var a:integer;
      b:char;
      c:boolean;
      d:word;
Begin
   a:=integer('A');
   b:=Char(4875);
   c:=boolean(100);
End.

How work the value typecast?
thanks.

Reply by email

Stefan Becker

Hi,
for a fast answer without compiling your example myself I would say that you didn't have "Range Checking" ON.

Maybe that way your example might compile but not run.

I don't know.

Anyway. You use "Type" casting to teach the compiler about (new) memory structures (you want to use) and how to use them.

example:

type
BtrvDate = LongInt;
BtrvTime = LongInt;
String4 = String[4];
FixRec = packed record
null : byte; {1}
buch : char; {2}
EingDt : BtrvDate; {3,4,5,6}
Jnr : word; {7,8 the Nr SampleNr. according to book} grp : word; {9,10}
code : word; {11,12 Anzahl / ParameterNr.} Ab_date: BtrvDate; {13,14,15,16 Abschluß_datum} An_date: BtrvDate; {17,18,19,20}
An_time: BtrvTime; {21,22,23,24}
Typtail: shortint; {25 the kind of tail}
Up_date: BtrvDate; {26,27,28,29 update-date} Up_time: BtrvTime; {30,31,32,33 update-time} uOwner : dword; {34,35,36,37 4 byte}
gOwner : dword; {38,39,40,41 4 byte}
Stat : string4; {42,43,44,45,46}
ApplId : word; {47,48}
end;

Best write what you are trying to do and then maybe try again!

my regards,
Stefan

Earlier message 32 lines
-----Ursprüngliche Nachricht----- Von: Luis Del Aguila An: Datum: Sonntag, 30. Juni 2002 22:31 Betreff: [fpc-pascal]I don't understand value typecast >In the manual explain that the value typecast doesn't work, when the type >size of the expression and the size of the type cast not must be the same. >(page 58. ref.pdf) >But this program compile. > >Var a:integer; > b:char; > c:boolean; > d:word; >Begin > a:=integer('A'); > b:=Char(4875); > c:=boolean(100); >End. > >How work the value typecast? >thanks. > > > > >_______________________________________________ >fpc-pascal maillist - >http://lists.freepascal.org/mailman/listinfo/fpc-pascal >

Reply by email

Jonas Maebe
to Luis Del Aguila: In the manual explain that the value typecast doesn't work, when the type size of the…
Luis Del Aguila wrote 18 lines
In the manual explain that the value typecast doesn't work, when the type size of the expression and the size of the type cast not must be the same. (page 58. ref.pdf) But this program compile. Var a:integer; b:char; c:boolean; d:word; Begin a:=integer('A'); b:=Char(4875); c:=boolean(100); End. How work the value typecast?

I think the manual is incomplete. For ordinal-to-ordinal type casts (integers/chars/booleans/range-types) and possibly also ordinal-to-enumerations and vice versa, this same-size requirement doesn't exist. In such a case,

a) when going from a smaller signed type to a larger signed type (e.g. longint_var := longint(shortint_var)), the effect is the same as when you would do a regular assignment (so if shortint_var contains $ff = -1 above, longintvar will become $ffffffff = -1)
b) when going from a smaller unsigned type to any larger type (e.g. longint_var := longint(byte_var)), the value of the smaller type is simply zero-extended (so if byte_var contains $ff = 255 above, longint_var will become $ff = 255)
c) when going from a larger to a smaller type, you just take the n lower bytes of the larger type and copy them into the smaller type, where n = the size in bytes of the smaller type.

Char, Boolean and all enumeration types (which don't have an explicit lower bound set that's negative) are treated as unsigned.

Jonas

Reply by email