Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- public class Program
- {
- public static string input_selection;
- public static string output_selection;
- public static void Main()
- {
- //Where the code actually starts
- menu_input();
- }
- public static void menu_draw()
- {
- Console.WriteLine("Hi, What data type you want to input?");
- Console.WriteLine("");
- Console.WriteLine(" +---------------[OPTION]-----------[EXAMPLE]-----[FUNCTIONALITY]-----+");
- Console.WriteLine(" | 1) Base 10: Decimal '42' (FULLY WORKING) |");
- Console.WriteLine(" | 2) Base 16: Hexadecimal '2A' (NEARLY) |");
- Console.WriteLine(" | 3) Base 2: Binary '101010' (NO) |");
- Console.WriteLine(" +--------------------------------------------------------------------+");
- }
- public static void menu_input()
- {
- menu_draw();
- //Variable assignment
- string string_input = Console.ReadLine();
- //Checks if the user input is a valid option
- switch (string_input.ToUpper())
- {
- case "1":
- case "BASE 10":
- case "DECIMAL":
- case "DENARY":
- input_selection = "decimal";
- Console.WriteLine("Okay, " + input_selection + " it is:");
- menu_output();
- break;
- case "2":
- case "BASE 16":
- case "HEX":
- case "HEXADECIMAL":
- input_selection = "hex";
- Console.WriteLine("Okay, " + input_selection + " it is:");
- menu_output();
- break;
- case "3":
- case "BASE 2":
- case "BINARY":
- input_selection = "binary";
- Console.WriteLine("Okay, " + input_selection + " it is:");
- menu_output();
- break;
- default:
- Console.WriteLine("Invalid input, try again.\n\n");
- menu_input();
- break;
- }
- }
- public static void menu_output()
- {
- menu_draw();
- //Variable assign
- string string_output = Console.ReadLine();
- //Variable assign
- //Checks if the user input is a valid option
- switch (string_output.ToUpper())
- {
- case "1":
- case "BASE 10":
- case "DECIMAL":
- case "DENARY":
- Console.WriteLine("Okay, converting " + input_selection + " to decimal:");
- output_selection = "decimal";
- forward_onto();
- break;
- case "2":
- case "BASE 16":
- case "HEX":
- case "HEXADECIMAL":
- Console.WriteLine("Okay, converting " + input_selection + " to hexadecimal:");
- output_selection = "hex";
- forward_onto();
- break;
- case "3":
- case "BASE 2":
- case "BINARY":
- Console.WriteLine("Okay, converting " + input_selection + " to binary:");
- output_selection = "binary";
- forward_onto();
- break;
- default:
- Console.WriteLine("Invalid input, try again.\n\n");
- menu_output();
- break;
- }
- }
- public static string data_input()
- {
- Console.WriteLine("Enter a " + input_selection + " value:");
- string data_input = Console.ReadLine();
- return data_input;
- }
- public static void forward_onto()
- {
- switch (input_selection)
- {
- case "decimal":
- switch (output_selection)
- {
- case "decimal":
- decimal2decimal();
- break;
- case "hex":
- decimal2hex();
- break;
- case "binary":
- decimal2binary();
- break;
- default:
- Console.Write("Error");
- break;
- }
- break;
- case "hex":
- switch (output_selection)
- {
- case "decimal":
- hex2decimal();
- break;
- case "hex":
- hex2hex();
- break;
- case "binary":
- hex2binary();
- break;
- default:
- Console.Write("Error");
- break;
- }
- break;
- case "binary":
- switch (output_selection)
- {
- case "decimal":
- binary2decimal();
- break;
- case "hex":
- binary2hex();
- break;
- case "binary":
- binary2binary();
- break;
- default:
- Console.Write("Error");
- break;
- }
- break;
- default:
- Console.Write("Error");
- break;
- }
- }
- public static void decimal2decimal()
- {
- Console.Write("Wow you're an idiot, " + input_selection + " and " + output_selection + " are the same thing!\nTry again!");
- Console.WriteLine("\n\n\n");
- menu_draw();
- menu_input();
- }
- public static void decimal2hex()
- {
- char[] hex = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
- string input = "";
- input = data_input();
- decimal_check(input);
- //Variable assign
- long hexinput = 0;
- string hex_finalstring = "";
- //char[] output = new char[20];
- try
- {
- hexinput = Convert.ToInt64(input);
- }
- catch (FormatException)
- {
- }
- //Variable assign
- //Do all the maths stuff to get the hex value and append it to a string
- for (int i = 0; (i > 10) | (hexinput != 0); i++)
- {
- hex_finalstring += (hex[hexinput % 16]);
- hexinput = hexinput / 16;
- }
- //Do all the maths stuff to get the hex value and append it to a string
- //Return value to the main method
- result(hex_finalstring);
- }
- public static void decimal2binary()
- {
- string input = data_input();
- decimal_check(input);
- //variable assign
- string binary_finalstring = "";
- long[] binary = new long[32];
- long seperator = 1073741824;
- long int_input = 0;
- try
- {
- int_input = Convert.ToInt64(input);
- }
- catch (FormatException)
- {
- }
- bool start_binary = false;
- //variable assign
- //Do all the maths stuff to get the binary value and put it in an array
- for (int arraypos = 0; arraypos <= 31;)
- {
- if (int_input >= seperator)
- {
- int_input = int_input - seperator;
- binary[arraypos] = 1;
- }
- else
- {
- binary[arraypos] = 0;
- }
- seperator /= 2;
- arraypos++;
- }
- //Do all the maths stuff to get the binary value and put it in an array
- //Check where the binary actually starts (we want 1101 not 000000001101)
- for (int i = 0; i < 31; i++)
- {
- if (start_binary == false)
- {
- if (binary[i] == 1)
- {
- start_binary = true;
- }
- }
- if (start_binary == true)
- {
- //Append a string with the binary value of the value in each array slot
- binary_finalstring += binary[i].ToString();
- //Append a string with the binary value of the value in each array slot
- }
- }
- result(binary_finalstring);
- //Convert to an integer and return the value back to the main method
- }
- public static void hex2decimal()
- {
- char[] hex = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
- string input = data_input();
- char[] hex_char = input.ToUpper().ToCharArray();
- int[] hex_final = new int[hex_char.Length];
- bool done = false;
- bool clear = true;
- double final_hex_value = 0;
- int position = 0;
- int exponent = hex_final.Length - 1;
- for (int hex_letter = 0; hex_letter < hex_char.Length && done == false;)
- {
- for (int hex_alpha = 0; hex_alpha < hex.Length && done == false;)
- {
- /*
- Console.Write("\n\n hex_char(input): ");
- for (int i = 0; i < hex_char.Length; i++)
- {
- Console.Write(hex_char[i]);
- }
- Console.Write("\n hex_final: ");
- for (int i = 0; i < hex_final.Length; i++)
- {
- Console.Write(hex_final[i]);
- }
- Console.WriteLine("\n"+hex_char.Length + "\n" + hex_letter + "\n hex_alpha: " + hex_alpha + "\n hex_letter: " + hex_letter);
- */
- if (hex_char[hex_letter].Equals(hex[hex_alpha]))
- {
- hex_final[hex_letter] = hex_alpha;
- hex_alpha = -1;
- hex_letter++;
- }
- if (hex_letter == hex_char.Length)
- {
- done = true;
- }
- hex_alpha++;
- }
- hex_letter++;
- }
- for (int i = hex_final.Length - 1; i != 0; i--)
- {
- if (hex_final[i] == 0)
- {
- clear = false;
- Array.Clear(hex_final, 0, hex_final.Length);
- }
- }
- if (clear == true)
- {
- for (position = 0; position != hex_final.Length; position++)
- {
- final_hex_value += hex_final[position] * (Math.Pow(16, exponent));
- exponent--;
- }
- Console.WriteLine(final_hex_value);
- }
- else
- {
- Console.WriteLine("Incorrect character entered, try again\n\n");
- hex2decimal();
- }
- }
- public static void hex2hex()
- {
- Console.Write("Wow you're an idiot, " + input_selection + " and " + output_selection + " are the same thing!\nTry again!");
- Console.WriteLine("\n\n\n");
- menu_draw();
- menu_input();
- }
- public static void hex2binary()
- {
- }
- public static void binary2decimal()
- {
- }
- public static void binary2hex()
- {
- }
- public static void binary2binary()
- {
- Console.Write("Wow you're an idiot, " + input_selection + " and " + output_selection + " are the same thing!\nTry again!");
- Console.WriteLine("\n\n\n");
- menu_draw();
- menu_input();
- }
- public static void decimal_check(string input)
- {
- long parse_check;
- if (long.TryParse(input, out parse_check) == false)
- {
- Console.WriteLine("Invalid input, try again.\n\n");
- forward_onto();
- }
- else
- {
- if ((Convert.ToInt64(input) < 1))
- {
- Console.WriteLine("Invalid input, try again.\n\n");
- forward_onto();
- }
- }
- }
- public static void result(string final)
- {
- Console.Write(final);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement