forked from mouredev/retos-programacion-2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCaSaLa.cs
50 lines (38 loc) · 1.15 KB
/
CaSaLa.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// See https://aka.ms/new-console-template for more information
using System.Text.RegularExpressions;
namespace reto14;
public class reto14
{
public static void Main(string[] args)
{
for (int i = 0; i < 1000; i++)
{
Console.WriteLine(ConvertirOctal(i));
}
for (int i = 0; i < 1000; i++)
{
Console.WriteLine(ConvertirHexadecimal(i));
}
}
public static string ConvertirOctal(int numeroAConvertir)
{
return BaseConverter(numeroAConvertir, 8);
}
public static string ConvertirHexadecimal(int numeroAConvertir)
{
return BaseConverter(numeroAConvertir, 16);
}
private static string BaseConverter(int numeroAConvertir, int numBase)
{
if (numeroAConvertir == 0) return 0.ToString();
var digito = "0123456789ABCDEF".ToCharArray();
var resultado = "";
var restoPorConvertir = numeroAConvertir;
while (restoPorConvertir > 0)
{
resultado = digito[restoPorConvertir % numBase] + resultado;
restoPorConvertir /= numBase;
}
return resultado;
}
}