0

I found this code somewhere else on stackexchange and it works great.

  char *szData = new char[str_hex.size()+1] = "\x4d\x4d\x4d\x5a\x90\x00\x03\x00\x00\x00\x04\x00\x00\x00\xe0\x00\x00\x00\x0e\x1f\xba\x0e\x00\xb4\x09\xcd\x00\x00\x00\x00\x00\x00\x00\x00";

        system("pause");
        HANDLE hFile; 
        DWORD dwBytesWritten = 0;
        TCHAR szPath[MAX_PATH] = "C:\\Users\\Jules\\Desktop\\halloskos.txt";

        hFile = CreateFile (szPath,               
                           GENERIC_WRITE,         
                           0,                      
                           NULL,                 
                           CREATE_NEW,            
                           FILE_ATTRIBUTE_NORMAL,  
                           NULL);              

        WriteFile( hFile,&hexCh,sizeof(hexCh)-1, &dwBytesWritten, NULL );
        CloseHandle(hFile);
        delete [] szData;

However, i want to pull the hex data from a text file (stored like this: 4d5a9000f4 etc...).
With fstream i can turn the contents of the text file to a string, but how can i convert this to the array shown in the code example? (So an char array like this: \x4d\x5a\x90\x00\xf4 etc...)
Conversion example:
from:

string str_hex = "4d5a9000f4";

to:

char *ch_hex = new char[str_hex.size()+1] = "\x4d\x5a\x90\x00\xf4"

Thanks for your help!

Jules
  • 1
  • 1

1 Answers1

1

When you write

"\x4d\x4d\x4d\x5a\x90\x00\x03\x00\x00\x00\x04\x00\x00\x00\xe0\x00\x00\x00\x0e\x1f\xba\x0e\x00\xb4\x09\xcd\x00\x00\x00\x00\x00\x00\x00\x00"

The compiler does conversion from hex to bin (char by char) at compile time.

What you need is to convert hex string to bin at run-time. There are a lot of answers like C++ convert hex string to signed integer

Here you will convert not to int but to char

Community
  • 1
  • 1
Dmitriy Zapevalov
  • 1,357
  • 8
  • 13