开发者

How to convert a floating point to hexadecimal (and back) in Delphi [duplicate]

开发者 https://www.devze.com 2022-12-19 19:47 出处:网络
This question already has answers here: 开发者_开发知识库 Closed 12 years ago. Possible Duplicate:
This question already has answers here: 开发者_开发知识库 Closed 12 years ago.

Possible Duplicate:

Converting float or negative integer to hexadecimal in Borland Delphi

Is there a function i can use to convert a floating point value to a hexadecimal value and back?


procedure ShowBinaryRepresentation;
var
  S, S2: Single;
  I: Integer;
  D, D2: Double;
  I64: Int64;
begin
  S := -1.841;
  I := PInteger(@S)^;
  OutputWriteLine(Format('Single in binary represenation: %.8X', [I]));

  S2 := PSingle(@I)^;
  OutputWriteLine(Format('Converted back to single: %f', [S2]));

  D := -1.841E50;
  I64 := PInt64(@D)^;
  OutputWriteLine(Format('Double in binary represenation: %.16X', [I64]));

  D2 := PDouble(@I64)^;
  OutputWriteLine(Format('Converted back to double: %f', [D2]));
end;
Single in binary represenation: BFEBA5E3
Converted back to single: -1,84
Double in binary represenation: CA5F7DD860D57D4D
Converted back to double: -1,841E50


Not floating point. There is inttohex to change integers (maybe int64 too) to hex strings.

Probably you need to pry apart the bits in the IEEE format double type and change them to hex which you then concat with a point inbetween.

If this is not the answer you are looking for, please specify what you mean by "hexadecimal value".

If you just want to convert it to an array of e.g. bytes, define a array of byte type with a suitable size (4 for single, 8 for double and 10 extended), and typecast.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号