Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
private void Form1_Load(object sender, EventArgs e)
           {
           // Initial Value
           float num1 = 36;

           // Convert to IEEE 754
           uint num2 = BitConverter.ToUInt32(BitConverter.GetBytes(num1),0);
           Console.WriteLine("{0} : {1}", num1, num2);

           // Hex representation
           byte[] byteArray = BitConverter.GetBytes(num1);
           //Array.Reverse(byteArray);
           Console.WriteLine(BitConverter.ToString(byteArray).Replace("-", " "));
           double doubleValue = BitConverter.Int64BitsToDouble(num2);
           Console.WriteLine(num2);
           // Convert back to float
            float num3 = BitConverter.ToSingle(BitConverter.GetBytes(num2), 0);
           Console.WriteLine(num3);


           }


What I have tried:

I found this and yes that works. But I wanted to know how I can do a simplified function can anyone help?

<pre>using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Text.RegularExpressions;



namespace WindowsFormsApplication18
    {
    public partial class Form1 : Form
        {
        private ByteConverter _converter;
        public Form1()

            {
            InitializeComponent();
            }
        
        private void convertU30()
            {
            if (int.Parse(textBox1.Text) < 1)
            return;
            string bin = (Convert.ToString(int.Parse(textBox1.Text), 2));
            string swappedbin = "";
            Console.WriteLine(bin);
            while (bin.Length > 0)
                {
                if (bin.Length > 7)
                    {
                    swappedbin += "1" + bin.Substring(bin.Length - 7);
                    bin = bin.Substring(0, bin.Length - 7);
                    }
                else
                    {
                    while (bin.Length < 8)
                        bin = "0" + bin;
                    swappedbin += bin;
                    bin = "";
                    }
                }
           

            textBox5.Text = Convert.ToInt32(swappedbin, 2).ToString("X");
            }
        private void Form1_Load(object sender, EventArgs e)
            {
            _converter = new ByteConverter();          
            textBox5.DataBindings.Add("Text", _converter, "U30", true, DataSourceUpdateMode.OnPropertyChanged, 0, "X");           
            tb_doubleHex.DataBindings.Add("Text", _converter, "ieee754x64", true, DataSourceUpdateMode.OnPropertyChanged, 0, "X");            
            }
        


        private void button1_Click(object sender, EventArgs e)
            {
            
            if (Regex.IsMatch(textBox1.Text, @"^-*\d+$"))
            {               
                convertU30();               
            }
            else
            {
                textBox1.BackColor = Color.Red;
            }
          }
        internal class ByteConverter : INotifyPropertyChanged
            {
            private int _4byte;           
            private int _u30;           
            private double _ieee754x64;

            public event PropertyChangedEventHandler PropertyChanged;

            public string U30
                {
                get
                    {
                    return this._u30.ToString("X");
                    }
                set
                    {
                    this._u30 = this.HandleHexInput(value);
                    this._4byte = this.convertU30ToInt(this._u30);                                     
                    this._ieee754x64 = Convert.ToDouble(this._4byte);
                    this.OnPropertyChanged(nameof(U30));
                    }
                }

            public string ieee754x64
                {
                get
                    {
                    byte[] bytes = BitConverter.GetBytes(_ieee754x64);
                    return string.Format("{0:X2}{1:X2}{2:X2}{3:X2}{4:X2}{5:X2}{6:X2}{7:X2}", (object)bytes[7], (object)bytes[6], (object)bytes[5], (object)bytes[4], (object)bytes[3], (object)bytes[2], (object)bytes[1], (object)bytes[0]);
                    }
                set
                    {
                    _ieee754x64 = HandleIEEE754x64Input(value);
                    try
                        {                        
 
                        }
                    catch (Exception ex)
                        {
                        int num = (int)MessageBox.Show(ex.Message, "Critical Error");
                        }
                    }
                }


            private double HandleIEEE754x64Input(string val)
                {
                val = val.Replace(" ", string.Empty);
                if (!Regex.IsMatch(val, "^[\\da-fA-F]+$"))
                    return 0.0;
                try
                    {
                    return BitConverter.ToDouble(BitConverter.GetBytes(Convert.ToInt64(val, 16)), 0);
                    }
                catch
                    {
                    return 0.0;
                    }
                }

            private int HandleHexInput(string val)
                {
                if (!Regex.IsMatch(val, "^[\\da-fA-F]+$"))
                    return 0;
                try
                    {
                    return Convert.ToInt32(val, 16);
                    }
                catch
                    {
                    return 0;
                    }
                }

            private int convertU30ToInt(int val)
                {
                try
                    {
                    string str1 = Convert.ToString(val, 2);
                    string str2 = string.Empty;
                    while (str1.Length > 0)
                        {
                        if (str1.Length > 8)
                            {
                            str2 = str1.Substring(1, 7) + str2;
                            str1 = str1.Substring(8);
                            }
                        else
                            {
                            while (str1.Length < 8)
                                str1 = "0" + str1;
                            str2 = str1 + str2;
                            str1 = "";
                            }
                        }
                    return Convert.ToInt32(str2, 2);
                    }
                catch { }
                    {
                    return this._u30;
                    }
                }

            

            private void OnPropertyChanged(string PropertyName)
                {
                PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
                if (propertyChanged == null)
                    return;
                propertyChanged((object)this, new PropertyChangedEventArgs(PropertyName));
                }
            }
        }
    }
Hello everyone, I need your help. need to convert 4 bytes to ieee754. however, it is necessary that the example format number 36 results in 42 40 in hex. and the number 100 is 59 40. Thanks
Posted
Updated 30-Apr-18 4:34am
v3
Comments
Richard MacCutchan 30-Apr-18 4:25am    
The C# float type is already IEE754.

1 solution

Check Here.

Convert value by IEEE 754 protocol[^][^]

Thanks
--RA
 
Share this answer
 
Comments
ShakalX 30-Apr-18 10:29am    
No work. Is not this what i want.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900