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
21 private BitSwitch[] bits;
\r
22 private int length = 4;
\r
28 InitializeComponent();
\r
30 bits = new BitSwitch[64];
\r
31 foreach (Control c in table.Controls)
\r
33 if (c is BitSwitches4)
\r
35 BitSwitches4 bs = c as BitSwitches4;
\r
36 int i = bs.StartBit;
\r
37 bits[i + 0] = bs.B0;
\r
38 bits[i + 1] = bs.B1;
\r
39 bits[i + 2] = bs.B2;
\r
40 bits[i + 3] = bs.B3;
\r
43 foreach (BitSwitch b in bits)
\r
45 b.StateChanged += BitChanged;
\r
47 Font mono = new Font(FontFamily.GenericMonospace, textHex.Font.Size);
\r
48 textHex.Font = mono;
\r
49 textValue.Font = mono;
\r
55 #region event handlers
\r
57 private void MainForm_KeyDown(object sender, KeyEventArgs e)
\r
72 e.SuppressKeyPress = true;
\r
76 private void radioUint_CheckedChanged(object sender, EventArgs e)
\r
78 if (radioUint.Checked)
\r
80 UpdateValue(GetBytes());
\r
84 private void radioInt_CheckedChanged(object sender, EventArgs e)
\r
86 if (radioInt.Checked)
\r
88 UpdateValue(GetBytes());
\r
92 private void radioFloat_CheckedChanged(object sender, EventArgs e)
\r
94 if (radioFloat.Checked)
\r
96 radio1B.Enabled = false;
\r
97 radio2B.Enabled = false;
\r
98 if (radio1B.Checked || radio2B.Checked)
\r
100 radio4B.Checked = true;
\r
102 SetForFloatingPoint(radio8B.Checked);
\r
103 UpdateValue(GetBytes());
\r
108 radio1B.Enabled = true;
\r
109 radio2B.Enabled = true;
\r
113 private void MainForm_Load(object sender, EventArgs e)
\r
115 radioUint.Checked = true;
\r
116 radio4B.Checked = true;
\r
117 BitChanged(null, null);
\r
120 private void radio8B_CheckedChanged(object sender, EventArgs e)
\r
122 if (radio8B.Checked)
\r
125 if (radioFloat.Checked)
\r
127 SetForFloatingPoint(true);
\r
128 UpdateValue(GetBytes());
\r
133 private void radio4B_CheckedChanged(object sender, EventArgs e)
\r
135 if (radio4B.Checked)
\r
138 if (radioFloat.Checked)
\r
140 SetForFloatingPoint(false);
\r
141 UpdateValue(GetBytes());
\r
146 private void radio2B_CheckedChanged(object sender, EventArgs e)
\r
148 if (radio2B.Checked)
\r
154 private void radio1B_CheckedChanged(object sender, EventArgs e)
\r
156 if (radio1B.Checked)
\r
159 UpdateValue(GetBytes());
\r
164 private void buttonInc_Click(object sender, EventArgs e)
\r
166 if (radioFloat.Checked)
\r
170 int value = BitConverter.ToInt32(GetBytes(), 0);
\r
171 if (value == -0x80000000)
\r
177 value += value >= 0 ? 1 : -1;
\r
179 UpdateAll(BitConverter.GetBytes(value));
\r
183 long value = BitConverter.ToInt64(GetBytes(), 0);
\r
184 if (value == (1L << 63))
\r
190 value += value >= 0 ? 1 : -1;
\r
192 UpdateAll(BitConverter.GetBytes(value));
\r
197 ulong value = BitConverter.ToUInt64(GetBytes(), 0);
\r
198 UpdateAll(BitConverter.GetBytes(++value));
\r
202 private void buttonDec_Click(object sender, EventArgs e)
\r
204 if (radioFloat.Checked)
\r
208 int value = BitConverter.ToInt32(GetBytes(), 0);
\r
215 value -= value >= 0 ? 1 : -1;
\r
217 UpdateAll(BitConverter.GetBytes(value));
\r
221 long value = BitConverter.ToInt64(GetBytes(), 0);
\r
228 value -= value >= 0 ? 1 : -1;
\r
230 UpdateAll(BitConverter.GetBytes(value));
\r
235 ulong value = BitConverter.ToUInt64(GetBytes(), 0);
\r
236 UpdateAll(BitConverter.GetBytes(--value));
\r
240 private void buttonClr_Click(object sender, EventArgs e)
\r
242 UpdateAll(BitConverter.GetBytes((ulong)0));
\r
245 private void buttonNot_Click(object sender, EventArgs e)
\r
247 ulong value = BitConverter.ToUInt64(GetBytes(), 0);
\r
248 UpdateAll(BitConverter.GetBytes(~value));
\r
251 private void buttonLsh_Click(object sender, EventArgs e)
\r
253 ulong value = BitConverter.ToUInt64(GetBytes(), 0);
\r
254 UpdateAll(BitConverter.GetBytes(value << 1));
\r
257 private void buttonRsh_Click(object sender, EventArgs e)
\r
259 if (radioInt.Checked)
\r
261 long value = ToInt64(GetBytes());
\r
262 UpdateAll(BitConverter.GetBytes(value >> 1));
\r
266 ulong value = BitConverter.ToUInt64(GetBytes(), 0);
\r
267 UpdateAll(BitConverter.GetBytes(value >> 1));
\r
271 private void textHex_TextChanged(object sender, EventArgs e)
\r
273 if (!textHex.Focused)
\r
278 UInt64.TryParse(textHex.Text,
\r
279 System.Globalization.NumberStyles.HexNumber, null, out value);
\r
280 byte[] b = BitConverter.GetBytes(value);
\r
285 private void textValue_TextChanged(object sender, EventArgs e)
\r
287 string text = textValue.Text;
\r
288 if (!textValue.Focused && sender != null)
\r
293 if (radioUint.Checked)
\r
296 UInt64.TryParse(text, out value);
\r
297 b = BitConverter.GetBytes(value);
\r
299 else if (radioInt.Checked)
\r
302 Int64.TryParse(text, out value);
\r
303 b = BitConverter.GetBytes(value);
\r
305 else if (radioFloat.Checked && length == 4)
\r
308 Single.TryParse(text, out value);
\r
309 b = BitConverter.GetBytes(value);
\r
311 else if (radioFloat.Checked && length == 8)
\r
314 Double.TryParse(text, out value);
\r
315 b = BitConverter.GetBytes(value);
\r
319 throw new Exception();
\r
325 private void textValue_Leave(object sender, EventArgs e)
\r
327 UpdateValue(GetBytes());
\r
330 private void textHex_Leave(object sender, EventArgs e)
\r
332 UpdateHex(GetBytes());
\r
335 private void BitChanged(object sender, EventArgs e)
\r
337 byte[] b = GetBytes();
\r
344 #region private methods
\r
346 private void SetForFloatingPoint(bool isDouble)
\r
348 for (int i = 23; i < 31; ++i)
\r
350 bits[i].IsExponential = !isDouble;
\r
352 for (int i = 52; i < 63; ++i)
\r
354 bits[i].IsExponential = isDouble;
\r
356 textValue.ValidatingType = typeof(Double);
\r
359 private void SetForInteger()
\r
361 for (int i = 0; i < 64; ++i)
\r
363 bits[i].IsExponential = false;
\r
365 textValue.ValidatingType = typeof(long);
\r
368 private byte[] GetBytes()
\r
370 byte[] b = new Byte[8];
\r
372 for (int j = 0; j < 8; ++j)
\r
376 for (int k = 0; k < 8; ++k)
\r
378 b[j] = (byte)(b[j] << 1 | (bits[--i].State ? 1 : 0));
\r
385 private long ToInt64(byte[] bytes)
\r
390 return (sbyte)bytes[0];
\r
392 return BitConverter.ToInt16(bytes, 0);
\r
394 return BitConverter.ToInt32(bytes, 0);
\r
396 return BitConverter.ToInt64(bytes, 0);
\r
401 private ulong ToUInt64(byte[] bytes)
\r
408 return BitConverter.ToUInt16(bytes, 0);
\r
410 return BitConverter.ToUInt32(bytes, 0);
\r
412 return BitConverter.ToUInt64(bytes, 0);
\r
417 private void SetLength(int length)
\r
419 this.length = length;
\r
420 int b = length * 8;
\r
421 for (int i = 8; i < b; ++i)
\r
423 bits[i].Enabled = true;
\r
425 for (int i = b; i < 64; ++i)
\r
427 bits[i].Enabled = false;
\r
429 if (radioFloat.Checked)
\r
431 textValue_TextChanged(null, null);
\r
435 BitChanged(null, null);
\r
439 private void UpdateValue(byte[] bytes)
\r
441 if (radioUint.Checked)
\r
443 textValue.Text = ToUInt64(bytes).ToString();
\r
445 else if (radioInt.Checked)
\r
447 textValue.Text = ToInt64(bytes).ToString();
\r
449 else if (radioFloat.Checked)
\r
453 float value = BitConverter.ToSingle(bytes, 0);
\r
454 textValue.Text = value.ToString("g8");
\r
458 double value = BitConverter.ToDouble(bytes, 0);
\r
459 textValue.Text = value.ToString("g17");
\r
464 private void UpdateHex(byte[] bytes)
\r
467 for (int i = length - 1; i >= 0; --i)
\r
469 textHex.Text += bytes[i].ToString("X2");
\r
473 private void UpdateBits(byte[] bytes)
\r
476 for (int j = 0; j < length; ++j)
\r
479 for (int k = 0; k < 8; ++k)
\r
481 bits[i++].SetStateWithoutEvent((bytes[j] & mask) != 0);
\r
487 private void UpdateAll(byte[] bytes)
\r
490 UpdateValue(bytes);
\r