-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhex2float.cpp
More file actions
63 lines (58 loc) · 1.37 KB
/
hex2float.cpp
File metadata and controls
63 lines (58 loc) · 1.37 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
#include <iostream>
#include <cmath>
#include <string>
#include <stdlib.h>
using namespace std;
union fi{
uint32_t i;
float f;
} ;
uint32_t hex2int(string hex) {
// std::stoiがうまく機能しないので自分で実装。
int ofst = (hex.at(1) == 'x') ? 2 : 0;
uint32_t ret = 0x00000000;
for (int i=0; i<hex.size()-ofst; i++){
ret <<= 4;
if ((uint32_t)(hex.at(i + ofst) - '0') < 10){ // num
ret += (uint32_t)(hex.at(i + ofst) - '0');
}
else {
ret += (uint32_t)(hex.at(i + ofst) - 'A') + (uint32_t)10;
}
}
return ret;
}
int main (int argc, char** argv){
if (argv[1][0] == 'd'){
while (1) {
uint32_t s;
cin >> s;
union fi fl;
fl.i = s;
printf("%8.8f\n", fl.f);
cout << hex << "0x" << fl.i << endl;
}
}
else if (argv[1][0] == 'b'){
union fi temp;
string str;
while (1){
cin >> str;
temp.i = stoul(str, 0, 2);
cout << temp.f << endl;
}
}
else {
// 16進
while (1) {
string s;
cin >> s;
uint32_t float_bit = hex2int(s);
union fi fl;
fl.i = float_bit;
cout << hex << float_bit << endl;
printf("%8.8f\n", fl.f);
}
}
return 0;
}