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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 226 - (hide annotations) (download)
Sat Jun 2 14:54:17 2012 UTC (8 years, 10 months ago) by william
File size: 7049 byte(s)
+ commit final wip for numeric datatype converter

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     break;
71     case NumericInputTypes.Hex:
72     txtHex.Value = txtInput.Value;
73     ConvertData(txtHex.Value, 16);
74     break;
75     //case NumericInputTypes.IEEE754HexFloat:
76     // txtFloat.Value = txtInput.Value;
77     // ConvertData(txtFloat.Value, 16);
78     // break;
79     case NumericInputTypes.Octal:
80     txtOctal.Value = (txtInput.Value.StartsWith("0")) ? txtInput.Value : string.Format("0{0}", txtInput.Value);
81     ConvertData(txtOctal.Value, 8);
82     break;
83     }
84 william 225 }
85 william 226 catch (FormatException ex)
86     {
87     MessageBox.Show(ex.ToString(),"Encountered an error in the input format", MessageBoxButtons.OK, MessageBoxIcon.Error);
88     }
89     catch (Exception ex)
90     {
91     MessageBox.Show(ex.ToString(),"Encountered an error while converting the data", MessageBoxButtons.OK, MessageBoxIcon.Error);
92     }
93 william 225 }
94    
95     private void ConvertData(string value, int FromBase)
96     {
97     // binary
98     if (txtInput.InputType != NumericInputTypes.Binary)
99     {
100 william 226 int val = Convert.ToInt32(value.Contains(".") ? value.Remove(value.IndexOf(".")) : value, FromBase);
101 william 225 txtBinary.Value = Convert.ToString(val, 2);
102 william 226 //if (txtInput.InputType == NumericInputTypes.IEEE754HexFloat)
103     //{
104     // ieee754FloatingPointConverter converter = new ieee754FloatingPointConverter();
105     // uint val2 = Convert.ToUInt32(value.Contains(".") ? value.Remove(value.IndexOf(".")) : value, FromBase);
106     // converter.HexFloat = val2;
107     // txtBinary.Value = Convert.ToString(converter.Float);
108     //}
109 william 225 }
110     // decimal
111     if (txtInput.InputType != NumericInputTypes.Decimal)
112     {
113     int val = Convert.ToInt32(value, FromBase);
114     txtDecimal.Value = Convert.ToString(val, 10);
115 william 226 //if (txtInput.InputType == NumericInputTypes.IEEE754HexFloat)
116     //{
117     // ieee754FloatingPointConverter converter = new ieee754FloatingPointConverter();
118     // uint val2 = Convert.ToUInt32(value.Contains(".") ? value.Remove(value.IndexOf(".")) : value, FromBase);
119     // converter.HexFloat = val2;
120     // txtDecimal.Value = string.Format("{0:R}", converter.Float);
121     //}
122 william 225 }
123     // hex
124     if (txtInput.InputType != NumericInputTypes.Hex)
125     {
126 william 226 int val = Convert.ToInt32(value.Contains(".") ? value.Remove(value.IndexOf(".")) : value, FromBase);
127 william 225 txtHex.Value = Convert.ToString(val, 16);
128 william 226 //if (txtInput.InputType == NumericInputTypes.IEEE754HexFloat)
129     //{
130     // ieee754FloatingPointConverter converter = new ieee754FloatingPointConverter();
131     // uint val2 = Convert.ToUInt32(value.Contains(".") ? value.Remove(value.IndexOf(".")) : value, FromBase);
132     // converter.HexFloat = val2;
133     //}
134 william 225 }
135     // octal
136     if (txtInput.InputType != NumericInputTypes.Octal)
137     {
138 william 226 int val = Convert.ToInt32(value.Contains(".") ? value.Remove(value.IndexOf(".")) : value, FromBase);
139 william 225 txtOctal.Value = Convert.ToString(val, 8);
140 william 226 //if (txtInput.InputType == NumericInputTypes.IEEE754HexFloat)
141     //{
142     // ieee754FloatingPointConverter converter = new ieee754FloatingPointConverter();
143     // uint val2 = Convert.ToUInt32(value.Contains(".") ? value.Remove(value.IndexOf(".")) : value, FromBase);
144     // converter.HexFloat = val2;
145     //}
146 william 225 }
147 william 226 //// float
148     //if (txtInput.InputType != NumericInputTypes.IEEE754HexFloat)
149     //{
150     // ieee754FloatingPointConverter converter = new ieee754FloatingPointConverter();
151     // uint val = Convert.ToUInt32(value.Contains(".") ? value.Remove(value.IndexOf(".")) : value, FromBase);
152     // float f = Convert.ToSingle(val);
153     // converter.Float = f;
154     // //txtFloat.Value = string.Format("{0:R}", converter.Float);
155     // txtFloat.Value = string.Format("0x{0:x8}", converter.HexFloat);
156     //}
157 william 225 }
158 william 218 }
159     }

  ViewVC Help
Powered by ViewVC 1.1.22