Is simple way to convert double value to its bytes representation? I tried using pointers like:
var
double_v:double;
address:^double;
....
double_v:=100;
address:=@double_v;
but every my concepts: how to read thise 8 bytes from address, end with "AV".
Use a variant record
Type
TDoubleAndBytes = Record
case boolean of
false : (dabDouble : Double);
true : (dabBytes : Array [0..7] Of Byte);
end;
Assign the double value to dabDouble and read the bytes through dabBytes
var
myVar : TDoubleAndBytes;
i : integer;
begin
myVar.dabDouble := 100;
for i:=0 to 7 do write(myVar.dabBytes[i]);
In XE3 there are record helpers for simple types, TDoubleHelper
.
This works:
var
d : Double;
i : Integer;
begin
d := 100.0;
for i := 0 to 7 do
WriteLn(d.Bytes[i]);
end;
In XE2 there is a declaration TDoubleRec
, which is an advanced record.
example:
var
dRec : TDoubleRec;
i : Integer;
begin
dRec := 100.0;
for i := 0 to 7 do
WriteLn(dRec.Bytes[i]);
end;
Another common option to access the bytes of a double is to use a typecast
:
type
TDoubleAsByteArr = array[0..7] of byte;
var
d : Double;
i : Integer;
begin
d := 100.0;
for i := 0 to 7 do
WriteLn(TDoubleAsByteArr(d)[i]);
end;
Two examples for using of "absolute" ...
Used as function
function GetDoubleByte(MyDouble: Double; Index: Byte): Byte;
var
Bytes: array[0..7] of Byte absolute MyDouble;
begin
Result := Bytes[Index];
end;
procedure TForm1.Button1Click(Sender: TObject);
var
MyDouble:Double;
DoubleBytes:Array[0..7] of Byte absolute MyDouble; // direct local use
begin
MyDouble := 17.123;
ShowMessage(IntToStr(DoubleBytes[0])); // local usage
ShowMessage(IntToStr(GetDoubleByte(MyDouble,0))); // via function call
end;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With