1 |
using System; |
2 |
using System.Collections.Generic; |
3 |
using System.ComponentModel; |
4 |
using System.Drawing; |
5 |
using System.Data; |
6 |
using System.Linq; |
7 |
using System.Text; |
8 |
using System.Windows.Forms; |
9 |
using libWin32.Win32.NumericConversion.IEEE754; |
10 |
|
11 |
namespace System.Windows.Forms |
12 |
{ |
13 |
public partial class IEEE754FloatConverter : UserControl |
14 |
{ |
15 |
public IEEE754FloatConverter() |
16 |
{ |
17 |
InitializeComponent(); |
18 |
txtIEEE754Float.ReadOnly = false; |
19 |
txtFloat.ReadOnly = false; |
20 |
btnConvertToFloat.PerformClick(); |
21 |
} |
22 |
|
23 |
private void btnConvertToFloat_Click(object sender, EventArgs e) |
24 |
{ |
25 |
CalculateFloat(); |
26 |
} |
27 |
|
28 |
private void btnConvertToIEEE754Float_Click(object sender, EventArgs e) |
29 |
{ |
30 |
CalculateHexFloat(); |
31 |
} |
32 |
|
33 |
public void SetFloatHexValue(uint value) |
34 |
{ |
35 |
txtIEEE754Float.Value = value; |
36 |
} |
37 |
public uint GetFloatHexValue() |
38 |
{ |
39 |
return (uint)txtIEEE754Float.Value; |
40 |
} |
41 |
public void SetFloatValue(float value) |
42 |
{ |
43 |
txtFloat.Value = value.ToString(); |
44 |
} |
45 |
public float GetFloatValue() |
46 |
{ |
47 |
return Convert.ToSingle(txtFloat.Value); |
48 |
} |
49 |
public void CalculateFloat() |
50 |
{ |
51 |
ieee754FloatingPointConverter converter = new ieee754FloatingPointConverter(); |
52 |
converter.HexFloat = txtIEEE754Float.ToUInt32(); |
53 |
txtFloat.Text = string.Format("{0:R}", converter.Float); |
54 |
} |
55 |
public void CalculateHexFloat() |
56 |
{ |
57 |
ieee754FloatingPointConverter converter = new ieee754FloatingPointConverter(); |
58 |
float f; |
59 |
if (!float.TryParse(txtFloat.Text, out f)) |
60 |
{ |
61 |
MessageBox.Show("Failed to Convert: " + txtFloat.Text + " to a float.", "Float Conversion Error", MessageBoxButtons.OK, MessageBoxIcon.Error); |
62 |
return; |
63 |
} |
64 |
converter.Float = f; |
65 |
txtIEEE754Float.Value = converter.HexFloat; |
66 |
} |
67 |
} |
68 |
} |