Converting float or negative integer to hexadecimal in Borland Delphi

前端 未结 4 2124
南旧
南旧 2021-01-23 22:51

Ive written a program that communicate with some hardware using a serial connection. It sends a lot of hexadecimal values my way (sensor readings) and every once in a while it s

4条回答
  •  没有蜡笔的小新
    2021-01-23 23:13

    If you want to separate the exponent and the significand, you can use a variant record:

      TExtRec = packed record
        case Boolean of
          false:
            (AValue: Extended);
          true:
            (ASignificand: uint64; ASignExp: uint16;)
      end;
    

    I think this helps to understand the structure of the floating point number.

    Example usage:

    var
      r: TExtRec;
    begin
      r.AValue := 123.567;
      ShowMessage(IntToHex(r.ASignExp) + IntToHex(r.ASignificand));
    end;
    

    Output:

     4005F7224DD2F1A9FBE7
    

    You can calculate it back:

    v = (-1)s * 2(e-16383) * (i.f)
    

    With

    • e = $4005 = 16389 and
    • i.f = $F7224DD2F1A9FBE7
    • i.f = 1.930734374999999999954029827886614611998084001243114471435546875
    • v=123.566999999999999997057908984743335167877376079559326171875

    To convert i.f, i've used a binary converter.

提交回复
热议问题