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 table.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
51 private void radioUint_CheckedChanged(object sender, EventArgs e)
\r
53 if (radioUint.Checked)
\r
55 UpdateValue(GetBytes());
\r
59 private void radioInt_CheckedChanged(object sender, EventArgs e)
\r
61 if (radioInt.Checked)
\r
63 UpdateValue(GetBytes());
\r
67 private void radioFloat_CheckedChanged(object sender, EventArgs e)
\r
69 if (radioFloat.Checked)
\r
71 radio1B.Enabled = false;
\r
72 radio2B.Enabled = false;
\r
73 if (radio1B.Checked || radio2B.Checked)
\r
75 radio4B.Checked = true;
\r
77 SetForFloatingPoint(radio8B.Checked);
\r
78 UpdateValue(GetBytes());
\r
83 radio1B.Enabled = true;
\r
84 radio2B.Enabled = true;
\r
88 private void MainForm_Load(object sender, EventArgs e)
\r
90 radioUint.Checked = true;
\r
91 radio4B.Checked = true;
\r
92 BitChanged(null, null);
\r
95 private void SetForFloatingPoint(bool isDouble)
\r
97 for (int i = 23; i < 31; ++i)
\r
99 bits[i].IsExponential = !isDouble;
\r
101 for (int i = 52; i < 63; ++i)
\r
103 bits[i].IsExponential = isDouble;
\r
107 private void SetForInteger()
\r
109 for (int i = 0; i < 64; ++i)
\r
111 bits[i].IsExponential = false;
\r
115 private byte[] GetBytes()
\r
117 byte[] b = new Byte[8];
\r
119 for (int j = 0; j < 8; ++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 long ToInt64(byte[] bytes)
\r
137 return (sbyte)bytes[0];
\r
139 return BitConverter.ToInt16(bytes, 0);
\r
141 return BitConverter.ToInt32(bytes, 0);
\r
143 return BitConverter.ToInt64(bytes, 0);
\r
148 private ulong ToUInt64(byte[] bytes)
\r
155 return BitConverter.ToUInt16(bytes, 0);
\r
157 return BitConverter.ToUInt32(bytes, 0);
\r
159 return BitConverter.ToUInt64(bytes, 0);
\r
164 private void radio8B_CheckedChanged(object sender, EventArgs e)
\r
166 if (radio8B.Checked)
\r
169 if (radioFloat.Checked)
\r
171 SetForFloatingPoint(true);
\r
176 private void radio4B_CheckedChanged(object sender, EventArgs e)
\r
178 if (radio4B.Checked)
\r
181 if (radioFloat.Checked)
\r
183 SetForFloatingPoint(false);
\r
188 private void radio2B_CheckedChanged(object sender, EventArgs e)
\r
190 if (radio2B.Checked)
\r
196 private void radio1B_CheckedChanged(object sender, EventArgs e)
\r
198 if (radio1B.Checked)
\r
201 UpdateValue(GetBytes());
\r
205 private void SetLength(int length)
\r
207 this.length = length;
\r
208 int b = length * 8;
\r
209 for (int i = 8; i < b; ++i)
\r
211 bits[i].Enabled = true;
\r
213 for (int i = b; i < 64; ++i)
\r
215 bits[i].Enabled = false;
\r
217 if (radioFloat.Checked)
\r
219 textValue_TextChanged(null, null);
\r
223 BitChanged(null, null);
\r
227 private void BitChanged(object sender, EventArgs e)
\r
229 byte[] b = GetBytes();
\r
234 private void UpdateValue(byte[] bytes)
\r
236 if (radioUint.Checked)
\r
238 textValue.Text = ToUInt64(bytes).ToString();
\r
240 else if (radioInt.Checked)
\r
242 textValue.Text = ToInt64(bytes).ToString();
\r
244 else if (radioFloat.Checked)
\r
248 float value = BitConverter.ToSingle(bytes, 0);
\r
249 textValue.Text = value.ToString("g8");
\r
253 double value = BitConverter.ToDouble(bytes, 0);
\r
254 textValue.Text = value.ToString("g17");
\r
259 private void UpdateHex(byte[] bytes)
\r
262 for (int i = length - 1; i >= 0; --i)
\r
264 textHex.Text += bytes[i].ToString("X2");
\r
268 private void UpdateBits(byte[] bytes)
\r
271 for (int j = 0; j < length; ++j)
\r
274 for (int k = 0; k < 8; ++k)
\r
276 bits[i++].SetStateWithoutEvent((bytes[j] & mask) != 0);
\r
282 private void UpdateAll(byte[] bytes)
\r
285 UpdateValue(bytes);
\r
289 private void textHex_TextChanged(object sender, EventArgs e)
\r
291 if (!textHex.Focused)
\r
296 UInt64.TryParse(textHex.Text,
\r
297 System.Globalization.NumberStyles.HexNumber, null, out value);
\r
298 byte[] b = BitConverter.GetBytes(value);
\r
303 private void textValue_TextChanged(object sender, EventArgs e)
\r
305 string text = textValue.Text;
\r
306 if (!textValue.Focused && sender != null)
\r
311 if (radioUint.Checked)
\r
314 UInt64.TryParse(text, out value);
\r
315 b = BitConverter.GetBytes(value);
\r
317 else if (radioInt.Checked)
\r
320 Int64.TryParse(text, out value);
\r
321 b = BitConverter.GetBytes(value);
\r
323 else if (radioFloat.Checked && length == 4)
\r
326 Single.TryParse(text, out value);
\r
327 b = BitConverter.GetBytes(value);
\r
329 else if (radioFloat.Checked && length == 8)
\r
332 Double.TryParse(text, out value);
\r
333 b = BitConverter.GetBytes(value);
\r
337 throw new Exception();
\r
343 private void buttonInc_Click(object sender, EventArgs e)
\r
345 if (radioFloat.Checked)
\r
351 ulong value = BitConverter.ToUInt64(GetBytes(), 0);
\r
352 UpdateAll(BitConverter.GetBytes(++value));
\r
355 private void buttonDec_Click(object sender, EventArgs e)
\r
357 if (radioFloat.Checked)
\r
363 ulong value = BitConverter.ToUInt64(GetBytes(), 0);
\r
364 UpdateAll(BitConverter.GetBytes(--value));
\r
368 private void buttonClr_Click(object sender, EventArgs e)
\r
370 UpdateAll(BitConverter.GetBytes((ulong)0));
\r
373 private void buttonNot_Click(object sender, EventArgs e)
\r
375 ulong value = BitConverter.ToUInt64(GetBytes(), 0);
\r
376 UpdateAll(BitConverter.GetBytes(~value));
\r
379 private void buttonLsh_Click(object sender, EventArgs e)
\r
381 ulong value = BitConverter.ToUInt64(GetBytes(), 0);
\r
382 UpdateAll(BitConverter.GetBytes(value << 1));
\r
385 private void buttonRsh_Click(object sender, EventArgs e)
\r
387 if (radioInt.Checked)
\r
389 long value = ToInt64(GetBytes());
\r
390 UpdateAll(BitConverter.GetBytes(value >> 1));
\r
394 ulong value = BitConverter.ToUInt64(GetBytes(), 0);
\r
395 UpdateAll(BitConverter.GetBytes(value >> 1));
\r