As from a file, where the file is, here is such an example text:

\x68\x65\x6c\x6c\x6f 

Get in unsigned char * How would I get this code

 std::ifstream ifs(file); std::string content((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>())); ifs.close(); har* hex = (har*)content.c_str(); 

But there is a difference if I do this.

 char *hex = "\x68\x65\x6c\x6c\x6f"; cout << strlen(hex); // будет равняться 5 

But if from a file, then, as I understand it, it reads all the characters, and the question is how then to get a hex from the file? Thank you in advance!

EDIT:

 size_t ReadHexFile(FILE *inf, unsigned char *dest) { size_t count = 0; int n, u; if (dest == NULL) { unsigned int OneByte; while (true) { if ((n = fscanf(inf, "%hx", &OneByte)) == 1){ count++; }else{ break; } } } else { while ((n = fscanf(inf, "%hx", dest)) == 1) { dest++; } } if (n != EOF) { ; } return count; } unsigned char *file_get_contents(char *file){ FILE *inf = fopen(file, "rt"); int n = ReadHexFile(inf, NULL); rewind(inf); unsigned char *hex = (unsigned char *)malloc(n); ReadHexFile(inf, hex); fclose(inf); return hex; } 
  • Use the function std :: getline - Vlad from Moscow
  • Are the characters '\' , 'x' , '6' , '8' in the file so directly? Or one character with code 0x68? - VladD
  • Why, like that \ x68 - HackMemory
  • This is standard screening in hex. Here, for example, how this problem is solved in the Linux kernel: lxr.free-electrons.com/ident?i=string_unescape - 0andriy
  • I just changed the code, now I have a hex of this type: 68 65 6c 6c 6f And I check it with this code, (n = fscanf (inf, "% hx", dest) But there is a problem when 00 (0x0) occurs, it just It does not accept. How to solve the problem? The code in question! - HackMemory

1 answer 1

Your code safely reads 00, but, as I understand it, it’s just further perceived by your program as the end of the line, that's all ... And, by the way, maybe you don’t need it - but your functions don’t add the terminating character to the read line.

Also, when reading to a real string, your function returns 0, which is not good, in my opinion, add count++ to dest++ as well.

And at the end, to make sure that everything is read, make a debug output of count read characters in the same hexadecimal form.

Yes, in %hx h it turns out to be superfluous, this is for reading the unsigned short : in the first case it is not enough (you read in unsigned int ), in the second - a lot: you read in unsigned char . Think for yourself how best to read. so as not to get into trouble if the numbers are not of two characters :)

  • I checked, yes, everything counts, but for some reason, when I display the hex, After 0x00, it simply shows nothing. I did not understand how to solve the problem. And yes, for char as I understood it, % hhx is needed , right? Thanks in advance! - HackMemory
  • Output not as a string, but as a set of integers. As a string, the output is terminated as soon as a null character is encountered. - Harry
  • I have already given the vehicle as it was done in a well-known project, there is parsing, creation, and dumps - everything is there. Take it and use it! - 0andriy