/[RomCheater]/trunk/RomCheater/Docking/FloatingDataTypeConverter.cs
ViewVC logotype

Annotation of /trunk/RomCheater/Docking/FloatingDataTypeConverter.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 368 - (hide annotations) (download)
Sun Jun 10 04:03:24 2012 UTC (9 years, 1 month ago) by william
File size: 7374 byte(s)

1 william 218 using System;
2     using System.Collections.Generic;
3     using System.ComponentModel;
4     using System.Data;
5     using System.Drawing;
6     using System.Linq;
7     using System.Text;
8     using System.Windows.Forms;
9     using WeifenLuo.WinFormsUI.Docking;
10 william 225 using libWin32.Win32.NumericConversion.IEEE754;
11 william 218
12     namespace RomCheater.Docking
13     {
14     public partial class FloatingDataTypeConverter : DockContent
15     {
16     public FloatingDataTypeConverter()
17     {
18     InitializeComponent();
19 william 223 chkhexadecimal.Checked = true;
20 william 218 }
21 william 222
22     private void chkBinary_CheckedChanged(object sender, EventArgs e)
23     {
24 william 223 // binary input
25 william 225 txtInput.InputType = NumericInputTypes.Binary;
26     txtInput.Value = (0).ToString();
27 william 223 }
28 william 222
29 william 223 private void chkdecimal_CheckedChanged(object sender, EventArgs e)
30     {
31     // decimal input
32 william 225 txtInput.InputType = NumericInputTypes.Decimal;
33     txtInput.Value = (0).ToString();
34 william 222 }
35 william 223
36     private void chkhexadecimal_CheckedChanged(object sender, EventArgs e)
37     {
38     // hex input
39 william 225 txtInput.InputType = NumericInputTypes.Hex;
40     txtInput.Value = (0).ToString();
41 william 223 }
42    
43     private void chkoctal_CheckedChanged(object sender, EventArgs e)
44     {
45     // octal input
46 william 225 txtInput.InputType = NumericInputTypes.Octal;
47     txtInput.Value = (0).ToString();
48 william 223 }
49    
50     private void chkieee754float_CheckedChanged(object sender, EventArgs e)
51     {
52 william 226 //// IEEE754 Hex Float (hex)
53     //txtInput.InputType = NumericInputTypes.IEEE754HexFloat;
54     //txtInput.Value = (0).ToString();
55 william 223 }
56 william 225
57     private void btnConvert_Click(object sender, EventArgs e)
58     {
59 william 226 try
60 william 225 {
61 william 226 switch (txtInput.InputType)
62     {
63     case NumericInputTypes.Binary:
64     txtBinary.Value = txtInput.Value;
65     ConvertData(txtBinary.Value, 2);
66     break;
67     case NumericInputTypes.Decimal:
68     txtDecimal.Value = txtInput.Value;
69     ConvertData(txtDecimal.Value, 10);
70 william 368 FloatConverter.SetFloatValue(Convert.ToSingle(txtDecimal.Value));
71     FloatConverter.CalculateHexFloat();
72 william 226 break;
73     case NumericInputTypes.Hex:
74     txtHex.Value = txtInput.Value;
75     ConvertData(txtHex.Value, 16);
76 william 368 FloatConverter.SetFloatHexValue(Convert.ToUInt32(txtHex.Value,16));
77     FloatConverter.CalculateFloat();
78 william 226 break;
79     //case NumericInputTypes.IEEE754HexFloat:
80     // txtFloat.Value = txtInput.Value;
81     // ConvertData(txtFloat.Value, 16);
82     // break;
83     case NumericInputTypes.Octal:
84     txtOctal.Value = (txtInput.Value.StartsWith("0")) ? txtInput.Value : string.Format("0{0}", txtInput.Value);
85     ConvertData(txtOctal.Value, 8);
86     break;
87     }
88 william 368
89 william 225 }
90 william 226 catch (FormatException ex)
91     {
92     MessageBox.Show(ex.ToString(),"Encountered an error in the input format", MessageBoxButtons.OK, MessageBoxIcon.Error);
93     }
94     catch (Exception ex)
95     {
96     MessageBox.Show(ex.ToString(),"Encountered an error while converting the data", MessageBoxButtons.OK, MessageBoxIcon.Error);
97     }
98 william 225 }
99    
100     private void ConvertData(string value, int FromBase)
101     {
102     // binary
103     if (txtInput.InputType != NumericInputTypes.Binary)
104     {
105 william 226 int val = Convert.ToInt32(value.Contains(".") ? value.Remove(value.IndexOf(".")) : value, FromBase);
106 william 225 txtBinary.Value = Convert.ToString(val, 2);
107 william 226 //if (txtInput.InputType == NumericInputTypes.IEEE754HexFloat)
108     //{
109     // ieee754FloatingPointConverter converter = new ieee754FloatingPointConverter();
110     // uint val2 = Convert.ToUInt32(value.Contains(".") ? value.Remove(value.IndexOf(".")) : value, FromBase);
111     // converter.HexFloat = val2;
112     // txtBinary.Value = Convert.ToString(converter.Float);
113     //}
114 william 225 }
115     // decimal
116     if (txtInput.InputType != NumericInputTypes.Decimal)
117     {
118     int val = Convert.ToInt32(value, FromBase);
119     txtDecimal.Value = Convert.ToString(val, 10);
120 william 226 //if (txtInput.InputType == NumericInputTypes.IEEE754HexFloat)
121     //{
122     // ieee754FloatingPointConverter converter = new ieee754FloatingPointConverter();
123     // uint val2 = Convert.ToUInt32(value.Contains(".") ? value.Remove(value.IndexOf(".")) : value, FromBase);
124     // converter.HexFloat = val2;
125     // txtDecimal.Value = string.Format("{0:R}", converter.Float);
126     //}
127 william 225 }
128     // hex
129     if (txtInput.InputType != NumericInputTypes.Hex)
130     {
131 william 226 int val = Convert.ToInt32(value.Contains(".") ? value.Remove(value.IndexOf(".")) : value, FromBase);
132 william 225 txtHex.Value = Convert.ToString(val, 16);
133 william 226 //if (txtInput.InputType == NumericInputTypes.IEEE754HexFloat)
134     //{
135     // ieee754FloatingPointConverter converter = new ieee754FloatingPointConverter();
136     // uint val2 = Convert.ToUInt32(value.Contains(".") ? value.Remove(value.IndexOf(".")) : value, FromBase);
137     // converter.HexFloat = val2;
138     //}
139 william 225 }
140     // octal
141     if (txtInput.InputType != NumericInputTypes.Octal)
142     {
143 william 226 int val = Convert.ToInt32(value.Contains(".") ? value.Remove(value.IndexOf(".")) : value, FromBase);
144 william 225 txtOctal.Value = Convert.ToString(val, 8);
145 william 226 //if (txtInput.InputType == NumericInputTypes.IEEE754HexFloat)
146     //{
147     // ieee754FloatingPointConverter converter = new ieee754FloatingPointConverter();
148     // uint val2 = Convert.ToUInt32(value.Contains(".") ? value.Remove(value.IndexOf(".")) : value, FromBase);
149     // converter.HexFloat = val2;
150     //}
151 william 225 }
152 william 226 //// float
153     //if (txtInput.InputType != NumericInputTypes.IEEE754HexFloat)
154     //{
155     // ieee754FloatingPointConverter converter = new ieee754FloatingPointConverter();
156     // uint val = Convert.ToUInt32(value.Contains(".") ? value.Remove(value.IndexOf(".")) : value, FromBase);
157     // float f = Convert.ToSingle(val);
158     // converter.Float = f;
159     // //txtFloat.Value = string.Format("{0:R}", converter.Float);
160     // txtFloat.Value = string.Format("0x{0:x8}", converter.HexFloat);
161     //}
162 william 225 }
163 william 218 }
164     }

  ViewVC Help
Powered by ViewVC 1.1.22