float to binary in delphi - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

float to binary in delphi

please help me:
1. how to convert floating point (real) type into binary in delphi 7
2. how to get the LSB from the result of number 1

Comments

  • zibadianzibadian Posts: 6,349Member
    : please help me:
    : 1. how to convert floating point (real) type into binary in delphi 7
    : 2. how to get the LSB from the result of number 1
    :
    You can define an array of byte to hold the float, and copy the float into that array using the Move() procedure:
    [code]
    type
    TByteArray = array of byte;

    FloatSize := SizeOf(FloatVar);
    SetLength(ByteArray, FloatSize);
    Move(FloatVar, ByteArray[0], FloatSize);
    [/code]
    Then you can use the array of byte to process the binary further.

    2: The easiest way to do this is to use the and operator:
    [code]
    LSB := SomeInteger and $01;
    [/code]
  • blue_cygnusblue_cygnus Posts: 7Member
    : : please help me:
    : : 1. how to convert floating point (real) type into binary in delphi 7
    : : 2. how to get the LSB from the result of number 1
    : :
    : You can define an array of byte to hold the float, and copy the float into that array using the Move() procedure:
    : [code]
    : type
    : TByteArray = array of byte;
    :
    : FloatSize := SizeOf(FloatVar);
    : SetLength(ByteArray, FloatSize);
    : Move(FloatVar, ByteArray[0], FloatSize);
    : [/code]
    : Then you can use the array of byte to process the binary further.
    :
    : 2: The easiest way to do this is to use the and operator:
    : [code]
    : LSB := SomeInteger and $01;
    : [/code]
    :
    could you give me more spesific example, please...
    thanks a lot =)
  • zibadianzibadian Posts: 6,349Member
    [b][red]This message was edited by zibadian at 2005-6-21 14:51:22[/red][/b][hr]
    : : : please help me:
    : : : 1. how to convert floating point (real) type into binary in delphi 7
    : : : 2. how to get the LSB from the result of number 1
    : : :
    : : You can define an array of byte to hold the float, and copy the float into that array using the Move() procedure:
    : : [code]
    : : type
    : : TByteArray = array of byte;
    : :
    : : FloatSize := SizeOf(FloatVar);
    : : SetLength(ByteArray, FloatSize);
    : : Move(FloatVar, ByteArray[0], FloatSize);
    : : [/code]
    : : Then you can use the array of byte to process the binary further.
    : :
    : : 2: The easiest way to do this is to use the and operator:
    : : [code]
    : : LSB := SomeInteger and $01;
    : : [/code]
    : :
    : could you give me more spesific example, please...
    : thanks a lot =)
    :
    Here is a full working example:
    [code]
    type
    TByteArray = array of byte;
    var
    FloatSize: integer;
    FloatVar: Double;
    ByteArray: TByteArray;
    i: integer;
    FloatBinary: string;
    begin
    FloatVar := 500.5;
    FloatSize := SizeOf(FloatVar);
    SetLength(ByteArray, FloatSize);
    Move(FloatVar, ByteArray[0], FloatSize);
    FloatBinary := '';
    [red] for i := High(ByteArray) downto 0 do [/red]
    FloatBinary := FloatBinary + FastIntToBin(ByteArray[i], 8);
    [blue] FloatVar := 0;[/blue]
    Move(ByteArray[0], FloatVar, FloatSize);
    [blue] Memo1.Text := FloatToStr(FloatVar);[/blue]
    [/code]
    The FastIntToBin() function can be found in the CodePedia.

    I've updated and tested the sample above. It works correctly now. The red line contains the most important change, while the blue lines were used to test it.
  • blue_cygnusblue_cygnus Posts: 7Member
    by the way, how to convert that binary back to float? i have tried but the result not same. thanks anyway =) plz reply ASAP...
  • zibadianzibadian Posts: 6,349Member
    : by the way, how to convert that binary back to float? i have tried but the result not same. thanks anyway =) plz reply ASAP...
    :
    I have answer this in my previous post. I also tested the changes and they produced the same answers. Here is the entire code, which I used to test it:
    [code]
    function FastIntToBin(Num: cardinal; Length: integer): string;
    var
    i: cardinal;
    begin
    i := 1;
    Result := '';
    while (i <= Num) or (system.Length(Result) < Length) do begin
    if Num and i = i then
    Result := '1' + Result
    else
    Result := '0' + Result;
    i := i * 2;
    end;
    end;

    procedure TForm1.Button1Click(Sender: TObject);
    type
    TByteArray = array of byte;
    var
    FloatSize: integer;
    FloatVar1, FloatVar2: Double;
    ByteArray: TByteArray;
    i, j: integer;
    FloatBinary: string;
    begin
    for j := -50000 to 50000 do
    begin
    FloatVar1 := j;
    FloatSize := SizeOf(FloatVar1);
    SetLength(ByteArray, FloatSize);
    Move(FloatVar1, ByteArray[0], FloatSize);
    FloatBinary := '';
    for i := High(ByteArray) downto 0 do
    FloatBinary := FloatBinary + FastIntToBin(ByteArray[i], 8);
    Move(ByteArray[0], FloatVar2, FloatSize);
    if FloatVar1 <> FloatVar2 then
    Memo1.Lines.Add('Error with: '+IntToStr(j));
    end;
    Memo1.Lines.Add('Done');
    end;
    [/code]
Sign In or Register to comment.