1

I want to read binary with Python and then store the binary in a header file for a C++ program. How can I store this binary data for later use like writing it to an .exe file to execute it? What encoding should I use?

Also is for example \x00 ASCII or HEX? Can I encode the binary in ASCII then store it in a header file for later use in C++ and then convert it to binary on the go to write the variable content to a file in binary format? How can I achieve this.

A little more clarification. C++ Header file will have a variable storing binary data, but I don't know how to achieve that, since I don't think it's as simple as pouring the binary data to a char pointer like this

char *binary = "?>!#{PL{"; // Some unreadable by text editors binary data go here

I think it should be more like

char *binary = "\x00\x01\x04"; // When I encoded it in ASCII it was in this format, but I remember that HEX format was similar too maybe even the same?

Or even storing it in an array in this format

char binaryp[] = { 0x546, 0x423 etc...};

But for that I need a Separator in my Python code.

Python code:

with open("../testing.exe", "rb") as f:

with open("asd.txt", "wb") as b:
    while True:
        c = f.read(1)
        if not c:
            break
        b.write("{}".format(c).encode("ascii")) # Or HEX

In general I just want to store binary in a C++ variable and it doesn't matter if it's readable or not I just want to be able to write it to a binary file in the end, without doing any kind of reading on C++ side. Think it as if you already have some binary data on a variable in C++ and you just write it down to an exe file and it works. The issue is I don't know what's the easiest way to do it and the best way. The Performance isn't as important, but if you have a better solution I'm always happy to listen!

3
  • Those are HEX values, do I have to convert them to binary before writing to an .exe file or can I directly write them? I assume not. Also why bother with separating them in an array, can't I just write the HEX values onto a single string like char hexvalue[] = "442342ado1FKO11a" without 0x part, since that part is only to notify the computer that that value is a HEX value? Commented Feb 10, 2020 at 21:06
  • For example I don't really care what encoding I'm using as long as in the end I'm able to convert everything onto raw binary data and when I write that RAW BINARY data onto a file it should work as if I just copied it. Commented Feb 10, 2020 at 21:08
  • FYI, if you store values in a C header file, you'll need to recompile or write code that can parse a C header file. Much simpler to put the data into file using a data language like JSON, HTML, XML, or INI. Commented Feb 10, 2020 at 21:46

2 Answers 2

0

The Answer is python bytes to C array (like xxd program"

from binascii import hexlify

with open("../testing.exe", "rb") as f:

with open("header.hpp", "w") as b:
    arr = []
    while True:
        c = f.read(1)
        if not c:
            b.write("unsigned char buff[] = {{{}}};".format(b", ".join(arr)))
            break
        arr.append(b"0x" + hexlify(c))

This will produce the wished header file and after that you can just write the buffer onto a file with binary mode on aka "wb" on C/C++. Note that this code will wrap the array in b' ' don't forget to remove that before any use. This code will produce an array of HEX code, each equaling to a byte of the binary desired.

Sign up to request clarification or add additional context in comments.

Comments

-1

Encode the binary using Base 64 or some other text compatible encoding

base64 decode snippet in c++

2 Comments

Might you explain this solution a little furthermore? For example what's the difference of encoding it in Base 64 and ASCII or HEX? Which one is easier to implement?
It’s the standard way to do this that would work in any language. There are typically libraries to decode/encode, and even implementing it yourself is trivial. Another choose is UUEncoding. Your use case is exactly why they were designed and what they are used for : embedding binary formats in text.