-
Notifications
You must be signed in to change notification settings - Fork 20
/
Hex2Double.pas
208 lines (195 loc) · 5.26 KB
/
Hex2Double.pas
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
unit Hex2Double;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics,
Controls, Forms, Dialogs, StdCtrls, ExtCtrls;
type
TFHex2DoubleDlg=class(TForm)
rgDataViewStyle: TRadioGroup;
edtValue: TLabeledEdit;
procedure FormShow(Sender : TObject);
procedure edtValueEnter(Sender : TObject);
procedure rgDataViewStyleClick(Sender : TObject);
private
{ Private declarations }
PrevIdx:Integer;
BinData:Array[0..15] of Byte;
Procedure Str2Binary(const AStr:AnsiString);
Function Binary2Str(BytesNum:Integer):AnsiString;
public
{ Public declarations }
end;
var
FHex2DoubleDlg:TFHex2DoubleDlg;
implementation
{$R *.DFM}
Uses Def_main,Misc,Scanf;
procedure TFHex2DoubleDlg.FormShow(Sender : TObject);
begin
rgDataViewStyle.ItemIndex := 0;
PrevIdx := 0;
edtValue.Text := '';
if edtValue.CanFocus then ActiveControl := edtValue;
end;
procedure TFHex2DoubleDlg.edtValueEnter(Sender : TObject);
begin
edtValue.SelectAll;
end;
Procedure TFHex2DoubleDlg.Str2Binary (Const AStr:AnsiString);
var
c:Char;
n:Integer;
src:PAnsiChar;
Begin
src := PAnsiChar(AStr);
FillMemory(@BinData, SizeOf(BinData),0);
n := 0;
while True do
begin
c := src^;
if c=#0 then break;
if c <> ' ' then
begin
sscanf(src, '%l8X', [@BinData[n]]);
Inc(n);
while True do
begin
c := src^;
if (c=#0) or (c = ' ') then break;
Inc(src);
End;
if c=#0 then break;
End;
Inc(src);
End;
end;
Function TFHex2DoubleDlg.Binary2Str (BytesNum:Integer):AnsiString;
var
n:Integer;
Begin
result := '';
for n := 0 To BytesNum-1 do
begin
if n<>0 then result :=Result+' ';
result :=Result + Val2Str(BinData[n],2);
end;
end;
procedure TFHex2DoubleDlg.rgDataViewStyleClick(Sender : TObject);
var
singleVal:Single;
doubleVal:Double;
extendedVal:Extended;
realVal:Real;
compVal:Comp;
result:AnsiString;
begin
if edtValue.Text = '' then
begin
PrevIdx := rgDataViewStyle.ItemIndex;
Exit;
End;
if rgDataViewStyle.ItemIndex = PrevIdx then Exit;
result := edtValue.Text;
try
if rgDataViewStyle.ItemIndex = 0 Then
Begin
//Hex
FillMemory(@BinData, SizeOf(BinData),0);
Case TFloatKind(PrevIdx) Of
FT_SINGLE:
begin
singleVal := StrToFloat(edtValue.Text);
MoveMemory(@BinData, @singleVal, SizeOf(singleVal));
result := Binary2Str(SizeOf(singleVal));
end;
FT_DOUBLE:
begin
doubleVal := StrToFloat(edtValue.Text);
MoveMemory(@BinData, @doubleVal, SizeOf(doubleVal));
result := Binary2Str(SizeOf(doubleVal));
end;
FT_EXTENDED:
begin
extendedVal := StrToFloat(edtValue.Text);
MoveMemory(@BinData, @extendedVal, SizeOf(extendedVal));
result := Binary2Str(SizeOf(extendedVal));
end;
FT_REAL:
begin
realVal := StrToFloat(edtValue.Text);
MoveMemory(@BinData, @realVal, SizeOf(realVal));
result := Binary2Str(SizeOf(realVal));
end;
FT_COMP:
begin
compVal := StrToFloat(edtValue.Text);
MoveMemory(@BinData, @compVal, SizeOf(compVal));
result := Binary2Str(SizeOf(compVal));
End;
end;
end
else
begin
Case TFloatKind(PrevIdx) of
FT_NONE: Str2Binary(edtValue.Text);
FT_SINGLE:
begin
singleVal := StrToFloat(edtValue.Text);
MoveMemory(@BinData, @singleVal, SizeOf(singleVal));
End;
FT_DOUBLE:
begin
doubleVal := StrToFloat(edtValue.Text);
MoveMemory(@BinData, @doubleVal, SizeOf(doubleVal));
End;
FT_EXTENDED:
begin
extendedVal := StrToFloat(edtValue.Text);
MoveMemory(@BinData, @extendedVal, SizeOf(extendedVal));
end;
FT_REAL:
begin
realVal := StrToFloat(edtValue.Text);
MoveMemory(@BinData, @realVal, SizeOf(realVal));
End;
FT_COMP:
begin
compVal := StrToFloat(edtValue.Text);
MoveMemory(@BinData, @compVal, SizeOf(compVal));
end;
End;
Case TFloatKind(rgDataViewStyle.ItemIndex) of
FT_SINGLE:
begin
MoveMemory(@singleVal, @BinData, SizeOf(singleVal));
result := FloatToStr(singleVal);
End;
FT_DOUBLE:
begin
MoveMemory(@doubleVal, @BinData, SizeOf(doubleVal));
result := FloatToStr(doubleVal);
End;
FT_EXTENDED:
begin
MoveMemory(@extendedVal, @BinData, SizeOf(extendedVal));
result := FloatToStr(extendedVal);
End;
FT_REAL:
begin
MoveMemory(@realVal, @BinData, SizeOf(realVal));
result := FloatToStr(realVal);
End;
FT_COMP:
begin
MoveMemory(@compVal, @BinData, SizeOf(compVal));
result := FloatToStr(compVal);
end;
End;
end;
Except
result := 'Impossible!';
End;
PrevIdx := rgDataViewStyle.ItemIndex;
edtValue.Text := result;
end;
end.