-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconvertAnyBase.cpp
More file actions
95 lines (81 loc) · 2.18 KB
/
convertAnyBase.cpp
File metadata and controls
95 lines (81 loc) · 2.18 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
# Descrição:
Dados dois inteiros positivos A e B e uma string S de tamanho N, denotando um
número na base A, o objetivo da função "convertBase()" é converter a string S
da base A para a base B. A ideia do algoritmo é converter para a string S para
a representação decimal e posteriormente converte-lo para a nova base B.
# Funções auxiliares:
int toDeci(string str, int base) -> Converte um número de uma determinada base para decimal.
string fromDeci(int base, int inputNum) -> Converte um número na base decimal para outra base.
int val(char c) -> Função que retorna o ASCII de um determinado caracter.
char reVal(int num) -> Função para retornar o char equivalente ao int "num".
# Parâmetros:
string s -> Número na base "a" a ser convertido.
ll a -> Base original do número
ll b -> Nova base.
# Retorno
A função "convertBase(string s, ll a, ll b)" retorna a conversão do número
de base "a" representado pela string "s" para a base "b".
# Complexidade:
O(N), onde N é o tamanho da string "s".
# Referência:
https://www.geeksforgeeks.org/convert-a-number-from-base-a-to-base-b/
# Problemas:
BEE 1538
*/
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
ll val(char c)
{
if (c >= '0' && c <= '9')
return (ll)c - '0';
else
return (ll)c - 'A' + 10;
}
char reVal(ll num)
{
if (num >= 0 && num <= 9)
return (char)(num + '0');
else
return (char)(num - 10 + 'A');
}
ll toDeci(string str, ll base)
{
ll len = str.size();
ll power = 1;
ll num = 0;
for (int i = len - 1; i >= 0; i--)
{
if (val(str[i]) >= base)
{
printf("Invalid Number");
return -1;
}
num += val(str[i]) * power;
power = power * base;
}
return num;
}
string fromDeci(ll base, ll inputNum)
{
string res = "";
while (inputNum > 0)
{
res += reVal(inputNum % base);
inputNum /= base;
}
reverse(res.begin(), res.end());
return res;
}
string convertBase(string s, ll a, ll b)
{
ll num = toDeci(s, a);
string ans = fromDeci(b, num);
return ans;
}
int main()
{
cout << convertBase("FF", 16, 2) << endl;
return 0;
}