0

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.

David Alex
  • 423
  • 1
  • 4
  • 11
  • You're using C++, you may use its standard library instead of the C one while you're at it. – Federico klez Culloca Oct 21 '19 at 10:52
  • Also, you may want to check [this answer](https://stackoverflow.com/a/2054941/133203) and see whether your `char`s are `signed` or `unsigned`. If they are signed, `FF` won't be what you expect. – Federico klez Culloca Oct 21 '19 at 10:57
  • Finally `char chr = (char)(int)strtol(byte.c_str(), nullptr, 16);` that double cast is overkill, just cast to `char`. – Federico klez Culloca Oct 21 '19 at 10:58
  • I go to debug and watch this sOutputAsciiString the string is shown correct ÿ!9ÿ, only when I write to text ÿ is not shown. Also I tried to directly cast to char, it has the same result as before. I dont know whether my codes got problems or not? – David Alex Oct 21 '19 at 11:05
  • It's `int main()` https://en.cppreference.com/w/cpp/language/main_function – Thomas Sablik Oct 21 '19 at 11:14
  • 1
    I can't reproduce the problem. I compiled and ran the program. The content of the file is `�!9�`. `hexdump -C 1.txt` gives me `ff 21 39 ff`. Probably notepad can't show you the symbols. – Thomas Sablik Oct 21 '19 at 11:18
  • Hi, @ThomasSablik, when I open the text file, it show !9 where two digits character is not shown. �!9� also not what I expected. I want to show ÿ!9ÿ in the notepad. Is my notepad settings not correct or program need to do some conversions to the output string? – David Alex Oct 21 '19 at 11:22
  • 2
    Note that ASCII is a 7-bit encoding, and that only the first 127 characters are defined. Extended ASCII is a common name for a bunch of different character encodings that build on top of ASCII, but they do not agree at all about what the upper half of those characters are. So while 0xFF may translate to ÿ in the encoding used by your debugger, it is not guaranteed that it will translate to the same in your console window (or notepad). – Frodyne Oct 21 '19 at 11:22
  • 0xFF = 255. [Here](https://www.cs.cmu.edu/~pattis/15-1XX/common/handouts/ascii.html) is an ASCII table. There is neither 255 nor ÿ – Thomas Sablik Oct 21 '19 at 11:25
  • @Frodyne, I think you are correct. I need to handle the extended ascii table for my code purpose and so I used this https://www.ascii-code.com/ the full ascii table. – David Alex Oct 21 '19 at 11:28
  • You can use the extended ASCII encoding in your application but most applications don't use it. You can't see it in notepad. vim shows it. Visual Studio Code doesn't show it. – Thomas Sablik Oct 21 '19 at 11:29
  • Right, but that is only one possible extended ASCII table. I was able to find a site where you can select some of the other ones that exist: https://www.rapidtables.com/code/text/ascii-table.html In any case, your problem is that the character a specific value gets interpreted as depends on the encoding that the _reader_ is using. If you want a specific result, then you have to ensure that your reader is using the correct encoding. – Frodyne Oct 21 '19 at 11:40
  • Try to read this for a a somewhat more in-depth explanation of character encodings: https://www.joelonsoftware.com/2003/10/08/the-absolute-minimum-every-software-developer-absolutely-positively-must-know-about-unicode-and-character-sets-no-excuses/ – Frodyne Oct 21 '19 at 11:45
  • Do you have any proper solutions for this as I need to write the ÿ!9ÿ directly to notepad, it seems that there are other ways. – David Alex Oct 21 '19 at 11:45

2 Answers2

0

Use better notepad - or even better, any hexeditor to view result.

Try for example XVI 32 hex editor

PiotrK
  • 4,210
  • 6
  • 45
  • 65
0

I found a way to do thing, Split this HexString FF to two BYTE(unsigned char) "F" and "F", and then construct together and convert to decimal. It can show the correct letter.

David Alex
  • 423
  • 1
  • 4
  • 11