OSDN Git Service

4bit単位でグループ化,16進数表示対応
[fugashi/fugashi.git] / Fugashi / MainForm.cs
1 /*\r
2  *      Fugashi Project\r
3  *\r
4  *      This software is distributed under a BSD-style license.\r
5  *      See license.txt for more information.\r
6  */\r
7 \r
8 using System;\r
9 using System.Collections.Generic;\r
10 using System.ComponentModel;\r
11 using System.Data;\r
12 using System.Drawing;\r
13 using System.Text;\r
14 using System.Windows.Forms;\r
15 \r
16 namespace Fugashi\r
17 {\r
18     public partial class MainForm : Form\r
19     {\r
20         private BitSwitch[] bits;\r
21         private int length = 4;\r
22 \r
23         public MainForm()\r
24         {\r
25             InitializeComponent();\r
26 \r
27             bits = new BitSwitch[64];\r
28             foreach (Control c in Controls)\r
29             {\r
30                 if (c is BitSwitches4)\r
31                 {\r
32                     BitSwitches4 bs = c as BitSwitches4;\r
33                     int i = bs.StartBit;\r
34                     bits[i + 0] = bs.B0;\r
35                     bits[i + 1] = bs.B1;\r
36                     bits[i + 2] = bs.B2;\r
37                     bits[i + 3] = bs.B3;\r
38                 }\r
39             }\r
40             foreach (BitSwitch b in bits)\r
41             {\r
42                 b.StateChanged += BitChanged;\r
43             }\r
44             Font mono = new Font(FontFamily.GenericMonospace, textHex.Font.Size);\r
45             textHex.Font = mono;\r
46             textValue.Font = mono;\r
47 \r
48         }\r
49         private void radioUint_CheckedChanged(object sender, EventArgs e)\r
50         {\r
51             if (radioUint.Checked)\r
52             {\r
53                 UpdateUint(GetBytes());\r
54             }\r
55         }\r
56 \r
57         private void radioInt_CheckedChanged(object sender, EventArgs e)\r
58         {\r
59             if (radioInt.Checked)\r
60             {\r
61                 UpdateInt(GetBytes());\r
62             }\r
63         }\r
64 \r
65         private void radioFloat_CheckedChanged(object sender, EventArgs e)\r
66         {\r
67             if (radioFloat.Checked)\r
68             {\r
69                 radio1B.Enabled = false;\r
70                 radio2B.Enabled = false;\r
71                 if (radio1B.Checked || radio2B.Checked)\r
72                 {\r
73                     radio4B.Checked = true;\r
74                 }\r
75                 SetForFloatingPoint(radio8B.Checked);\r
76                 UpdateFloat(GetBytes());\r
77             }\r
78             else\r
79             {\r
80                 SetForInteger();\r
81                 radio1B.Enabled = true;\r
82                 radio2B.Enabled = true;\r
83             }\r
84         }\r
85 \r
86         private void MainForm_Load(object sender, EventArgs e)\r
87         {\r
88             radioUint.Checked = true;\r
89             radio4B.Checked = true;\r
90         }\r
91 \r
92         private void SetForFloatingPoint(bool isDouble)\r
93         {\r
94             Color c1 = isDouble ? Color.White : Color.Moccasin;\r
95             Color c2 = isDouble ? Color.Moccasin : Color.White;\r
96             for (int i = 23; i < 31; ++i)\r
97             {\r
98                 bits[i].BackColor = c1;\r
99             }\r
100             for (int i = 52; i < 63; ++i)\r
101             {\r
102                 bits[i].BackColor = c2;\r
103             }\r
104         }\r
105 \r
106         private void SetForInteger()\r
107         {\r
108             for (int i = 0; i < 64; ++i)\r
109             {\r
110                 bits[i].BackColor = Color.White;\r
111             }\r
112         }\r
113 \r
114 \r
115         private byte[] GetBytes()\r
116         {\r
117             byte[] b = new Byte[length];\r
118             int i = 0;\r
119             for (int j = 0; j < length; ++j)\r
120             {\r
121                 b[j] = 0;\r
122                 i += 8;\r
123                 for (int k = 0; k < 8; ++k)\r
124                 {\r
125                     b[j] = (byte)(b[j] << 1 | (bits[--i].State ? 1 : 0));\r
126                 }\r
127                 i += 8;\r
128             }\r
129             return b;\r
130         }\r
131 \r
132         private void radio8B_CheckedChanged(object sender, EventArgs e)\r
133         {\r
134             if (radio8B.Checked)\r
135             {\r
136                 SetLength(8);\r
137                 if (radioFloat.Checked)\r
138                 {\r
139                     SetForFloatingPoint(true);\r
140                 }\r
141             }\r
142         }\r
143 \r
144         private void radio4B_CheckedChanged(object sender, EventArgs e)\r
145         {\r
146             if (radio4B.Checked)\r
147             {\r
148                 SetLength(4);\r
149                 if (radioFloat.Checked)\r
150                 {\r
151                     SetForFloatingPoint(false);\r
152                 }\r
153             }\r
154         }\r
155 \r
156         private void radio2B_CheckedChanged(object sender, EventArgs e)\r
157         {\r
158             if (radio2B.Checked)\r
159             {\r
160                 SetLength(2);\r
161             }\r
162         }\r
163 \r
164         private void radio1B_CheckedChanged(object sender, EventArgs e)\r
165         {\r
166             if (radio1B.Checked)\r
167             {\r
168                 SetLength(1);\r
169             }\r
170         }\r
171 \r
172         private void SetLength(int length)\r
173         {\r
174             this.length = length;\r
175             int b = length * 8;\r
176             for (int i = 8; i < b; ++i)\r
177             {\r
178                 bits[i].Enabled = true;\r
179             }\r
180             for (int i = b; i < 64; ++i)\r
181             {\r
182                 bits[i].Enabled = false;\r
183             }\r
184         }\r
185 \r
186         private void BitChanged(object sender, EventArgs e)\r
187         {\r
188             byte[] b = GetBytes();\r
189             if (radioUint.Checked)\r
190             {\r
191                 UpdateUint(b);\r
192             }\r
193             else if (radioInt.Checked)\r
194             {\r
195                 UpdateInt(b);\r
196             }\r
197             else if (radioFloat.Checked)\r
198             {\r
199                 UpdateFloat(b);\r
200             }\r
201             UpdateHex(b);\r
202         }\r
203         \r
204         private void UpdateUint(byte[] bytes)\r
205         {\r
206             ulong value = 0;\r
207             switch (length)\r
208             {\r
209                 case 1:\r
210                     value = bytes[0]; \r
211                     break;\r
212                 case 2:\r
213                     value = BitConverter.ToUInt16(bytes, 0);\r
214                     break;\r
215                 case 4:\r
216                     value = BitConverter.ToUInt32(bytes, 0);\r
217                     break;\r
218                 case 8:\r
219                     value = BitConverter.ToUInt64(bytes, 0);\r
220                     break;\r
221             }\r
222             textValue.Text = value.ToString();\r
223         }\r
224 \r
225         private void UpdateInt(byte[] bytes)\r
226         {\r
227             long value = 0;\r
228             switch (length)\r
229             {\r
230                 case 1:\r
231                     value = (sbyte)bytes[0];\r
232                     break;\r
233                 case 2:\r
234                     value = BitConverter.ToInt16(bytes, 0);\r
235                     break;\r
236                 case 4:\r
237                     value = BitConverter.ToInt32(bytes, 0);\r
238                     break;\r
239                 case 8:\r
240                     value = BitConverter.ToInt64(bytes, 0);\r
241                     break;\r
242             }\r
243             textValue.Text = value.ToString();\r
244         }\r
245 \r
246         private void UpdateFloat(byte[] bytes)\r
247         {\r
248             if (length == 4)\r
249             {\r
250                 float value = BitConverter.ToSingle(bytes, 0);\r
251                 textValue.Text = value.ToString("g8");\r
252             }\r
253             else\r
254             {\r
255                 double value = BitConverter.ToDouble(bytes, 0);\r
256                 textValue.Text = value.ToString("g17");\r
257             }\r
258         }\r
259 \r
260         private void UpdateHex(byte[] bytes)\r
261         {\r
262             textHex.Text = "";\r
263             for (int i = length - 1; i >= 0; --i)\r
264             {\r
265                  textHex.Text += bytes[i].ToString("X2");\r
266             }\r
267         }\r
268 \r
269     }\r
270 }\r