How to convert a hexadecimal string to a decimal value

I tried converting a hexadecimal string to a decimal value, but this did not give the expected result

I tried convert.toint32(hexa,16), convert.todecimal(hexa).

My line looks like this:

  • 1 12 94 201 198

And I convert it to:

  • 10C5EC9C6

And I know the result:

  • 4502505926

I need your help

Many thanks for your help:)

+4
source share
1 answer

System.Decimal (# decimal) NumberStyles.HexNumber. System.Int32 (# int) . System.Int64 (# long):

string s = "10C5EC9C6";
long n = Int64.Parse(s, System.Globalization.NumberStyles.HexNumber);
'n ==> 4502505926

, decimal :

decimal d = (decimal)Int64.Parse(s, System.Globalization.NumberStyles.HexNumber);

.

string s = "1 12 94 201 198";
string[] groups = s.Split();
long result = 0;
foreach (string hexGroup in groups) {
    result = 256 * result + Int32.Parse(hexGroup);
}
Console.WriteLine(result); // ==> 4502505926

, 16 * 16 = 256.

+6

All Articles