用c# 进行浮点型与UINT 转换 可以用MEMORY的操作

来源:互联网 发布:淘宝客佣金怎么提现 编辑:程序博客网 时间:2024/05/16 06:10

在网上  只有UINT转换为浮点型的例子 并且方法很笨  我发一个全的

前阵子正好用到  发现没有人写 自己写一个发出来 给大家使用

 

这个转换有精度的丢失,大家拿回去改一改 看看为什么会丢失精度

 

转载请标明 kris

 

#region //Uint转化float型
            uint a = 0xC1C90000;//这个添要转换的UINT就可以
            byte b = (byte)(a >> 31);

            ushort c = (ushort)((a & 0x7F800000) >> 23);
            int saveM = (int)((a & 0x7FFFFF) + 0x800000);
            float f1 = (float)saveM;
            if ((c - 0x7F) < 23)
                for (int i = 0; i < 23 - c + 0x7F; i++)
                {
                    f1 /= 2;
                }
            else
                for (int i = 0; i < 23 - c + 0x7F; i++)
                {
                    f1 *= 2;
                }
            if (b == 0x1)
            { f1 = -f1; }
            MessageBox.Show("原数:" + Convert.ToString(a, 16) +
                "/n符号位:" + Convert.ToString(b, 16) + " E位:" + Convert.ToString(c, 16) + " M位:" + Convert.ToString(a & 0x7FFFFF, 16) +
                "结果" + f1.ToString());
            #endregion  //Uint转化float型结束

 

 

 

  void floattouint()
        {
            //MessageBox.Show("进入");//**********************************
            // float转uint
            uint reslut = 0x0;//保存整个16进制值

            //float f2 = -178.125f; //要转化的浮点数
            float f2 = 0.5f; //要转化的浮点数


            float f3 = 0;//存整个浮点数的绝对值

            float f4 = 0;//存小数计算初始值


            if (f2 < 0)
                f3 = -f2;
            else
                f3 = f2;
            string str1 = f3.ToString();//存整个浮点数的绝对值的字符串
            string str2 = "";//存小数部分
            string str3 = "";//存整数部分
            if (f2 < 0)
            { reslut = 0x80000000; }
            string[] strA = str1.Split('.');
            if (strA[0] == "0")
            {
                if (strA[1] != "")//只有小数
                {
                    MessageBox.Show("只有小数");//**********************************
                    f4 = f3;
                    int check = 0;
                    while (f4 != 0 && check < 24)//计算小数部分的2进制值
                    {
                        if ((f4 * 2) > 0)
                        {
                            str2 += "1";
                            f4 = f4 * 2 - 1;
                        }
                        else
                        {
                            str2 += "0";
                            f4 = f4 * 2;
                        }

                        check++;
                    }
                    bool b_check = false;
                    int i_getyiwei = -1;
                    for (int i = 0; i < str1.Length; i++)//查找第一个有效位
                    {
                        if (str2[i] == '1')
                        {
                            i_getyiwei = i + 1;
                            b_check = true;
                            break;
                        }
                    }
                    if (b_check && i_getyiwei != -1)//求E码
                    {
                        string str_t1 = "";
                        if (-i_getyiwei + 127 > 254 || -i_getyiwei + 127 < 1)
                        {
                            MessageBox.Show("E码异常"); return;
                        }
                        reslut += ((uint)(-i_getyiwei + 127)) << 23;//E码加到结果中
                        for (int i = str2.Length; i < 24; i++)//补位到 24位
                        {
                            str2 += "0";
                        }
                        for (int i = 0; i < 6; i++)//转化成16进制
                        {
                            str_t1 += get0X(str2.Substring(i * 4, 4));
                        }
                        reslut += (Convert.ToUInt32(str_t1, 16) - 0x800000);//值加到结果中 结束
                    }
                    else
                    {
                        reslut = 0x0;
                    }


                }
                else
                {
                    reslut = 0x0;
                }
            }
            else if (strA[0] != "")//有整数
            {
                MessageBox.Show("有整数");//**********************************
               
                if (strA.Length !=1)//有整数和小数
                {//计算小数位数
                    MessageBox.Show("有整数和小数");//**********************************
                    f4 = f3 - Convert.ToInt32(strA[0]);//得到小数
                    int check = 0;
                    while (f4 != 0 && check < 24)//求出小数的2进制数
                    {
                        if ((f4 * 2) > 0)
                        {
                            str2 += "1";
                            f4 = f4 * 2 - 1;
                        }
                        else
                        {
                            str2 += "0";
                            f4 = f4 * 2;
                        }

                        check++;
                    }

                    //计算整数数位数
                    int i_t1 = Convert.ToInt32(strA[0]);//得到整数
                    string str_t1 = Convert.ToString(i_t1, 16);//转化成16进制
                    for (int i = 0; i < str_t1.Length; i++)//把整数转化成2进制
                    {
                        str3 += get0101(str_t1[i].ToString());
                    }
                    int i_t2 = 0;
                    for (int i = 0; i < str3.Length; i++)//除去前面多余的0
                    {
                        if (str3[i] == '0')
                        {
                            i_t2++;
                        }
                        else
                        {
                            break;
                        }

                    }
                    str3 = str3.Substring(i_t2);//除掉了前面的0
                    int i_save = str3.Length; //记录整数有效部分的长度用于计算E
                    //求E码
                    if ((i_save-1  + 127) > 254 || (i_save-1  + 127) < 1)
                    {
                        MessageBox.Show("E码异常"); return;
                    }
                    reslut += ((uint)(i_save-1 + 127)) << 23;
                    //合起来运算
                    string str_t2 = str3 + str2;
                    if (str_t2.Length > 24)//去除多余的位
                    {
                        str_t2 = str_t2.Substring(0, 24);
                    }
                    else
                    {

                        for (int i = str_t2.Length; i < 24; i++)//补到24位计算M
                        {
                            str_t2 += "0";
                        }
                    }

                    string str_t3 = "";
                    for (int i = 0; i < 6; i++)
                    {
                        str_t3 += get0X(str_t2.Substring(i * 4, 4));
                    }
                    reslut += (Convert.ToUInt32(str_t3, 16) - 0x800000);

                }
                else//只有整数
                {
                    MessageBox.Show("只有整数");//**********************************
                    int i_t1 = Convert.ToInt32(strA[0]);//得到整数
                    string str_t1 = Convert.ToString(i_t1, 16);//得到16进制字符串
                    for (int i = 0; i < str_t1.Length; i++)//得到整数的2进制
                    {
                        str3 += get0101(str_t1[i].ToString());
                    }
                    int i_t2 = 0;
                    for (int i = 0; i < 4; i++)//除去前面多余的0
                    {
                        if (str3[i] == '0')
                        {
                            i_t2++;
                        }
                        else
                        {
                            break;
                        }

                    }
                    str3 = str3.Substring(i_t2);//除掉了前面的0
                    int i_save = str3.Length; //记录整数有效部分的长度用于计算E
                    //求E码
                    if ((i_save-1 + 127) > 254 || (i_save-1 + 127) < 1)
                    {
                        MessageBox.Show("E码异常"); return;
                    }
                    reslut += ((uint)(i_save-1 + 127)) << 23;
                    string str_t2 = str3;
                    if (str_t2.Length > 24)//去除多余的位
                    {
                        str_t2 = str_t2.Substring(0, 23);
                    }
                    else
                    {

                        for (int i = str_t2.Length; i < 24; i++)//补位计算M
                        {
                            str_t2 += "0";
                        }
                    }
                    string str_t3 = "";
                    for (int i = 0; i < 6; i++)
                    {
                        str_t3 += get0X(str_t2.Substring(i * 4, 4));
                    }
                    reslut += (Convert.ToUInt32(str_t3, 16) - 0x800000);

                }
            }
            else
            {
                MessageBox.Show("浮点型变量不正确"); return;
            }
            if (f2 == 0)
            { reslut = 0x0; }
           // textBox1.Text = reslut.ToString();
            MessageBox.Show("结果为:"+Convert.ToString( reslut,16));
        }
        // float转uint结束

 

