Convert string Base16 (Hexadecimal) to Base10 (Decimal)

7

I have the following string "615769EF" , which is in hex.

How to convert it to base10 and that the result is the string "01633118703" ?

    
asked by anonymous 16.06.2015 / 15:07

2 answers

6

You can do this:

string hex = "615769EF";

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

For decimal it would look like this:

string hex = "615769EF";

decimal decValue = decimal.Parse(hex, System.Globalization.NumberStyles.HexNumber);

But the following error occurs:

  

The AllowHexSpecifier number style is not supported on floating-point data types.

But converting to int will return the result 1633118703 without 0 up front. Then, if you want to string only make a ToString ;

Example:

string hex = "615769EF";

string decValue = int.Parse(hex, System.Globalization.NumberStyles.HexNumber).ToString();
    
16.06.2015 / 15:25
5

I found an interesting response that led me to using Convert.ToUInt64(valor,fromBase) .

This second parameter fromBase allows conversion to base 2, 8, 10, and 16.

//Hexadecimal para Decimal
string cpfHexadecimal = "615769EF";

string cpfDecimal = Convert.ToUInt64(cpfHexadecimal,16).ToString("00000000000");

Convert.ToString also has this second parameter.

Example:

//Decimal para Hexadecimal
cpfDecimal = "01633118703";
cpfHexadecimal = Convert.ToString(long.Parse(cpfDecimal),16);

Here is an example I created in link

    
16.06.2015 / 16:03