0

I have some problems understanding how the binary files cand be rewritten.

#include <iostream>                                    
#include <cmath>  
using namespace std;
constexpr auto PI = 3.14 ;

int main()
{
    double x, y;
 {
    cout << "[>>] Type X: "; cin >> x;
    cout << "[>>] Type Y: "; cin >> y;
}
double sum = sin(x) + sin(y);
cout << "[i] Radians: sin(" << x << ") + sin(" << y << ") = " << sum << endl;
cout << "[i] Degrees: sin(" << x << ") + sin(" << y << ") = " << sin(x * PI / 180) + sin(y * PI / 180) << endl;
}

I need a code that edites double sum = sin(x) + sin(y) to double sum = 2sin((x+y)/2)cos((x-y)/2) from binary file after the code outputs the result of the sinus summary if randomed boolean is true and does double sum = 2sin((x+y)/2)cos((x-y)/2) to double sum = sin(x) + sin (y) if randomed boolean is false. Is there any solution?

  • 3
    I honestly don't understand what you want. Can you try to describe that using simple, short sentences? I don't understand in particular which binary file you are referring to and what you want to do with it. – Ulrich Eckhardt Dec 24 '19 at 14:10
  • @UlrichEckhardt I need to write something like a self-modifying code which changes the double parameter to another by the luck of boolean randomizer. The changes must be done by rewriting data in a binary file of this code. Sorry if you don't understand, I'm new to c++. – Sergei Khokhryakov Dec 24 '19 at 14:18
  • You want to edit an already generated executable file? Why? I think some context is needed here. – ChrisMM Dec 24 '19 at 14:24
  • @ChrisMM For research, I'm trying to understand how the code can be rewritten (in case of the double parameter) without changing it manually – Sergei Khokhryakov Dec 24 '19 at 14:30
  • So you want the compiled executable to use a specific calculation based on a boolean value? In that case I recommend using defines. You can find more information for using preprocessor directives here: https://learn.microsoft.com/en-us/cpp/preprocessor/preprocessor-directives?view=vs-2019. If you would like to modify the executable file after it's been compiled then please make that clear in your answer. – avgcoder Dec 24 '19 at 14:48
  • Why don't you use ifs? Like: `if (rand()%2)` then sum = your first function. else, sum = your second function. – fern17 Dec 24 '19 at 14:51
  • @fern17 I know its the simple solution. But how to do this by changing the binary? – Sergei Khokhryakov Dec 24 '19 at 14:58
  • If you are new to C++, this question shouldn't be of interest to you. what you are trying to do is extremely difficult. Also: a self modifying executable will be blacklisted by any antivirus.... – fern17 Dec 24 '19 at 15:01
  • @fern17 it's just for research. I know this is difficult so I asked here for a solution. :) – Sergei Khokhryakov Dec 24 '19 at 15:06
  • A solution (if one exists) would be strictly dependatd on the compiler, compiler options (like optimizatinos) and architecture, for which the program is built. You can try to first analyze assembly generated by your compiler. Once you understand the assembly, you can try to map it specific part of the binary file, but that's a terrible task to do. – Yksisarvinen Dec 24 '19 at 16:19
  • If you want to modify binary code you had to learn assembly language. C++ doesn't help much here. To cite from this recent [answer](https://stackoverflow.com/a/59456112/7478597): _Contrary to popular belief, C++ source code is not a sequence of instructions provided to the executing computer. It is not a list of things that the executable will contain. It is merely a description of a program._ – Scheff's Cat Dec 24 '19 at 16:19

0 Answers0