回答編集履歴
1
VC\+\+2010 SP1 インストール記念に質問者の希望とは違うけど検証結果証跡として記録
test
CHANGED
@@ -47,3 +47,187 @@
|
|
47
47
|
コード
|
48
48
|
|
49
49
|
```
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
```C
|
54
|
+
|
55
|
+
// TEST01.cpp : コンソール アプリケーションのエントリ ポイントを定義します。
|
56
|
+
|
57
|
+
//
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
#include "stdafx.h"
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
#include <stdio.h>
|
66
|
+
|
67
|
+
#include <stdlib.h>
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
typedef unsigned char uchar;
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
typedef union bitfloat_t {
|
76
|
+
|
77
|
+
float value;
|
78
|
+
|
79
|
+
uchar bytes[sizeof(float)];
|
80
|
+
|
81
|
+
} bitfloat_t;
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
#define FLOATLEN sizeof(float) * 8
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
float bits2float(uchar* buffer);
|
90
|
+
|
91
|
+
void clearMem(uchar* buffer, int size);
|
92
|
+
|
93
|
+
void float2bits(float v, uchar* buffer);
|
94
|
+
|
95
|
+
void test(char *title, float val);
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
int _tmain(int argc, _TCHAR* argv[])
|
100
|
+
|
101
|
+
{
|
102
|
+
|
103
|
+
test("テスト1", 1e26F);
|
104
|
+
|
105
|
+
test("テスト2", 1.1234567890123456789F);
|
106
|
+
|
107
|
+
test("テスト3", 1.1234567e10F);
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
return 0;
|
112
|
+
|
113
|
+
}
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
void test(char *title, float val) {
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
bitfloat_t v;
|
122
|
+
|
123
|
+
uchar buffer[FLOATLEN +1];
|
124
|
+
|
125
|
+
v.value = val;
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
float2bits(val, buffer);
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
printf("%s\n", title);
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
printf("実数値→変換:%f => %s\n", val, buffer);
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
float ff = bits2float(buffer);
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
printf("変換→実数値:%s => %f(%e)\n", buffer, ff, ff);
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
fgetc(stdin);
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
}
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
void clearMem(uchar* buffer, int size) {
|
158
|
+
|
159
|
+
for (int i=0;i < size;i++) buffer[i] = 0;
|
160
|
+
|
161
|
+
}
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
float bits2float(uchar* buffer) {
|
166
|
+
|
167
|
+
bitfloat_t vv;
|
168
|
+
|
169
|
+
clearMem(vv.bytes, sizeof(vv));
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
int maxLen = FLOATLEN;
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
uchar bit = 0;
|
178
|
+
|
179
|
+
for (int i=0;i < maxLen;i++) {
|
180
|
+
|
181
|
+
if (bit == 0) bit = 0x80;
|
182
|
+
|
183
|
+
vv.bytes[i / 8] |= buffer[i] == '1' ? bit : 0;
|
184
|
+
|
185
|
+
bit >>= 1;
|
186
|
+
|
187
|
+
}
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
return vv.value;
|
192
|
+
|
193
|
+
}
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
void float2bits(float v, uchar* buffer) {
|
198
|
+
|
199
|
+
bitfloat_t vv;
|
200
|
+
|
201
|
+
vv.value = v;
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
int maxLen = FLOATLEN;
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
buffer[maxLen] = 0;
|
210
|
+
|
211
|
+
uchar bit = 0;
|
212
|
+
|
213
|
+
for (int i=0;i < maxLen;i++) {
|
214
|
+
|
215
|
+
if (bit == 0) bit = 0x80;
|
216
|
+
|
217
|
+
buffer[i] = vv.bytes[i / 8] & bit ? '1' : '0';
|
218
|
+
|
219
|
+
bit >>= 1;
|
220
|
+
|
221
|
+
}
|
222
|
+
|
223
|
+
}
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
```
|
230
|
+
|
231
|
+
VC++2010 Sp1 当てた記念に残しておきます。
|
232
|
+
|
233
|
+
![実行結果](534b5c41ba8ead760ae374e774f652e3.png)
|