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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 838 - (hide annotations) (download)
Tue Sep 16 00:57:18 2014 UTC (6 years, 10 months ago) by william
File size: 7442 byte(s)
+ add an icon resource

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

  ViewVC Help
Powered by ViewVC 1.1.22