Typecasting Cardinal to Single

delphi, delphi-10.1-berlin

Solution

I would do it like this:

lSingleVar := PSingle(@lCardinalVar)^;

And in the opposite direction it would be:

lCardinalVar := PCardinal(@lSingleVar)^;

I cannot think of a more direct way to achieve what you request. To my mind this has the advantage of not requiring any type or function definitions.

Problem

Both `Cardinal` and `Single` are 4 byte / 32 bit datatypes, but when I typecast them to each other I get an `Invalid Typecast` error in Delphi 10.1 (Berlin). ``` lSingleVar := Single(lCardinalVar); ``` I am NOT talking about converting between the two types, as that will only store 23 bits of the cardinal data (the fraction part of the Single datatype). I need to store minimum 30 bits of data into a `Single` variable. I have good reasons for that (the type cannot be changed), which I will be pleased to elaborate on. How do I typecast a `Single` variable?

Original source