How to convert HEX string to normal text string C#

c# hex string to string
c# convert hex string to byte array
c# hex to ascii
c# convert byte array into hex string
c convert hex string to char
c# hexdump string
c# hex encoded string
how to convert string to hexadecimal

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)


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));

Select(x => Convert. ToByte(InputText. Substring(x, 2), 16)) . ToArray(); return System. string normal = ConvertHexToString (hex, System.Text. Encoding .Unicode); Debug. Assert (testString. CompareTo (normal) == 0, "They are not identical" ); For example, you use Encoding.Unicode for RC4 or Encoding.ASCII for simple text . The sample code is in C# but can translated to VB.NET very easily, for example the sample usage code is:


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.

How to convert between hexadecimal strings and numeric types (C# Programming Guide). 07/20/2015; 3 minutes to read. We have a keyMaterial HEX string. It looks like this. 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)


    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();
    }

The method does two different things and thus should be split in two: Interpret a hex string as a sequence of bytes. You can find many possible implementations  Convert a hexadecimal string to a float. Convert a byte array to a hexadecimal string. Example. This example outputs the hexadecimal value of each character in a string. First it parses the string to an array of characters. Then it calls ToInt32(Char) on each character to obtain its numeric value. Finally, it formats the number as its


CompareTo(normal) == 0, "They are not identical"); For example, you use Encoding.Unicode for RC4 or Encoding.ASCII for simple text . The sample code is in C#  var hexString = BitConverter.ToString(ba); now, that's going to return a string with dashes ( -) in it so you can then simply use this: hexString = hexString.Replace("-", ""); to get rid of those if you want. NOTE: you could use a different Encoding if you needed to. Share a link to this answer. improve this answer. edited Feb 5 '14 at 3:34.


Use our Hex to String Calculator to easily convert Hex/Hexadecimal text to a string of text. Check out our free online string manipulation tools for programmers! I tried to convert an hex string into a decimal value but it doesn't gave me the expected result . I tried convert.toint32(hexa,16), convert.todecimal(hexa). I have a string look like this : 1 12 94 201 198; And I convert it into : 10C5EC9C6; And I know that the result is: 4502505926; I need your help . Thank you very much for your help :)


This example parses a string of hexadecimal values and outputs the character corresponding Duration: 12:43 Posted: Jan 28, 2019 For a hex literal that’s not prefixed you can quite easily convert it using int.Parse in C#: But as you’ve probably noticed, most hex literals are prefixed with 0x (e.g. “0x142CBD”) which would throw a FormatException if you try to parse it using the above code. In order to parse a 0x prefixed hex literal … Converting Hex string to Int in C# Read More »