LAB: Hex/Decimal Converter

There are times that it is necessary to convert between hex and decimals numbers. Recently I was using a hex color on a web site and then wanted to use the same color with an alpha value to make the color semi-transparent. This is very easy to do in CSS but you can’t do it with the hex values so I had to convert them into their decimal equivalents for the RGB values. This also is rather easy to do in C# code so here are the objectives for this lab:

Create a basic console application that will:

  • Ask a user if they want to convert from hex to decimal or vice versa and capture their response
  • Ask user to input a two character hex value if they want to convert from hex to decimal
  • Ask the user to input a numeric value up to 255 if they want to convert from decimal to hex
  • Capture their input and preform the conversion then display the conversion results
  • Allow the user to repeat the process as many times as they would desire.

/**************************************************************************************/
/**************************************************************************************/
/** EXAMPLE SOLUTION *** EXAMPLE SOLUTION *** EXAMPLE SOLUTION **/
/**************************************************************************************/
/**************************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HexDecimalConverter
{
    class Program
    {
        static void Main()
        {
            Console.WriteLine("Hex/Decimal Converter");
            Console.WriteLine();

            Console.WriteLine("---------------------------------------------------------");
            Console.WriteLine("Please enter A for converting Hex to Decimal.\nPlease enter B for converting Decimal to Hex.");
            string conversionChoice = Console.ReadLine();
            Console.WriteLine();

            if (conversionChoice.ToUpper() == "A")
            {
                Console.WriteLine("Please enter a two character hexidecimal code.");
                string hexValue = Console.ReadLine().ToUpper();
                Console.WriteLine();

                int decValue = Convert.ToInt32(hexValue, 16);
                Console.WriteLine("The Decimal value of Hex code {0} is {1}", hexValue, decValue);
                Console.WriteLine();

                Main();
            }
            else if (conversionChoice.ToUpper() == "B")
            {
                Console.WriteLine("Please enter a number from 0 to 255");
                string decValue = Console.ReadLine();
                Console.WriteLine();

                string hexValue = Byte.Parse(decValue).ToString("x").ToUpper();
                Console.WriteLine("The Hex code of {0} is {1}", decValue, hexValue);
                Console.WriteLine();

                Main();
            }
            else {
                Console.WriteLine("Please enter one of the two choice options provided.");
                Console.WriteLine();
                Main();
            }
        }
    }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s