-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathn_to_hex.cpp
More file actions
61 lines (57 loc) · 1.18 KB
/
n_to_hex.cpp
File metadata and controls
61 lines (57 loc) · 1.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
#include "stdafx.h"
__int64 str_hex_to_int(string s){
__int64 num = 0, aux = 0;
int ponto = 0;
for (int i = s.length() - 1; i >= 0; i--){
int j = s.length() - 1 - i - ponto;
char a = s[i];
if (s[i] >= 0x30 && s[i] <= 0x39) aux = s[i] - 0x30;
else if (s[i] >= 0x61 && s[i] <= 0x66) aux = s[i] - 0x61 + 10;
else if (s[i] >= 0x41 && s[i] <= 0x46) aux = s[i] - 0x41 + 10;
else if (s[i] == 0x2E){
num = 0;
ponto = j + 1;
}
else break;
num += (pow(16, j))*aux;
}
return num;
}
__int64 str_dec_to_int(string s){
__int64 num = 0, aux = 0;
int ponto = 0;
for (int i = s.length() - 1; i >= 0; i--){
int j = s.length() - 1 - i - ponto;
char a = s[i];
if (s[i] >= 0x30 && s[i] <= 0x39) aux = s[i] - 0x30;
else if (s[i] == 0x2E){
num = 0;
ponto = j + 1;
}
else if (s[i] == 0x2D){
num *= -1;
break;
}
else break;
num += (pow(10, j))*aux;
}
return num;
}
string int_to_hex(int i)
{
stringstream stream;
stream << hex << i;
string s = stream.str();
//while (s.length() < 8)
// s = "0" + s;
return s;
}
string int_to_dec(int i)
{
stringstream stream;
stream << i;
string s = stream.str();
//while (s.length() < 8)
// s = "0" + s;
return s;
}