C++ convert function to unsigned char array

How to convert function to unsigned char array (*void pointer)
I'm sorry if I do not understand correctly what is *void pointer
For example i have functions like this

Example 1
1
2
3
void test(){
  std::cout << "Hello" << std::endl;
}


Example 2
1
2
3
4
void test2(int a){
  std::cout << "Hello" << std::endl;
  std::cout << std::to_string(a) << std::endl;
}


Example 3
1
2
3
int test3(int a){
  return a;
}


In short i need univesal method to convert any method(function) to raw data.
I need to save this functions as array or vector of unsigned char.

Someone can help me?
Thank you so much.
Do you mean while it is still text, or do you mean the function after it has been compiled and turned into binary executable?

I suspect this is related to your other thread. Again, a large part of the issue here is that we don't know what you're trying to do. What you just asked makes no sense.

Raw text in an array with a pointer to it is useless to you.
Raw binary in an array with a pointer to it is almost certainly also useless to you.

What are you trying to do? Tell us what you're trying to do.
Last edited on
1. I need to convert function to array of unsigned char.
2. encrypt this array
3. Save Encrypted array in header file
4. Include header file in my code
5. decrypt the array from header file
6. Runtime use this array as function

Thank you @Repeater so much, and sorry for bad explaination.
Last edited on
Oh right, shellcode again.

You need to take the function, translate it into assembly code (or make the compiler do it and then look at that), turn that assembly code into hex, type that hex into your source code.

To do this, you will need to understand assembly code, and understand how your particular OS calls functions.
Oh thank you so much, first smart answer for me.
Can i ask other questions in progress?
Well, a pointer to a function is not different from any other pointer. So in theory the size of a function can be calculated as:

1
2
3
4
5
6
7
8
9
10
11
12
void test2(int a){
  std::cout << "Hello" << std::endl;
  std::cout << std::to_string(a) << std::endl;
}

int test3(int a){
  return a;
}

..
ptrdiff_t size_of_test2 = test3 - test2;
...
Or copying it to a vector:
1
2
3
4
5
6
7
8
9
10
11
12
13
void test2(int a){
  std::cout << "Hello" << std::endl;
  std::cout << std::to_string(a) << std::endl;
}

int test3(int a){
  return a;
}

..
std::vector<unsigned char> v;
copy(reinterpret_cast<const unsigned char *>(test2), reinterpret_cast<const unsigned char *>(test3), std::back_inserter(v));
...
Topic archived. No new replies allowed.