How to convert numbers between hexadecimal and decimal

Andy McCluggage picture Andy McCluggage · Sep 16, 2008 · Viewed 358.7k times · Source

How do you convert between hexadecimal numbers and decimal numbers in C#?

Answer

Andy McCluggage picture Andy McCluggage · Sep 16, 2008

To convert from decimal to hex do...

string hexValue = decValue.ToString("X");

To convert from hex to decimal do either...

int decValue = int.Parse(hexValue, System.Globalization.NumberStyles.HexNumber);

or

int decValue = Convert.ToInt32(hexValue, 16);