18

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?

4 Answers 4

34

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

In 2020 I'd do it like this

char c = (char)0x3A;

If I needed it to be a string for use in removing a non-printable character, it would be like this

s = s.Replace($"{(char)0x3A}", ""));
2

You can use the following code:

label1.Text = System.Convert.ToChar(System.Convert.ToUInt32("0x00AC", 16)).ToString();
0

First parse it using Int32.Parse(), then use Convert.ToChar() .

Not the answer you're looking for? Browse other questions tagged or ask your own question.