-9

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

KHS
  • 53
  • 1
  • 2
  • 7
  • [Convert.ToString(Int32, Int32)](https://msdn.microsoft.com/en-us/library/14kwkz77%28v=vs.110%29.aspx) and/or [Convert.ToInt32(string, Int32)](https://msdn.microsoft.com/en-us/library/1k20k614%28v=vs.110%29.aspx) – Alexander Feb 04 '15 at 12:56
  • int decValue = int.Parse(hexValue, System.Globalization.NumberStyles.HexNumber); from http://stackoverflow.com/questions/74148/how-to-convert-numbers-between-hexadecimal-and-decimal-in-c – Christofer Ohlsson Feb 04 '15 at 12:57
  • @Alexander - I already used that, it gives me an error. The program can only convert **8000** not the **8000 8000 1000 0100** – KHS Feb 04 '15 at 13:00

2 Answers2

2

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.

H W
  • 2,556
  • 3
  • 21
  • 45
0
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

user3021830
  • 2,784
  • 2
  • 22
  • 43
  • I inputted **8000 8000 0100** the result was this **{0} = {1} 0100=256** the program only display the 3rd group of number. – KHS Feb 04 '15 at 13:43
  • Sorry, my bad. I fixed typos and quotations. Now it should wok. – user3021830 Feb 04 '15 at 13:51
  • What's the equivalent of Console.ReadKey() in Winforms? – KHS Feb 04 '15 at 13:57
  • There is no equivalent. This is a Console Application spesific command which pauses and waits the user to press any key. It is just used to keep the application windows open until you press a key. In WinForms you do not need such an action. Form is displayed until you hide or slose it. – user3021830 Feb 04 '15 at 14:05
  • **An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll** in this line **TxttText.Text = string.Format("{0} = {1}" + "=" + hex + "=" + Convert.ToDecimal(value));** – KHS Feb 04 '15 at 14:18
  • Why did you added additional "=" strings? They distort the string.Format() method paremeters. It is a method that gets format string and parameters only. It sewlf constructs the string. You should not do it. If you want to format string then do not use Format() method. See https://msdn.microsoft.com/en-us/library/b1csw23d(v=vs.110).aspx – user3021830 Feb 04 '15 at 14:30
  • I already erase the string.Format() but still having a problem of the output. It only display the answer of 3rd group of number. – KHS Feb 04 '15 at 14:35
  • I don't know what but you are doing something wrong. Please check the image I have added to the answer. – user3021830 Feb 04 '15 at 14:44
  • I already got it. I just put '+' before the '=' sign. Thanks for helping me. 'Til next time. – KHS Feb 05 '15 at 06:32