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
172 UpdateValue(GetBytes());
\r
177 private void radio4B_CheckedChanged(object sender, EventArgs e)
\r
179 if (radio4B.Checked)
\r
182 if (radioFloat.Checked)
\r
184 SetForFloatingPoint(false);
\r
185 UpdateValue(GetBytes());
\r
190 private void radio2B_CheckedChanged(object sender, EventArgs e)
\r
192 if (radio2B.Checked)
\r
198 private void radio1B_CheckedChanged(object sender, EventArgs e)
\r
200 if (radio1B.Checked)
\r
203 UpdateValue(GetBytes());
\r
207 private void SetLength(int length)
\r
209 this.length = length;
\r
210 int b = length * 8;
\r
211 for (int i = 8; i < b; ++i)
\r
213 bits[i].Enabled = true;
\r
215 for (int i = b; i < 64; ++i)
\r
217 bits[i].Enabled = false;
\r
219 if (radioFloat.Checked)
\r
221 textValue_TextChanged(null, null);
\r
225 BitChanged(null, null);
\r
229 private void BitChanged(object sender, EventArgs e)
\r
231 byte[] b = GetBytes();
\r
236 private void UpdateValue(byte[] bytes)
\r
238 if (radioUint.Checked)
\r
240 textValue.Text = ToUInt64(bytes).ToString();
\r
242 else if (radioInt.Checked)
\r
244 textValue.Text = ToInt64(bytes).ToString();
\r
246 else if (radioFloat.Checked)
\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
261 private void UpdateHex(byte[] bytes)
\r
264 for (int i = length - 1; i >= 0; --i)
\r
266 textHex.Text += bytes[i].ToString("X2");
\r
270 private void UpdateBits(byte[] bytes)
\r
273 for (int j = 0; j < length; ++j)
\r
276 for (int k = 0; k < 8; ++k)
\r
278 bits[i++].SetStateWithoutEvent((bytes[j] & mask) != 0);
\r
284 private void UpdateAll(byte[] bytes)
\r
287 UpdateValue(bytes);
\r
291 private void textHex_TextChanged(object sender, EventArgs e)
\r
293 if (!textHex.Focused)
\r
298 UInt64.TryParse(textHex.Text,
\r
299 System.Globalization.NumberStyles.HexNumber, null, out value);
\r
300 byte[] b = BitConverter.GetBytes(value);
\r
305 private void textValue_TextChanged(object sender, EventArgs e)
\r
307 string text = textValue.Text;
\r
308 if (!textValue.Focused && sender != null)
\r
313 if (radioUint.Checked)
\r
316 UInt64.TryParse(text, out value);
\r
317 b = BitConverter.GetBytes(value);
\r
319 else if (radioInt.Checked)
\r
322 Int64.TryParse(text, out value);
\r
323 b = BitConverter.GetBytes(value);
\r
325 else if (radioFloat.Checked && length == 4)
\r
328 Single.TryParse(text, out value);
\r
329 b = BitConverter.GetBytes(value);
\r
331 else if (radioFloat.Checked && length == 8)
\r
334 Double.TryParse(text, out value);
\r
335 b = BitConverter.GetBytes(value);
\r
339 throw new Exception();
\r
345 private void buttonInc_Click(object sender, EventArgs e)
\r
347 if (radioFloat.Checked)
\r
351 int value = BitConverter.ToInt32(GetBytes(), 0);
\r
352 if (value == -0x80000000)
\r
358 value += value >= 0 ? 1 : -1;
\r
360 UpdateAll(BitConverter.GetBytes(value));
\r
364 long value = BitConverter.ToInt64(GetBytes(), 0);
\r
365 if (value == (1L << 63))
\r
371 value += value >= 0 ? 1 : -1;
\r
373 UpdateAll(BitConverter.GetBytes(value));
\r
378 ulong value = BitConverter.ToUInt64(GetBytes(), 0);
\r
379 UpdateAll(BitConverter.GetBytes(++value));
\r
383 private void buttonDec_Click(object sender, EventArgs e)
\r
385 if (radioFloat.Checked)
\r
389 int value = BitConverter.ToInt32(GetBytes(), 0);
\r
396 value -= value >= 0 ? 1 : -1;
\r
398 UpdateAll(BitConverter.GetBytes(value));
\r
402 long value = BitConverter.ToInt64(GetBytes(), 0);
\r
409 value -= value >= 0 ? 1 : -1;
\r
411 UpdateAll(BitConverter.GetBytes(value));
\r
416 ulong value = BitConverter.ToUInt64(GetBytes(), 0);
\r
417 UpdateAll(BitConverter.GetBytes(--value));
\r
421 private void buttonClr_Click(object sender, EventArgs e)
\r
423 UpdateAll(BitConverter.GetBytes((ulong)0));
\r
426 private void buttonNot_Click(object sender, EventArgs e)
\r
428 ulong value = BitConverter.ToUInt64(GetBytes(), 0);
\r
429 UpdateAll(BitConverter.GetBytes(~value));
\r
432 private void buttonLsh_Click(object sender, EventArgs e)
\r
434 ulong value = BitConverter.ToUInt64(GetBytes(), 0);
\r
435 UpdateAll(BitConverter.GetBytes(value << 1));
\r
438 private void buttonRsh_Click(object sender, EventArgs e)
\r
440 if (radioInt.Checked)
\r
442 long value = ToInt64(GetBytes());
\r
443 UpdateAll(BitConverter.GetBytes(value >> 1));
\r
447 ulong value = BitConverter.ToUInt64(GetBytes(), 0);
\r
448 UpdateAll(BitConverter.GetBytes(value >> 1));
\r
452 private void textValue_Leave(object sender, EventArgs e)
\r
454 UpdateValue(GetBytes());
\r
457 private void textHex_Leave(object sender, EventArgs e)
\r
459 UpdateHex(GetBytes());
\r
462 private void MainForm_KeyDown(object sender, KeyEventArgs e)
\r
477 e.SuppressKeyPress = true;
\r