<分区>
我在 stackexchange 的其他地方找到了这段代码,它工作得很好。
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;
但是,我想从文本文件中提取十六进制数据(存储方式如下:4d5a9000f4 等...)。
使用 fstream,我可以将文本文件的内容转换为字符串,但如何将其转换为代码示例中显示的数组? (所以像这样的字符数组:\x4d\x5a\x90\x00\xf4 等...)
转换示例:
来自:
string str_hex = "4d5a9000f4";
到:
char *ch_hex = new char[str_hex.size()+1] = "\x4d\x5a\x90\x00\xf4"
感谢您的帮助!