Converting hex string back to char

16,833

Solution 1

You could try:

hex = hex.Substring(2); // To remove leading 0x
int num = int.Parse(hex, NumberStyles.AllowHexSpecifier);
char cnum = (char)num;

Solution 2

using System;
using System.Globalization;

class Sample {
    static void Main(){
        char c = 'あ';
        int unicode = c;
        string hex = string.Format("0x{0:x4}", unicode);
        Console.WriteLine(hex);
        unicode = int.Parse(hex.Substring(2), NumberStyles.HexNumber);
        c = (char)unicode;
        Console.WriteLine(c);
    }
}
Share:
16,833
Min0
Author by

Min0

Updated on July 28, 2022

Comments

  • Min0
    Min0 over 1 year

    I know- there are lots of topics concerning this, BUT even though I did look through a bunch of them couldn't figure the solution.. I'm converting char to hex like this:

    char c = i;
    int unicode = c;
    string hex = string.Format("0x{0:x4}", unicode);
    

    Question: how to convert hex to char back?

  • Marco
    Marco over 12 years
    Thanks @Oded, I didn't see leading "0x", my mistake.