How to convert hex string to extended ascii code symbol code and write the converted codes to the text file.
Example input string:
std:string strInput = "FF2139FF"
Example output string should be "ÿ!9ÿ" in the text file.
I tried to write the program as below to write to a text file.
#include <string>
using namespace std;
string ConvertHexStringToAsciiString(string sInputHexString, int step)
{
int len = sInputHexString.length();
string sOutputAsciiString;
for (int i = 0; i < len; i += step)
{
string byte = sInputHexString.substr(i, step);
char chr = (char)(int)strtol(byte.c_str(), nullptr, 16);
sOutputAsciiString.push_back(chr);
}
return sOutputAsciiString;
}
void main()
{
string sInputHexString = "FF2139FF";
string sOutputAsciiString = "";
sOutputAsciiString = ConvertHexStringToAsciiString(sInputHexString, 2);
const char* sFileName = "E:\\MyProgramDev\\Convert_HexString_To_AsciiCode\\Convert_HexString_To_AsciiCode\\TestFolder\\1.txt";
FILE* file = fopen(sFileName, "wt");
if (nullptr != file)
{
fputs(sOutputAsciiString.c_str(), file);
fclose(file);
}
}
It seems working but when I open the text file 1.txt with notepad, I cannot see the ÿ and only !9 displayed. I am not sure how to display it correctly using notepad or my code is wrong?
Thanks.