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

巧了我就是萌 提交于 2019-12-13 11:30:39

问题


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



来源:https://stackoverflow.com/questions/28321924/how-to-convert-hex-to-decimal-in-c-net

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!