-1

How can I convert this UInt64 to a float?

UInt64 MyLongInt = 14250221004866263104;
float My4ByteFloat = MyLongInt;
Console.WriteLine(My4ByteFloat);

This has to do with a memory read I am performing. The value I am retrieving is an UInt64 and corresponds to 14250221004866263104. And I need to convert it to float since I know for a fact that my address is a float. Now when I do it with Cheat Engine it converts it correctly and gives me this result: -7088. When I do it with C# it gives me 1425002E+19.

Now I know that converting 8 bytes to 4 bytes is not the best thing to do and there might be some issues during the process but, since Cheat Engine is doing it and it is getting the right result, then it must be possible.

So anyone know how to convert this UInt64 to float?

Edit: enter image description here

enter image description here

John P.
  • 1,199
  • 2
  • 10
  • 33

2 Answers2

3

There are two problems here:

  • You can't copy a number from a screenshot: you wrote 14250221004866263104 but in your screenshot it is 14253330148871733248.

  • float is a 32bits type, so a 4 byte type, like an int, not a 8 byte type.

Now, if we take only 32 bits we can see that it is:

uint myuint = 0xc5dd8000;

Then with:

[StructLayout(LayoutKind.Explicit)]
public struct SingleToInt32
{
    [FieldOffset(0)]
    public float Single;

    [FieldOffset(0)]
    public int Int32;

    [FieldOffset(0)]
    public uint UInt32;
}

float fl = new SingleToInt32 { UInt32 = myuint }.Single;
xanatos
  • 109,618
  • 12
  • 197
  • 280
1

This should do the trick.

UInt64 MyLongInt = 14250221004866263104;
float My4ByteFloat = BitConverter.ToSingle(BitConverter.GetBytes(MyLongInt), 0);
Console.WriteLine(My4ByteFloat);
user2530266
  • 287
  • 3
  • 18