-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecToHex.cs
More file actions
50 lines (41 loc) · 1.15 KB
/
decToHex.cs
File metadata and controls
50 lines (41 loc) · 1.15 KB
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
using System;
namespace treinoObjetos
{
public class Solution
{
Dictionary<int, char> hexa = new Dictionary<int, char>
{
{10, 'A'}, {11, 'B'}, {12, 'C'}, {13, 'D'}, {14, 'E'}, {15, 'F'}
};
public string decToHex(int n)
{
string hex = "";
int resto;
while(n > 0)
{
resto = n % 16;
if (resto >= 10)
{
if (hexa.TryGetValue(resto, out char hexChar))
{
hex = (hexChar) + hex;
}
else
{
hex = resto + hex;
}
}
else hex = resto + hex;
n /= 16;
}
return hex;
}
static void Main(string[] args)
{
Solution solution = new Solution();
Console.Write("\nDigite um número decimal: ");
int num = int.Parse(Console.ReadLine());
Console.WriteLine("\nNúmero em hexadecimal: " + solution.decToHex(num));
}
}
}