• 大小: 53KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-25
  • 语言: C#
  • 标签: IEEE  745  C#  

资源简介

本代码实现了十进制浮点数与IEEE745标准的32位二进制浮点数的互相转换,二进制浮点数有十六进制和二进制两者表示形式,

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FlaotConvert
{
    class FConvert
    {
        public static string FloatToStandard(double f int style)
        {
            char[] Num = new char[32];//32为二进制结果加1结束符

            if(f<0) //f为负数 符号位Num[0]赋1
            {
                Num[0] = ‘1‘;
                f = f*(-1); //转换为整数
            }
            else
                Num[0] = ‘0‘;

            //求指数
            int exponent=0;
            if(f >= 1)//指数符号为正情况,即fabs(f)>=1 
            {
                while(f>=2)
                {
                exponent++;
                f = f/2;
                }
            }
            else//负指数情况
            {
                while (f<1)
                {
                exponent++;
                f = f*2;
                }
                exponent = (128-exponent) + 128;//(128-exponent)为补码+128是指数符号位置1
            }

            exponent += 127;//指数转换为阶码

            int i;
            for(i=8;i>=1;i--)//将指数转换为二进制数
            {
                if(exponent%2 == 0)
                Num[i] = ‘0‘;
                else
                Num[i] = ‘1‘;
                exponent  /= 2;
            }
        
            f = f -1;//小数转换为标准格式
            //求小数部分
            double s = 1;
            for(i=9;i<32;i++)
            {
                s /= 2;
                if(f>=s)
                {
                    //MessageBox.Show(Convert.ToString(s));
                f = f-s;
                Num[i] = ‘1‘;
                }
                else
                Num[i] = ‘0‘;
            }

            if (style == 2)//二进制输出
            {
                string tt = new string(Num);
                return tt;
            }
            else //二进制转换为十六进制
            {
                char sum;
                int j = 0;
                for (i = 0; i < 32; i = i + 4 j++)
                {
                    sum = Convert.ToChar((Num[i] - ‘0‘) * 8 + (Num[i + 1] - ‘0‘) * 4 + (Num[i + 2] - ‘0‘) * 2 + (Num[i + 3] - ‘0‘));
                    if (sum > 9)
                        Num[j] = Convert.ToChar((sum - 10) + ‘a‘);
                    else
                        Num[j] = Convert.ToChar(sum + ‘0‘);
                }
                string tt = new string(Num);
                tt = tt.Substring(0 8);
                return tt;
            }
        }

         public static double StandardToFloat(string FNum int style)
        {
            char[] Num = new char[32];
            char[] Hex = new char[8];
        
            int i j value;

            if (style == 2)//二进制
            {
                for (i = 0; i < 32; i++)
                    Num[i] = FNum[i];
            }
            else//十六进制转换为二进制
            {
                for (i = 0; i < 8; i++)
                {
                    Hex[i] 

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件      14336  2011-04-25 21:38  FlaotConvert\FlaotConvert\bin\Debug\FlaotConvert.exe

     文件      34304  2011-04-25 21:38  FlaotConvert\FlaotConvert\bin\Debug\FlaotConvert.pdb

     文件      14328  2011-04-25 21:35  FlaotConvert\FlaotConvert\bin\Debug\FlaotConvert.vshost.exe

     文件        490  2009-06-11 05:14  FlaotConvert\FlaotConvert\bin\Debug\FlaotConvert.vshost.exe.manifest

     文件       5235  2011-04-25 12:11  FlaotConvert\FlaotConvert\FConvert.cs

     文件       3809  2011-04-25 21:35  FlaotConvert\FlaotConvert\FlaotConvert.csproj

     文件       3932  2011-04-25 21:38  FlaotConvert\FlaotConvert\Form1.cs

     文件      12834  2011-04-25 21:29  FlaotConvert\FlaotConvert\Form1.Designer.cs

     文件       5814  2011-04-25 21:29  FlaotConvert\FlaotConvert\Form1.resx

     文件       1558  2011-04-25 21:35  FlaotConvert\FlaotConvert\obj\Debug\FlaotConvert.csproj.FileListAbsolute.txt

     文件        847  2011-04-25 21:29  FlaotConvert\FlaotConvert\obj\Debug\FlaotConvert.csproj.GenerateResource.Cache

     文件      14336  2011-04-25 21:38  FlaotConvert\FlaotConvert\obj\Debug\FlaotConvert.exe

     文件        180  2011-04-25 21:38  FlaotConvert\FlaotConvert\obj\Debug\FlaotConvert.form1.resources

     文件      34304  2011-04-25 21:38  FlaotConvert\FlaotConvert\obj\Debug\FlaotConvert.pdb

     文件        180  2011-04-25 21:38  FlaotConvert\FlaotConvert\obj\Debug\FlaotConvert.Properties.Resources.resources

     文件        493  2011-04-25 11:31  FlaotConvert\FlaotConvert\Program.cs

     文件       1356  2011-04-24 21:18  FlaotConvert\FlaotConvert\Properties\AssemblyInfo.cs

     文件       2874  2011-04-24 21:18  FlaotConvert\FlaotConvert\Properties\Resources.Designer.cs

     文件       5612  2011-04-24 21:18  FlaotConvert\FlaotConvert\Properties\Resources.resx

     文件       1097  2011-04-24 21:18  FlaotConvert\FlaotConvert\Properties\Settings.Designer.cs

     文件        249  2011-04-24 21:18  FlaotConvert\FlaotConvert\Properties\Settings.settings

     文件      14336  2011-04-25 21:38  FlaotConvert\FlaotConvert.exe

     文件        926  2011-04-24 21:18  FlaotConvert\FlaotConvert.sln

    ..A..H.     19456  2011-04-25 21:41  FlaotConvert\FlaotConvert.suo

     目录          0  2011-04-24 22:20  FlaotConvert\FlaotConvert\obj\Debug\Refactor

     目录          0  2011-04-24 21:18  FlaotConvert\FlaotConvert\obj\Debug\TempPE

     目录          0  2011-04-25 21:29  FlaotConvert\FlaotConvert\bin\Debug

     目录          0  2011-04-25 21:38  FlaotConvert\FlaotConvert\obj\Debug

     目录          0  2011-04-24 22:11  FlaotConvert\FlaotConvert\bin

     目录          0  2011-04-24 21:18  FlaotConvert\FlaotConvert\obj

............此处省略6个文件信息

评论

共有 条评论