string get0101(string str)
        {

            byte bt = Convert.ToByte(str, 16);
            if (bt == 0x0)
            {
                str = "0000";
            }
            else if (bt == 0x1)
            {
                str = "0001";
            }
            else if (bt == 0x2)
            {
                str = "0010";
            }
            else if (bt == 0x3)
            {
                str = "0011";
            }
            else if (bt == 0x4)
            {
                str = "0100";
            }
            else if (bt == 0x5)
            {
                str = "0101";
            }
            else if (bt == 0x6)
            {
                str = "0110";
            }
            else if (bt == 0x7)
            {
                str = "0111";
            }
            else if (bt == 0x8)
            {
                str = "1000";
            }
            else if (bt == 0x9)
            {
                str = "1001";
            }
            else if (bt == 0x10)
            {
                str = "1010";
            }
            else if (bt == 0x11)
            {
                str = "1011";
            }
            else if (bt == 0x12)
            {
                str = "1100";
            }
            else if (bt == 0x13)
            {
                str = "1101";
            }
            else if (bt == 0x14)
            {
                str = "1110";
            }
            else if (bt == 0x15)
            {
                str = "1111";
            }
            return str;
        }
 string get0X(string str)
        {
            string str1 = "";
            uint u_1 = 0;
            if (str.Length == 4)
            {
                if (str[0] != '0')
                {
                    u_1 += 0x8;
                }

                if (str[1] != '0')
                {
                    u_1 += 0x4;
                }

                if (str[2] != '0')
                {
                    u_1 += 0x2;
                }

                if (str[3] != '0')
                {
                    u_1 += 0x1;
                }

            }
            str1 = Convert.ToString(u_1, 16);
            return str1;
        }