简体   繁体   中英

Proper way to convert HEX to ASCII read from a file C++

In my code bellow CODE 1 reading HEX from a file and storing in in string array won't convert it to ASCII when printed out.

#include <iostream>
#include <sstream>
#include <fstream>

int main(int argc, char** argv)
{
    // CODE 1
    std::ifstream input("C:\\test.txt"); // The test.txt contains \x48\x83\xEC\x28\x48\x83
    std::stringstream sstr;
    input >> sstr.rdbuf();

    std::string test = sstr.str();
    std::cout << "\nString from file: " << test;
    
    //char* lol = new char[test.size()];
    //memcpy(lol, test.data(), test.size());
    
    ////////////////////////////////////////////////////////
    
    // CODE 2
    std::string test_2 = "\x48\x83\xEC\x28\x48\x83";
    std::cout << "\n\nHardcoded string: " << test_2 << "\n";
    // Prints as ASCII "H(H" , which I want my CODE 1 to do.

}

In my CODE 2 sample, same HEX is used and it prints it as ASCII . Why is it not the same for CODE 1 ? 在此处输入图像描述

Okay, it looks like there is some confusion. First, I have to ask if you're SURE you know what is in your file.

That is, does it contain, oh, it looks like about 20 characters:

\
x
4
8

et cetera?

Or does it contain a hex 48 (one byte), a hex 83 (one byte), for a total of 5-ish characters?

I bet it's the first. I bet your file is about 20 characters long and literally contains the string that's getting printed.

And if so, then the code is doing what you expect. It's reading a line of text and writing it back out. If you want it to actually interpret it like the compiler does, then you're going to have to do the steps yourself.

Now, if it actually contains the hex characters (but I bet it doesn't), then that's a little different problem, and we'll have to look at that. But I think you just have a string of characters that includes \x in it. And reading / writing that isn't going to automatically do some magic for you.

When you read from file, the backslash characters are not escaped. Your test string from file is literally an array of chars: {'\\', 'x', '4', '8', ... }

Whereas your hardcoded literal string, "\x48\x83\xEC\x28\x48\x83"; is fully hex escaped by the compiler.

If you really want to store your data as a text file as a series of "backslash x NN" sequences, you'll need to convert after you read from file. Here's a hacked up loop that would do it for you.

std::string test = sstr.str();

char temp[3] = {};
size_t t = 0;
std::string corrected;

for (char c : test)
{
   if (isxdigit(c))
   {
       temp[t] = c;
       t++;
       if (t == 2)
       {
          t = 0;
          unsigned char uc = (unsigned char)strtoul(tmp, nullptr, 16);
          corrected += (char)uc;
       }
   }
}

You can split the returned string in \x then make casting from string to int, finally casting to char.

this resource will be helpful

strtok And convert

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM