'How to convert HEX string to normal text string C#

We have a keyMaterial HEX string. It looks like this

453F1287225ED9971D389A35F8D1032E7748DD0B88302F7C6C194626D4C8659B000000000E800000000200002000000047C2CA7B9A1F1C343CA228CC314A42F063A240E17624F886AF6CE9A135CAF65310000000D30489E536548F129E43240A26344811400000009EE2F5549B1447548ADADDD60A212C22DC2F9B2DC67D8E567B48B3847A525244A2F575AAFFB3AECD0385612BE7C38CA403BE6B5DFA8BEDEFBFA35C5ECC1818AB

You can see the whole file. How can we translate this HEX code to normal text string with C#? I have some examples on C++, it hasn't helped me. Can you give an advise?

THE ENCODING OF THIS STRING IS 16291388 (but it's not a solution of the problem)



Solution 1:[1]

If you take the conversion method from here and extend that with conversion of the resulting byte array using the right encoding, you are there.

public static byte[] StringToByteArray(String hex)
{
    int NumberChars = hex.Length;
    byte[] bytes = new byte[NumberChars / 2];
    for (int i = 0; i < NumberChars; i += 2)
        bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
    return bytes;
}

// use UTF8 in this sample:
string output = Encoding.UTF8.GetString(StringToByteArray(hex));

Solution 2:[2]

If you look at the OP's original file he is trying to decode the keyMaterial of a wireless profile.

This data is not just encoded in hex, it is also encrypted which is why just converting it to a string results in nonsensical data.

<WLANProfile xmlns="http://www.microsoft.com/networking/WLAN/profile/v1">
    <name>TP-LINK_D84690</name>
    <SSIDConfig>
        <SSID>
            <hex>54502D4C494E4B5F443834363930</hex>
            <name>TP-LINK_D84690</name>
        </SSID>
    </SSIDConfig>
    <connectionType>ESS</connectionType>
    <connectionMode>auto</connectionMode>
    <MSM>
        <security>
            <authEncryption>
                <authentication>WPA2PSK</authentication>
                <encryption>AES</encryption>
                <useOneX>false</useOneX>
            </authEncryption>
            <sharedKey>
                <keyType>passPhrase</keyType>
                <protected>true</protected>
                <keyMaterial>01000000D08C9DDF0115D1118C7A00C04FC297EB01000000E72F4D29E2AD8E40BBAD5B02842DA10000000000020000000000106600000001000020000000453F1287225ED9971D389A35F8D1032E7748DD0B88302F7C6C194626D4C8659B000000000E800000000200002000000047C2CA7B9A1F1C343CA228CC314A42F063A240E17624F886AF6CE9A135CAF65310000000D30489E536548F129E43240A26344811400000009EE2F5549B1447548ADADDD60A212C22DC2F9B2DC67D8E567B48B3847A525244A2F575AAFFB3AECD0385612BE7C38CA403BE6B5DFA8BEDEFBFA35C5ECC1818AB</keyMaterial>
            </sharedKey>
        </security>
    </MSM>
</WLANProfile>

This data is encrypted so you cannot just decode it into a string.

Solution 3:[3]

    byte[] bytes = StringToByteArray(your hex string here);
    var strArray = (Encoding.Default.GetString(
             bytes,
             0,
             bytes.Length - 1)).Split(new string[] { "\r\n", "\r", "\n", "\0" },
                                         StringSplitOptions.None);
    string output = string.Join("", strArray);

The StringToByteArray Code is here

    public static byte[] StringToByteArray(string hex)
    {
        return Enumerable.Range(0, hex.Length)
            .Where(x => x % 2 == 0)
            .Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
            .ToArray();
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community
Solution 2
Solution 3 AbdusSalam