| View previous topic :: View next topic |
| Author |
Message |
seesix How do I cheat?
Reputation: 0
Joined: 20 Aug 2009 Posts: 2
|
Posted: Thu Aug 20, 2009 11:42 am Post subject: [delphi] 4-byte floats |
|
|
with c++, a float is 4 bytes.
as a dword, the float value of 383.2284 would look like 0x43bf9d3b.
i'm using the 'single' type in delphi to store a 4-byte float value from a file structure - however, the value isn't calculated correctly:
| Code: | var x : single;
begin
x := $43bf9d3b;
ShowMessage(Format('%f', [x]));
end; |
the message shows "1136631040.00" - which looks similar to a lousy int conversion...
help!
|
|
| Back to top |
|
 |
DanielG Expert Cheater
Reputation: 1
Joined: 13 May 2009 Posts: 130 Location: The Netherlands
|
Posted: Thu Aug 20, 2009 1:10 pm Post subject: |
|
|
I've never used delphi, but could you try this?
| Code: | var x : single;
var y : single;
begin
x := $43bf9d3b;
y := 383.2284;
ShowMessage(Format('x fixed: %f', [x]));
ShowMessage(Format('x hex: %x', [x]));
ShowMessage(Format('y fixed: %f', [y]));
ShowMessage(Format('y hex: %x', [y]));
end; |
I'm interested if the 'x hex' and 'y hex' are the same, the problem could be there.
|
|
| Back to top |
|
 |
seesix How do I cheat?
Reputation: 0
Joined: 20 Aug 2009 Posts: 2
|
Posted: Thu Aug 20, 2009 1:41 pm Post subject: |
|
|
thank you for replying.
unfortunately, you can't format as another type / on-the-fly like that
edit
(with a clearer head:)
it seemed i was just casting x as a large number (only in hex).
i ended up hacking together the following function:
| Code: |
procedure SingleAsDWORD(var outSingle : Single; inDWORD : DWORD);
begin
CopyMemory(@outSingle, @inDWORD, 4);
end;
|
instead of just setting the single type's value _equal_ to the dword, it copies it to where it would be accessed - which returns the correct float value!
solved
|
|
| Back to top |
|
 |
Deltron Z Expert Cheater
Reputation: 1
Joined: 14 Jun 2009 Posts: 164
|
Posted: Sat Aug 22, 2009 9:23 am Post subject: |
|
|
0x43BF9D3B = 1136631099.
It converted int to float automatically, why didn't you simply do:
x := 383.2284?
Or, you could also use pointers. cast 0x43BF9D3B to a pointer to float and then dereference it.
|
|
| Back to top |
|
 |
|