OMGKurtNilsen OMGKurtNilsen - 1 month ago 13
C# Question

C# Hexadecimal to char

I have a hexidecimal string with a length of 4, such as "003a".

What is the best way to convert this into a char? First convert to bytes and then to char?

Answer

Try this:

(char)Int16.Parse("003a", NumberStyles.AllowHexSpecifier);

or

System.Convert.ToChar(System.Convert.ToUInt32("003a", 16));

or

string str = "";
for(int i = 0; i<myHex.Length; i += 4)
    str += (char)Int16.Parse(myHex.Substring(i, 4), 
                             NumberStyles.AllowHexSpecifier);