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

2019-09-22 12:11发布

This question already has an answer here:

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

标签: c# hex decimal
2条回答
\"骚年 ilove
2楼-- · 2019-09-22 12:50
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

enter image description here

查看更多
劳资没心,怎么记你
3楼-- · 2019-09-22 13:11

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.

查看更多
登录 后发表回答