V
V
vari0us2016-09-10 14:13:39
C++ / C#
vari0us, 2016-09-10 14:13:39

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();
}

I need the float patched in the file to be exactly the same as I pass it to the program, help.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrew, 2016-09-10
@impwx

We BinaryWriterhave a method Write(float arg), and we don't need to convert anything to a string.

A
Anatoliy Mikhailov, 2016-09-13
@yamaoto

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 question

Ask a Question

731 491 924 answers to any question