using System;
using System.Collections;
using System.Text;
using System.Text.RegularExpressions;
/*
DECIMAL-BINARY INTERCONVERTER
BENEDICT W. HAZEL
This program interconverts between binary and decimal numbers. To use
it enter a calculation mode and number on two separate lines into
the stdin text field, e.g.
[mode]
[number]
For the mode use:
bin - decimal -> binary
dec - binary -> decimal
The possible range for both modes is 0 to 65535 due to data type
restrictions.
*/
/// <summary>
/// Binary to decimal interconverter
/// </summary>
public class Numerics
{
/// <summary>
/// Converts a decimal number to binary.
/// </summary>
/// <param name="decimalNumber">The decimal number to convert.</param>
/// <returns>The binary representation of the decimal number.</returns>
public static string ToBinary(uint decimalNumber)
{
StringBuilder binNumBuilder = new StringBuilder();
uint divisionTotal = decimalNumber;
byte remainder = 0;
while (divisionTotal >= 1)
{
remainder = (byte)(divisionTotal % 2);
binNumBuilder.Insert(0, remainder);
divisionTotal /= 2;
}
return binNumBuilder.ToString();
}
/// <summary>
/// Convertsa binary number to decimal.
/// </summary>
/// <param name="binaryNumber">The binary number to convert.</param>
/// <returns>The decimal representation of the binary number.</returns>
public static uint FromBinary(string binaryNumber)
{
BitArray bits = GetBits(binaryNumber);
uint decimalNumber = 0;
for (int i = 1; i <= bits.Length; i++)
{
if (bits[i - 1] == true)
{
decimalNumber += (uint)Math.Pow(2, binaryNumber.Length - i);
}
}
return decimalNumber;
}
/// <summary>
/// Converts a string representation of a binary number into a <see cref="BitArray" />.
///</summary>
/// <param name="binaryNumber">The string representation of a binary number.</param>
/// <returns>The binary number as a <see cref="BitArray" />.
public static BitArray GetBits (string binaryNumber)
{
BitArray bits = new BitArray(binaryNumber.Length);
byte boolConversion = 0;
for (int i = 0; i < bits.Length; i++)
{
boolConversion = byte.Parse(binaryNumber[i].ToString());
bits[i] = Convert.ToBoolean(boolConversion);
}
return bits;
}
}
/// <summary>
/// Main program.
/// </summary>
public class Program
{
/// <summary>
/// Main program code.
/// </summary>
public static void Main(string[] args)
{
string calculationMode = Console.ReadLine();
string inputNumber = Console.ReadLine();
if (calculationMode == "bin")
{
uint uintConversionOutput = 0;
if (uint.TryParse(inputNumber, out uintConversionOutput))
{
string binaryNumber = Numerics.ToBinary(uint.Parse(inputNumber));
Console.WriteLine(binaryNumber);
}
else
{
WriteError(inputNumber);
}
}
else if (calculationMode == "dec")
{
if (Regex.IsMatch(inputNumber, "^[01]*$"))
{
uint decimalNumber = Numerics.FromBinary(inputNumber);
Console.WriteLine(decimalNumber);
}
else
{
WriteError(inputNumber);
}
}
else
{
WriteError(calculationMode);
}
}
/// <summary>
/// Writes an error message to the console.
/// </summary>
/// <param name="input">The input causing the error.</param>
public static void WriteError(string input)
{
Console.WriteLine("An error occurred when processing {0}.", input);
}
}