Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.01 KB | None | 0 0
  1. using System;
  2.  
  3. public class Program
  4. {
  5. public static string input_selection;
  6. public static string output_selection;
  7.  
  8. public static void Main()
  9. {
  10. //Where the code actually starts
  11. menu_input();
  12. }
  13.  
  14. public static void menu_draw()
  15. {
  16. Console.WriteLine("Hi, What data type you want to input?");
  17. Console.WriteLine("");
  18. Console.WriteLine(" +---------------[OPTION]-----------[EXAMPLE]-----[FUNCTIONALITY]-----+");
  19. Console.WriteLine(" | 1) Base 10: Decimal '42' (FULLY WORKING) |");
  20. Console.WriteLine(" | 2) Base 16: Hexadecimal '2A' (NEARLY) |");
  21. Console.WriteLine(" | 3) Base 2: Binary '101010' (NO) |");
  22. Console.WriteLine(" +--------------------------------------------------------------------+");
  23. }
  24.  
  25. public static void menu_input()
  26. {
  27. menu_draw();
  28. //Variable assignment
  29. string string_input = Console.ReadLine();
  30. //Checks if the user input is a valid option
  31. switch (string_input.ToUpper())
  32. {
  33. case "1":
  34. case "BASE 10":
  35. case "DECIMAL":
  36. case "DENARY":
  37. input_selection = "decimal";
  38. Console.WriteLine("Okay, " + input_selection + " it is:");
  39. menu_output();
  40. break;
  41. case "2":
  42. case "BASE 16":
  43. case "HEX":
  44. case "HEXADECIMAL":
  45. input_selection = "hex";
  46. Console.WriteLine("Okay, " + input_selection + " it is:");
  47. menu_output();
  48. break;
  49. case "3":
  50. case "BASE 2":
  51. case "BINARY":
  52. input_selection = "binary";
  53. Console.WriteLine("Okay, " + input_selection + " it is:");
  54. menu_output();
  55. break;
  56. default:
  57. Console.WriteLine("Invalid input, try again.\n\n");
  58. menu_input();
  59. break;
  60. }
  61. }
  62.  
  63. public static void menu_output()
  64. {
  65. menu_draw();
  66. //Variable assign
  67. string string_output = Console.ReadLine();
  68. //Variable assign
  69. //Checks if the user input is a valid option
  70. switch (string_output.ToUpper())
  71. {
  72. case "1":
  73. case "BASE 10":
  74. case "DECIMAL":
  75. case "DENARY":
  76. Console.WriteLine("Okay, converting " + input_selection + " to decimal:");
  77. output_selection = "decimal";
  78. forward_onto();
  79. break;
  80. case "2":
  81. case "BASE 16":
  82. case "HEX":
  83. case "HEXADECIMAL":
  84. Console.WriteLine("Okay, converting " + input_selection + " to hexadecimal:");
  85. output_selection = "hex";
  86. forward_onto();
  87. break;
  88. case "3":
  89. case "BASE 2":
  90. case "BINARY":
  91. Console.WriteLine("Okay, converting " + input_selection + " to binary:");
  92. output_selection = "binary";
  93. forward_onto();
  94. break;
  95. default:
  96. Console.WriteLine("Invalid input, try again.\n\n");
  97. menu_output();
  98. break;
  99. }
  100. }
  101.  
  102. public static string data_input()
  103. {
  104. Console.WriteLine("Enter a " + input_selection + " value:");
  105. string data_input = Console.ReadLine();
  106. return data_input;
  107. }
  108.  
  109. public static void forward_onto()
  110. {
  111. switch (input_selection)
  112. {
  113. case "decimal":
  114. switch (output_selection)
  115. {
  116. case "decimal":
  117. decimal2decimal();
  118. break;
  119. case "hex":
  120. decimal2hex();
  121. break;
  122. case "binary":
  123. decimal2binary();
  124. break;
  125. default:
  126. Console.Write("Error");
  127. break;
  128. }
  129.  
  130. break;
  131. case "hex":
  132. switch (output_selection)
  133. {
  134. case "decimal":
  135. hex2decimal();
  136. break;
  137. case "hex":
  138. hex2hex();
  139. break;
  140. case "binary":
  141. hex2binary();
  142. break;
  143. default:
  144. Console.Write("Error");
  145. break;
  146. }
  147.  
  148. break;
  149. case "binary":
  150. switch (output_selection)
  151. {
  152. case "decimal":
  153. binary2decimal();
  154. break;
  155. case "hex":
  156. binary2hex();
  157. break;
  158. case "binary":
  159. binary2binary();
  160. break;
  161. default:
  162. Console.Write("Error");
  163. break;
  164. }
  165.  
  166. break;
  167. default:
  168. Console.Write("Error");
  169. break;
  170. }
  171. }
  172.  
  173. public static void decimal2decimal()
  174. {
  175. Console.Write("Wow you're an idiot, " + input_selection + " and " + output_selection + " are the same thing!\nTry again!");
  176. Console.WriteLine("\n\n\n");
  177. menu_draw();
  178. menu_input();
  179. }
  180.  
  181. public static void decimal2hex()
  182. {
  183. char[] hex = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
  184. string input = "";
  185. input = data_input();
  186. decimal_check(input);
  187. //Variable assign
  188. long hexinput = 0;
  189. string hex_finalstring = "";
  190. //char[] output = new char[20];
  191. try
  192. {
  193. hexinput = Convert.ToInt64(input);
  194. }
  195. catch (FormatException)
  196. {
  197. }
  198.  
  199. //Variable assign
  200. //Do all the maths stuff to get the hex value and append it to a string
  201. for (int i = 0; (i > 10) | (hexinput != 0); i++)
  202. {
  203. hex_finalstring += (hex[hexinput % 16]);
  204. hexinput = hexinput / 16;
  205. }
  206.  
  207. //Do all the maths stuff to get the hex value and append it to a string
  208. //Return value to the main method
  209. result(hex_finalstring);
  210. }
  211.  
  212. public static void decimal2binary()
  213. {
  214. string input = data_input();
  215. decimal_check(input);
  216. //variable assign
  217. string binary_finalstring = "";
  218. long[] binary = new long[32];
  219. long seperator = 1073741824;
  220. long int_input = 0;
  221. try
  222. {
  223. int_input = Convert.ToInt64(input);
  224. }
  225. catch (FormatException)
  226. {
  227. }
  228.  
  229. bool start_binary = false;
  230. //variable assign
  231. //Do all the maths stuff to get the binary value and put it in an array
  232. for (int arraypos = 0; arraypos <= 31;)
  233. {
  234. if (int_input >= seperator)
  235. {
  236. int_input = int_input - seperator;
  237. binary[arraypos] = 1;
  238. }
  239. else
  240. {
  241. binary[arraypos] = 0;
  242. }
  243.  
  244. seperator /= 2;
  245. arraypos++;
  246. }
  247.  
  248. //Do all the maths stuff to get the binary value and put it in an array
  249. //Check where the binary actually starts (we want 1101 not 000000001101)
  250. for (int i = 0; i < 31; i++)
  251. {
  252. if (start_binary == false)
  253. {
  254. if (binary[i] == 1)
  255. {
  256. start_binary = true;
  257. }
  258. }
  259.  
  260. if (start_binary == true)
  261. {
  262. //Append a string with the binary value of the value in each array slot
  263. binary_finalstring += binary[i].ToString();
  264. //Append a string with the binary value of the value in each array slot
  265. }
  266. }
  267.  
  268. result(binary_finalstring);
  269. //Convert to an integer and return the value back to the main method
  270. }
  271.  
  272. public static void hex2decimal()
  273. {
  274. char[] hex = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
  275. string input = data_input();
  276. char[] hex_char = input.ToUpper().ToCharArray();
  277. int[] hex_final = new int[hex_char.Length];
  278. bool done = false;
  279. bool clear = true;
  280. double final_hex_value = 0;
  281. int position = 0;
  282. int exponent = hex_final.Length - 1;
  283. for (int hex_letter = 0; hex_letter < hex_char.Length && done == false;)
  284. {
  285. for (int hex_alpha = 0; hex_alpha < hex.Length && done == false;)
  286. {
  287. /*
  288. Console.Write("\n\n hex_char(input): ");
  289. for (int i = 0; i < hex_char.Length; i++)
  290. {
  291. Console.Write(hex_char[i]);
  292. }
  293.  
  294. Console.Write("\n hex_final: ");
  295. for (int i = 0; i < hex_final.Length; i++)
  296. {
  297. Console.Write(hex_final[i]);
  298. }
  299.  
  300. Console.WriteLine("\n"+hex_char.Length + "\n" + hex_letter + "\n hex_alpha: " + hex_alpha + "\n hex_letter: " + hex_letter);
  301. */
  302. if (hex_char[hex_letter].Equals(hex[hex_alpha]))
  303. {
  304. hex_final[hex_letter] = hex_alpha;
  305. hex_alpha = -1;
  306. hex_letter++;
  307. }
  308.  
  309. if (hex_letter == hex_char.Length)
  310. {
  311. done = true;
  312. }
  313.  
  314. hex_alpha++;
  315. }
  316.  
  317. hex_letter++;
  318. }
  319.  
  320. for (int i = hex_final.Length - 1; i != 0; i--)
  321. {
  322. if (hex_final[i] == 0)
  323. {
  324. clear = false;
  325. Array.Clear(hex_final, 0, hex_final.Length);
  326. }
  327. }
  328.  
  329. if (clear == true)
  330. {
  331. for (position = 0; position != hex_final.Length; position++)
  332. {
  333. final_hex_value += hex_final[position] * (Math.Pow(16, exponent));
  334. exponent--;
  335. }
  336.  
  337. Console.WriteLine(final_hex_value);
  338. }
  339. else
  340. {
  341. Console.WriteLine("Incorrect character entered, try again\n\n");
  342. hex2decimal();
  343. }
  344. }
  345.  
  346. public static void hex2hex()
  347. {
  348. Console.Write("Wow you're an idiot, " + input_selection + " and " + output_selection + " are the same thing!\nTry again!");
  349. Console.WriteLine("\n\n\n");
  350. menu_draw();
  351. menu_input();
  352. }
  353.  
  354. public static void hex2binary()
  355. {
  356. }
  357.  
  358. public static void binary2decimal()
  359. {
  360. }
  361.  
  362. public static void binary2hex()
  363. {
  364. }
  365.  
  366. public static void binary2binary()
  367. {
  368. Console.Write("Wow you're an idiot, " + input_selection + " and " + output_selection + " are the same thing!\nTry again!");
  369. Console.WriteLine("\n\n\n");
  370. menu_draw();
  371. menu_input();
  372. }
  373.  
  374. public static void decimal_check(string input)
  375. {
  376. long parse_check;
  377. if (long.TryParse(input, out parse_check) == false)
  378. {
  379. Console.WriteLine("Invalid input, try again.\n\n");
  380. forward_onto();
  381. }
  382. else
  383. {
  384. if ((Convert.ToInt64(input) < 1))
  385. {
  386. Console.WriteLine("Invalid input, try again.\n\n");
  387. forward_onto();
  388. }
  389. }
  390. }
  391.  
  392. public static void result(string final)
  393. {
  394. Console.Write(final);
  395. }
  396. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement