Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting hex string back to char

Tags:

c#

.net

hex

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?

like image 586
Min0 Avatar asked Dec 06 '11 12:12

Min0


2 Answers

You could try:

hex = hex.Substring(2); // To remove leading 0x
int num = int.Parse(hex, NumberStyles.AllowHexSpecifier);
char cnum = (char)num;
like image 149
Marco Avatar answered Oct 27 '22 10:10

Marco


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);
    }
}
like image 22
BLUEPIXY Avatar answered Oct 27 '22 11:10

BLUEPIXY