How to convert hex to decimal in c#.net? [duplicat

2019-09-22 12:27发布

问题:

This question already has an answer here:

  • How to convert numbers between hexadecimal and decimal 17 answers

Convert Hex to Decimal

Example:

It would ask a Hex. Shown below.

Enter Hex: 8000 8000 1000 0100

Then,

The Result: 32768 32768 4096 256

Convert each hex to decimal.

HEX = DECIMAL

8000 = 32768

8000 = 32768

1000 = 4096

0100 = 256

回答1:

use string.split(' ') to get your individual hex-numbers as a string-array. Then you can call

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

to convert each hex into its decimal representation.



回答2:

Console.Write("Enter HEX: ");
string hexValues = Console.ReadLine();
string[] hexValuesSplit = hexValues.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
Console.WriteLine("HEX = DECIMAL");
foreach (String hex in hexValuesSplit)
{
    // Convert the number expressed in base-16 to an integer. 
    int value = Convert.ToInt32(hex, 16);
    Console.WriteLine(string.Format("{0} = {1}", hex, Convert.ToDecimal(value)));
}

Console.ReadKey();

P.S. : The original code does not belong to me. For original codes please refer to MSDN



标签: c# hex decimal