So we were asked to convert a Hexadecimal Value (stored in a String) to its Decimal Value. Each character in the String should be manually converted to decimal within a loop, and then post the total Decimal Value of the Hexadecimal Value. I got here the codes that I have written but I couldn't identify where I could have gone wrong for an "F4" Hexa (as an example) to have a Decimal Equivalent of "292" instead of "244". I have debugged everything, the code seems fine. Any ideas?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AdvProgCS
{
class Program
{
static int dec=0;
static string hex;
static void Main(string[] args)
{
do{
Console.Write("[Press 0 to Stop] Hexadecimal Value: ");
hex = Console.ReadLine();
if (hex == "0") break;
int length = hex.Length;
for (int i = 0; i < length+1; i++)
{
if (hex[i] == 'A' || hex[i] == 'B' || hex[i] == 'C' || hex[i] == 'D' || hex[i] == 'E' || hex[i] == 'F')
{
if (hex[i] == 'A')
dec+= 10 * Convert.ToInt32(Math.Pow(16, length - 1));
if (hex[i] == 'B')
dec += 11 * Convert.ToInt32(Math.Pow(16, length - 1));
if (hex[i] == 'C')
dec += 12 * Convert.ToInt32(Math.Pow(16, length - 1));
if (hex[i] == 'D')
dec += 13 * Convert.ToInt32(Math.Pow(16, length - 1));
if (hex[i] == 'E')
dec += 14 * Convert.ToInt32(Math.Pow(16, length - 1));
if (hex[i] == 'F')
dec += 15 * (Convert.ToInt32(Math.Pow(16, length - 1)));
}
else
dec += hex[i];
length--;
}
Console.WriteLine("DECIMAL EQUIVALENT: " + dec + "\n");
}
while(hex != "0");
}
}
}
You forgot about the Math.Pow
in the dec += hex[i]
line where you also have to convert hex[i]
from char into a number.
dec += (int)char.GetNumericValue(hex[i]) * (int)Math.Pow(16, length - 1);
Moreover, as observed by Partha:
Also add dec = 0; after your print statement. I think the dec values are getting adding to itself for each iteration.
Personally, my brain likes it better like this:
static void Main(string[] args)
{
int dec;
string hex;
int decValue;
string hexReversed;
string hexValues = "0123456789ABCDEF";
do
{
Console.Write("[Press 0 to Stop] Hexadecimal Value: ");
hex = Console.ReadLine().ToUpper().Trim();
if (hex != "0")
{
dec = 0;
hexReversed = new String(hex.Reverse().ToArray());
for (int i = 0; i < hexReversed.Length; i++)
{
decValue = hexValues.IndexOf(hexReversed.Substring(i, 1));
if (decValue != -1)
{
dec = dec + (decValue * (int)Math.Pow(16, i));
}
else
{
Console.WriteLine("Invalid Hexadecimal Value!");
break;
}
}
if (dec != 0)
{
Console.WriteLine(String.Format("{0} hex = {1} dec", hex, dec.ToString()));
}
}
} while (hex != "0");
}
Here is my hex to decimal function, much smaller but it doesnt check if the input is a hex value or not.
private int hex2Decimal(string hex)
{
string hexV = "0123456789ABCDEF"; // For the hex values (ex: F=15)
hex = hex.ToUpper().Replace("0X", ""); // Get good string format
int res;
char[] cArr = hex.ToCharArray();
Array.Reverse(cArr); // Reverse hex string
List<int> iArr = new List<int>();
for (int i = hex.Length - 1; i > -1; i--) // Get the bits
iArr.Add(hexV.IndexOf(cArr[i]) * (int)Math.Pow(16, i)); // Calculate each bits and add to an array
res = iArr.ToArray().Sum(); // Calculate all the numbers and add to the final result
return res;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.