4 * This software is distributed under a BSD-style license.
\r
5 * See license.txt for more information.
\r
9 using System.Collections.Generic;
\r
10 using System.ComponentModel;
\r
12 using System.Drawing;
\r
14 using System.Windows.Forms;
\r
18 public partial class MainForm : Form
\r
20 private BitSwitch[] bits;
\r
21 private int length = 4;
\r
25 InitializeComponent();
\r
27 bits = new BitSwitch[64];
\r
28 foreach (Control c in Controls)
\r
30 if (c is BitSwitches4)
\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
40 foreach (BitSwitch b in bits)
\r
42 b.StateChanged += BitChanged;
\r
44 Font mono = new Font(FontFamily.GenericMonospace, textHex.Font.Size);
\r
45 textHex.Font = mono;
\r
46 textValue.Font = mono;
\r
49 private void radioUint_CheckedChanged(object sender, EventArgs e)
\r
51 if (radioUint.Checked)
\r
53 UpdateUint(GetBytes());
\r
57 private void radioInt_CheckedChanged(object sender, EventArgs e)
\r
59 if (radioInt.Checked)
\r
61 UpdateInt(GetBytes());
\r
65 private void radioFloat_CheckedChanged(object sender, EventArgs e)
\r
67 if (radioFloat.Checked)
\r
69 radio1B.Enabled = false;
\r
70 radio2B.Enabled = false;
\r
71 if (radio1B.Checked || radio2B.Checked)
\r
73 radio4B.Checked = true;
\r
75 SetForFloatingPoint(radio8B.Checked);
\r
76 UpdateFloat(GetBytes());
\r
81 radio1B.Enabled = true;
\r
82 radio2B.Enabled = true;
\r
86 private void MainForm_Load(object sender, EventArgs e)
\r
88 radioUint.Checked = true;
\r
89 radio4B.Checked = true;
\r
92 private void SetForFloatingPoint(bool isDouble)
\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
98 bits[i].BackColor = c1;
\r
100 for (int i = 52; i < 63; ++i)
\r
102 bits[i].BackColor = c2;
\r
106 private void SetForInteger()
\r
108 for (int i = 0; i < 64; ++i)
\r
110 bits[i].BackColor = Color.White;
\r
115 private byte[] GetBytes()
\r
117 byte[] b = new Byte[length];
\r
119 for (int j = 0; j < length; ++j)
\r
123 for (int k = 0; k < 8; ++k)
\r
125 b[j] = (byte)(b[j] << 1 | (bits[--i].State ? 1 : 0));
\r
132 private void radio8B_CheckedChanged(object sender, EventArgs e)
\r
134 if (radio8B.Checked)
\r
137 if (radioFloat.Checked)
\r
139 SetForFloatingPoint(true);
\r
144 private void radio4B_CheckedChanged(object sender, EventArgs e)
\r
146 if (radio4B.Checked)
\r
149 if (radioFloat.Checked)
\r
151 SetForFloatingPoint(false);
\r
156 private void radio2B_CheckedChanged(object sender, EventArgs e)
\r
158 if (radio2B.Checked)
\r
164 private void radio1B_CheckedChanged(object sender, EventArgs e)
\r
166 if (radio1B.Checked)
\r
172 private void SetLength(int length)
\r
174 this.length = length;
\r
175 int b = length * 8;
\r
176 for (int i = 8; i < b; ++i)
\r
178 bits[i].Enabled = true;
\r
180 for (int i = b; i < 64; ++i)
\r
182 bits[i].Enabled = false;
\r
186 private void BitChanged(object sender, EventArgs e)
\r
188 byte[] b = GetBytes();
\r
189 if (radioUint.Checked)
\r
193 else if (radioInt.Checked)
\r
197 else if (radioFloat.Checked)
\r
204 private void UpdateUint(byte[] bytes)
\r
213 value = BitConverter.ToUInt16(bytes, 0);
\r
216 value = BitConverter.ToUInt32(bytes, 0);
\r
219 value = BitConverter.ToUInt64(bytes, 0);
\r
222 textValue.Text = value.ToString();
\r
225 private void UpdateInt(byte[] bytes)
\r
231 value = (sbyte)bytes[0];
\r
234 value = BitConverter.ToInt16(bytes, 0);
\r
237 value = BitConverter.ToInt32(bytes, 0);
\r
240 value = BitConverter.ToInt64(bytes, 0);
\r
243 textValue.Text = value.ToString();
\r
246 private void UpdateFloat(byte[] bytes)
\r
250 float value = BitConverter.ToSingle(bytes, 0);
\r
251 textValue.Text = value.ToString("g8");
\r
255 double value = BitConverter.ToDouble(bytes, 0);
\r
256 textValue.Text = value.ToString("g17");
\r
260 private void UpdateHex(byte[] bytes)
\r
263 for (int i = length - 1; i >= 0; --i)
\r
265 textHex.Text += bytes[i].ToString("X2");
\r