Convert Base16 string (Hexadecimal) to Base10 (Decimal)

Asked

Viewed 1,287 times

7

I have the following string "615769EF", hexadecimal.

How to convert it to base10 and let the result be the string "01633118703" ?

2 answers

6


You can do it like this:

string hex = "615769EF";

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

To decimal it would be like this:

string hex = "615769EF";

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

But the following error occurs:

No support for Allowhexspecifier number style in floating point data types.

But converting to int it will return the result 1633118703 without the 0 in front. There if you want in format string just make a ToString;

Example:

string hex = "615769EF";

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

5

I found a reply interesting that led me to the use of 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’s an example I created in https://dotnetfiddle.net/jXJvLZ

Browser other questions tagged

You are not signed in. Login or sign up in order to post.