Answer the question
In order to leave comments, you need to log in
How exactly to write a float to a binary file?
Greetings. I encountered an unpleasant bug when I record a float at an address in someone else's exe / dll. I look at the result with a hex editor, the value of the numbers after the decimal point and their number are different.
For example, I set the number to patch = 2.01276 and it is patched to 2.0127599239e + 0
I do this
void Patch(string file_path, string tp, string val, long addr) {
BinaryWriter bw = new BinaryWriter(File.Open(file_path, FileMode.Open, FileAccess.ReadWrite));
bw.BaseStream.Seek(addr, SeekOrigin.Begin);
bw.Write(Convert.ToSingle(val.Replace('.', ',')));
bw.Close();
}
Answer the question
In order to leave comments, you need to log in
We BinaryWriter
have a method Write(float arg)
, and we don't need to convert anything to a string.
Have you tried BitConverter?
Remarks
The order of bytes in the array returned by the GetBytes method depends on whether the computer architecture of the elements is or little endian.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question