Hi! I made the calculations on paper first and then verified the numbers with my script. So if the input is 173 the output results are:
> Bin: 10101101
> Hex: AD
Here is the code I wrote to solve the challenge:
using System;
using System.Collections.Generic;
namespace ConsoleApp1
{
class Program
{
//Let's set the input value
static int inputValue = 173;
static void Main()
{
// Print the input value
Console.WriteLine("INPUT VALUE: " + inputValue);
// Convert the input value to binary
ConvertToBinary();
//Convert the input value to hexadecimal
ConvertToHex();
}
static void ConvertToBinary()
{
// Create a copy of the input value and create a list were we store the remainders
int binaryInput = inputValue;
List<int> remainders = new List<int>();
// Divide our input value copy until we reach zero. Add the remainders to our list
while (binaryInput != 0)
{
remainders.Add(binaryInput % 2);
binaryInput /= 2;
}
// Print the results
Console.Write("BINARY: ");
for (int i = remainders.Count - 1; i >= 0; i--)
{
Console.Write(remainders[i]);
}
}
static void ConvertToHex()
{
// Create a copy of the input value and create a list were we store the remainders
int hexInput = inputValue;
List<int> remainders = new List<int>();
// Create a hexadecimal list so we can convert numbers to letters
string[] hexArray = { "0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F" };
// Divide our input value copy until we reach zero. Add the remainders to our list
while (hexInput != 0)
{
remainders.Add(hexInput % 16);
hexInput /= 16;
}
// Print the results
Console.WriteLine();
Console.Write("HEX:");
for (int i = remainders.Count - 1; i >= 0; i--)
{
Console.Write(hexArray[remainders[i]]);
}
}
}
}
Thanks for a great course so far!