How to convert numbers between hexadecimal and decimal


Question

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

1
139
12/10/2018 9:52:57 AM

Accepted Answer

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);
265
4/15/2018 10:04:09 PM

Hex -> decimal:

Convert.ToInt64(hexValue, 16);

Decimal -> Hex

string.format("{0:x}", decValue);

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon