-9

With respect to this tool, I need to convert hexadecimal data, irrespective of their combination to equivalent text. For example:

"HelloWorld" = 48656c6c6f576f726c64;

The solution needs to take into account that hexadecimal can be grouped in different lengths:

48656c6c 6f576f72 6c64
or
48 65 6c 6c 6f 57 6f 72 6c 64

All of the hexadecimal values supplied above read as HelloWorld when converted to text.

Hazel へいぜる
  • 2,751
  • 1
  • 12
  • 44

1 Answers1

1

First, I would like to point out that this question has been asked many times on the web (here is one example). However, I am going to break this down step by step for you to hopefully teach you how to not only utilize your resources available on the web, but also how to solve your problem.

Overview: Converting from hexadecimal data to text that is able to be read by human beings is a straight-forward process in modern development languages; you clean the data (ensuring no illegal characters remain), then you convert down to the byte level so that you can work with the raw data. Finally, you'll convert that raw data into readable text utilizing a method that has already been created by Microsoft.

Important: Remember, for the conversion to work, you have to ensure you're converting in the same format that you started with:

ASCII -> ASCII: Works Great!
ASCII -> UTF7: Not so much...

Removing Illegal Characters: One of the first things you'll need to do is ensure the hexadecimal value that you're supplying doesn't contain any illegal characters. The simplest way to do this is to create an array of acceptable characters and then remove anything but these in a loop:

private string GetCleanHex(string hex) {
    string legalCharacters = "0123456789ABCDEF";
    string result = hex.ToUpper();
    foreach (char c in result) {
        if (!legalCharacters.Contains(c))
            result = result.Replace(c.ToString(), string.Empty);
    }
}

Getting The Byte Array: Once you've cleaned out all illegal characters, you can now convert your hexadecimal string into a byte array. This is required to convert from hexadecimal to ASCII. This step was provided by the linked post above:

private byte[] GetBytesFromHex(string hex) {
    byte[] bytes = new byte[result.Length / 2];
    for (int i = 0; i < bytes.Length; i++)
        bytes[i] = Convert.ToByte(result.Substring(i * 2, 2), 16);
}

Converting To Text: Now that you've cleaned your data, and converted it to a byte[], you can now convert that byte data into ASCII. This can be done using a method available in Encoding.ASCII called GetString:

string text = Encoding.ASCII.GetString(bytes);

The Final Result: Plug all of this into your application and you'll have successfully converted hexadecimal data into clean, readable text:

string hex = GetCleanHex("506c 65 61736520 72 656164 20686f77 2074 6f 2061 73 6b 2e");
byte[] bytes = GetBytesFromHex(hex);
string text = Encoding.ASCII.GetString(bytes);
Console.WriteLine(text);
Console.ReadKey();

The code above will print the following text to the console:

Please read how to ask.

Hazel へいぜる
  • 2,751
  • 1
  • 12
  • 44