Method 1:
Copy Code code as follows:
int d=10;
D.tostring ("x")//or change x to X, it becomes a 16-bit string.
int X=convert.toint32 (d.tostring ("X"), 16);//Turn 16 string back to 10.
Method 2:
Copy Code code as follows:
static void Main ()
{
int i = 446;
String hex = i.ToString ("x"/* or X *);
Console.WriteLine (hex);
Int J = HexToInt (hex);
Console.WriteLine (j);
}
static int HexToInt (string hex)
{
Hex = Regex.Replace (Hex, "^0x", "", regexoptions.ignorecase);
if (Regex.IsMatch (hex, "[G-z]", regexoptions.ignorecase))
{
throw new Exception ("Invalid hexadecimal Expression.: 0x" + hex);
}
char[] chars = hex. ToUpper (). ToCharArray ();
Array.reverse (chars);
int dec = 0;
for (int i = 0; i < chars. Length; i++)
{
Dec + + hexmapping (Chars[i]) * (int) Math.pow (i);
}
return Dec;
}
static int hexmapping (char c)
{
Switch (c)
{
Case ' 0 ':
return 0;
Case ' 1 ':
return 1;
Case ' 2 ':
return 2;
Case ' 3 ':
return 3;
Case ' 4 ':
return 4;
Case ' 5 ':
return 5;
Case ' 6 ':
return 6;
Case ' 7 ':
return 7;
Case ' 8 ':
return 8;
Case ' 9 ':
return 9;
Case ' A ':
return 10;
Case ' B ':
return 11;
Case ' C ':
return 12;
Case ' D ':
return 13;
Case ' E ':
return 14;
Case ' F ':
return 15;
Default:
throw new Exception ("Invalid hexadecimal Character:" + c.tostring ());
}
}