forked from mouredev/retos-programacion-2023
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9e0a56e
commit 765cf6c
Showing
1 changed file
with
98 additions
and
0 deletions.
There are no files selected for viewing
98 changes: 98 additions & 0 deletions
98
Retos/Reto #14 - OCTAL Y HEXADECIMAL [Fácil]/c#/Guillermoqnk.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<int> results = new List<int>(); | ||
|
||
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 <int, string> hexaValues= new Dictionary<int, string>() | ||
{ | ||
{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<string> results = new List<string>(); | ||
|
||
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(); | ||
} | ||
} | ||
} |