From 765cf6c99b7e4a71beb16957a09c7fcb550a12d6 Mon Sep 17 00:00:00 2001 From: Guillermoqnk <62642518+Guillermoqnk@users.noreply.github.com> Date: Mon, 3 Apr 2023 23:07:17 +0200 Subject: [PATCH] Reto #14 - C# --- .../c#/Guillermoqnk.cs" | 98 +++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 "Retos/Reto #14 - OCTAL Y HEXADECIMAL [F\303\241cil]/c#/Guillermoqnk.cs" diff --git "a/Retos/Reto #14 - OCTAL Y HEXADECIMAL [F\303\241cil]/c#/Guillermoqnk.cs" "b/Retos/Reto #14 - OCTAL Y HEXADECIMAL [F\303\241cil]/c#/Guillermoqnk.cs" new file mode 100644 index 0000000000..d0233d7720 --- /dev/null +++ "b/Retos/Reto #14 - OCTAL Y HEXADECIMAL [F\303\241cil]/c#/Guillermoqnk.cs" @@ -0,0 +1,98 @@ +using System.Text; + +namespace Reto14 +{ + internal class Guillermoqnk + { + static void Main(string[] args) + { + string input; + + Console.WriteLine("Introduce the number to transform: "); + + input = Console.ReadLine(); + + try + { + Console.WriteLine("\nThe number in octal: "); + Console.WriteLine(ConvertToOctal(Convert.ToDecimal(input))); + Console.WriteLine("\nThe number in hexadecimal: "); + Console.WriteLine(ConvertToHexadecimal(Convert.ToDecimal(input))); + } + catch { } + } + + public static int ConvertToOctal(decimal input) + { + int numberToTry = (int)input; + + List results = new List(); + + while(numberToTry >= 8) + { + int rest = numberToTry % 8; + + results.Add(rest); + + numberToTry = numberToTry / 8; + } + + results.Add(numberToTry); + + StringBuilder output = new StringBuilder(); + + results.Reverse(); + + foreach(int result in results) + { + output.Append(result); + } + + return Convert.ToInt32(output.ToString()); + } + + public static string ConvertToHexadecimal(decimal input) + { + Dictionary hexaValues= new Dictionary() + { + {0, "0"}, {1,"1"}, {2,"2"}, {3,"3"}, {4,"4"}, {5,"5"}, {6,"6"}, {7, "7"}, {8, "8"}, {9,"9"}, {10, "A"}, {11, "B"}, {12, "C"},{13, "D"},{14, "E"}, {15, "F"} + }; + + int numberToTry = (int)input; + + List results = new List(); + + while (numberToTry >= 16) + { + int rest = numberToTry % 16; + + string hexa = String.Empty; + + if(hexaValues.TryGetValue(rest, out hexa)) + { + results.Add(hexa); + } + + numberToTry = numberToTry / 16; + } + + string hexal; + + if (hexaValues.TryGetValue((int)numberToTry, out hexal)) + { + results.Add(hexal); + } + + StringBuilder output = new StringBuilder(); + + results.Reverse(); + + foreach (string result in results) + { + output.Append(result); + } + + return output.ToString(); + } + } +} \ No newline at end of file