J.Meulenbeld
J.Meulenbeld

Reputation: 355

Put a string of hexadecimal values directly into memory

I am working on a project in which I take the hexadecimal memory values of a variable/struct and print them into a file.

My goal is to get the hexadecimal memory values from that file and place it back in a pointer pointing to an "empty" variable. The part in which I get the hexadecimal memory elements works like this:

template <typename T>
void encode (T n, std::ofstream& file) {
    char *ptr = reinterpret_cast<char*>(&n);
    for (int i = 0; i < sizeof(T); i++) {
        unsigned int byte = static_cast<unsigned int>(ptr[i]);
        file << std::setw(2) << std::setfill('0') << std::hex << (byte & 0xff) << " ";
    }
}

This piece of code results in creating the following hexadecimal string:

7b 00 00 00 33 33 47 41 d9 22 00 00 01 ff 02 00 03 14 00 00 c6 1f 00 00

Now I want to place these hexadecimal values directly back into memory, but at a different location. (See it as a client recieving this string and having to decode it)

My problem now is that I don't know how to put it directly into memory. I've tried the following and unfortunately failed:

template<typename T>
void decode(T* ptr, std::ifstream& file){
    //Line of hex values
    std::string line;
    std::getline(file,line);

    //Size of string
    int n = line.length();
    
    //Converting the string into char *
    char * array = new char[n];
    strcpy(array, line.c_str());

    //copying the char * into the pointer given to the function
    memcpy(ptr,array,n);
}

This is the item which will be encoded. Its memory pattern is the same as in the outputted file: Expected

This is the result I'm getting which as you can see stores the char * into memory but not the way I want it: enter image description here

The expected result is that the decoded variable should have the same memory pattern as the encoded variable, how can I do this?

Upvotes: 0

Views: 747

Answers (1)

Sam Varshavchik
Sam Varshavchik

Reputation: 118292

std::getline(file,line);

This reads exactly what's in the file, character by character.

You indicate that your file contains this hexadecimal string:

7b 00 00 00 33 33 47 41 d9 22 00 00 01 ff 02 00 03 14 00 00 c6 1f 00 00

That is: the first character in the file is '7'. The next one is 'b', then a space character. And so on.

That's what you will get in your file, after std::getline() returns. That is, the first character of file will be 7, the next one will be b, the next one will be a space, and so on.

My problem now is that I don't know how to put it directly into memory.

No, your problem seems to be that you need to convert the read line of text back into actual, binary, raw bytes. You will need to write some code do it, first. You will need to write additional code that does the exact opposite of what you did here:

 file << std::setw(2) << std::setfill('0') << std::hex << (byte & 0xff) << " ";

The additional code, that needs to be written, does exactly the opposite of this. That is, once done, the first byte in your read buffer will be 0x7B, instead of three characters "7b", and so on.

There are many different ways to do it, ranging between using istringstream to writing a very simple hex-to-decimal conversion function. If you flip through the pages in your C++ textbook you are likely to find some sample code to do that, this is a fairly common algorithm that's offered as an example of a basic, logical test in most introductory textbooks.

And once you do that, you can copy it into your pointer. You cannot use strcpy(), for that, of course, because it copies whatever it sees up until the first 00 byte. You'll need to use std::copy, or maybe even your own, manual, copy loop.

Upvotes: 3

Related Questions