49 |
|
|
50 |
private void chkieee754float_CheckedChanged(object sender, EventArgs e) |
private void chkieee754float_CheckedChanged(object sender, EventArgs e) |
51 |
{ |
{ |
52 |
// IEEE754 Hex Float (hex) |
//// IEEE754 Hex Float (hex) |
53 |
txtInput.InputType = NumericInputTypes.IEEE754HexFloat; |
//txtInput.InputType = NumericInputTypes.IEEE754HexFloat; |
54 |
txtInput.Value = (0).ToString(); |
//txtInput.Value = (0).ToString(); |
55 |
} |
} |
56 |
|
|
57 |
private void btnConvert_Click(object sender, EventArgs e) |
private void btnConvert_Click(object sender, EventArgs e) |
58 |
{ |
{ |
59 |
switch (txtInput.InputType) |
try |
60 |
{ |
{ |
61 |
case NumericInputTypes.Binary: |
switch (txtInput.InputType) |
62 |
txtBinary.Value = txtInput.Value; |
{ |
63 |
ConvertData(txtBinary.Value, 2); |
case NumericInputTypes.Binary: |
64 |
break; |
txtBinary.Value = txtInput.Value; |
65 |
case NumericInputTypes.Decimal: |
ConvertData(txtBinary.Value, 2); |
66 |
txtDecimal.Value = txtInput.Value; |
break; |
67 |
ConvertData(txtDecimal.Value, 10); |
case NumericInputTypes.Decimal: |
68 |
break; |
txtDecimal.Value = txtInput.Value; |
69 |
case NumericInputTypes.Hex: |
ConvertData(txtDecimal.Value, 10); |
70 |
txtHex.Value = txtInput.Value; |
break; |
71 |
ConvertData(txtHex.Value, 16); |
case NumericInputTypes.Hex: |
72 |
break; |
txtHex.Value = txtInput.Value; |
73 |
case NumericInputTypes.IEEE754HexFloat: |
ConvertData(txtHex.Value, 16); |
74 |
txtFloat.Value = txtInput.Value; |
break; |
75 |
ConvertData(txtFloat.Value, 16); |
//case NumericInputTypes.IEEE754HexFloat: |
76 |
break; |
// txtFloat.Value = txtInput.Value; |
77 |
case NumericInputTypes.Octal: |
// ConvertData(txtFloat.Value, 16); |
78 |
txtOctal.Value = (txtInput.Value.StartsWith("0")) ? txtInput.Value : string.Format("0{0}",txtInput.Value); |
// break; |
79 |
ConvertData(txtOctal.Value, 8); |
case NumericInputTypes.Octal: |
80 |
break; |
txtOctal.Value = (txtInput.Value.StartsWith("0")) ? txtInput.Value : string.Format("0{0}", txtInput.Value); |
81 |
|
ConvertData(txtOctal.Value, 8); |
82 |
|
break; |
83 |
|
} |
84 |
|
} |
85 |
|
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 |
} |
} |
94 |
|
|
97 |
// binary |
// binary |
98 |
if (txtInput.InputType != NumericInputTypes.Binary) |
if (txtInput.InputType != NumericInputTypes.Binary) |
99 |
{ |
{ |
100 |
int val = Convert.ToInt32(value, FromBase); |
int val = Convert.ToInt32(value.Contains(".") ? value.Remove(value.IndexOf(".")) : value, FromBase); |
101 |
txtBinary.Value = Convert.ToString(val, 2); |
txtBinary.Value = Convert.ToString(val, 2); |
102 |
if (txtInput.InputType == NumericInputTypes.IEEE754HexFloat) |
//if (txtInput.InputType == NumericInputTypes.IEEE754HexFloat) |
103 |
{ |
//{ |
104 |
ieee754FloatingPointConverter converter = new ieee754FloatingPointConverter(); |
// ieee754FloatingPointConverter converter = new ieee754FloatingPointConverter(); |
105 |
uint val2 = Convert.ToUInt32(value, FromBase); |
// uint val2 = Convert.ToUInt32(value.Contains(".") ? value.Remove(value.IndexOf(".")) : value, FromBase); |
106 |
converter.HexFloat = val2; |
// converter.HexFloat = val2; |
107 |
txtBinary.Value = Convert.ToString(converter.Float); |
// txtBinary.Value = Convert.ToString(converter.Float); |
108 |
} |
//} |
109 |
} |
} |
110 |
// decimal |
// decimal |
111 |
if (txtInput.InputType != NumericInputTypes.Decimal) |
if (txtInput.InputType != NumericInputTypes.Decimal) |
112 |
{ |
{ |
113 |
int val = Convert.ToInt32(value, FromBase); |
int val = Convert.ToInt32(value, FromBase); |
114 |
txtDecimal.Value = Convert.ToString(val, 10); |
txtDecimal.Value = Convert.ToString(val, 10); |
115 |
if (txtInput.InputType == NumericInputTypes.IEEE754HexFloat) |
//if (txtInput.InputType == NumericInputTypes.IEEE754HexFloat) |
116 |
{ |
//{ |
117 |
ieee754FloatingPointConverter converter = new ieee754FloatingPointConverter(); |
// ieee754FloatingPointConverter converter = new ieee754FloatingPointConverter(); |
118 |
uint val2 = Convert.ToUInt32(value, FromBase); |
// uint val2 = Convert.ToUInt32(value.Contains(".") ? value.Remove(value.IndexOf(".")) : value, FromBase); |
119 |
converter.HexFloat = val2; |
// converter.HexFloat = val2; |
120 |
txtDecimal.Value = string.Format("{0:R}", converter.Float); |
// txtDecimal.Value = string.Format("{0:R}", converter.Float); |
121 |
} |
//} |
122 |
} |
} |
123 |
// hex |
// hex |
124 |
if (txtInput.InputType != NumericInputTypes.Hex) |
if (txtInput.InputType != NumericInputTypes.Hex) |
125 |
{ |
{ |
126 |
int val = Convert.ToInt32(value, FromBase); |
int val = Convert.ToInt32(value.Contains(".") ? value.Remove(value.IndexOf(".")) : value, FromBase); |
127 |
txtHex.Value = Convert.ToString(val, 16); |
txtHex.Value = Convert.ToString(val, 16); |
128 |
if (txtInput.InputType == NumericInputTypes.IEEE754HexFloat) |
//if (txtInput.InputType == NumericInputTypes.IEEE754HexFloat) |
129 |
{ |
//{ |
130 |
ieee754FloatingPointConverter converter = new ieee754FloatingPointConverter(); |
// ieee754FloatingPointConverter converter = new ieee754FloatingPointConverter(); |
131 |
uint val2 = Convert.ToUInt32(value, FromBase); |
// uint val2 = Convert.ToUInt32(value.Contains(".") ? value.Remove(value.IndexOf(".")) : value, FromBase); |
132 |
converter.HexFloat = val2; |
// converter.HexFloat = val2; |
133 |
} |
//} |
134 |
} |
} |
135 |
// octal |
// octal |
136 |
if (txtInput.InputType != NumericInputTypes.Octal) |
if (txtInput.InputType != NumericInputTypes.Octal) |
137 |
{ |
{ |
138 |
int val = Convert.ToInt32(value, FromBase); |
int val = Convert.ToInt32(value.Contains(".") ? value.Remove(value.IndexOf(".")) : value, FromBase); |
139 |
txtOctal.Value = Convert.ToString(val, 8); |
txtOctal.Value = Convert.ToString(val, 8); |
140 |
if (txtInput.InputType == NumericInputTypes.IEEE754HexFloat) |
//if (txtInput.InputType == NumericInputTypes.IEEE754HexFloat) |
141 |
{ |
//{ |
142 |
ieee754FloatingPointConverter converter = new ieee754FloatingPointConverter(); |
// ieee754FloatingPointConverter converter = new ieee754FloatingPointConverter(); |
143 |
uint val2 = Convert.ToUInt32(value, FromBase); |
// uint val2 = Convert.ToUInt32(value.Contains(".") ? value.Remove(value.IndexOf(".")) : value, FromBase); |
144 |
converter.HexFloat = val2; |
// converter.HexFloat = val2; |
145 |
} |
//} |
146 |
} |
} |
147 |
// float |
//// float |
148 |
if (txtInput.InputType != NumericInputTypes.IEEE754HexFloat) |
//if (txtInput.InputType != NumericInputTypes.IEEE754HexFloat) |
149 |
{ |
//{ |
150 |
ieee754FloatingPointConverter converter = new ieee754FloatingPointConverter(); |
// ieee754FloatingPointConverter converter = new ieee754FloatingPointConverter(); |
151 |
uint val = Convert.ToUInt32(value, FromBase); |
// uint val = Convert.ToUInt32(value.Contains(".") ? value.Remove(value.IndexOf(".")) : value, FromBase); |
152 |
float f = Convert.ToSingle(val); |
// float f = Convert.ToSingle(val); |
153 |
converter.Float = f; |
// converter.Float = f; |
154 |
//txtFloat.Value = string.Format("{0:R}", converter.Float); |
// //txtFloat.Value = string.Format("{0:R}", converter.Float); |
155 |
txtFloat.Value = string.Format("0x{0:x8}", converter.HexFloat); |
// txtFloat.Value = string.Format("0x{0:x8}", converter.HexFloat); |
156 |
} |
//} |
157 |
} |
} |
158 |
} |
} |
159 |
} |
} |