将float转换为二进制 [英] Convert float to binary

查看:229
本文介绍了将float转换为二进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我想将浮点数转换为二进制,但我有一堵墙请问我,我需要您的一些解释和帮助.我来自德国,请不要管我的语言.我在"IEEE 754" 中对Weekipedia进行了解释.



我尝试做的事:

Hi Guys, please i try to Convert a float number to binary but i have a wall bevor me and i need some explanation and help from u. Please don''t take care of my language, i''m from Germany. i take the explanation of weekipedia in this "IEEE 754".



hier ist waht i try to do:

public class Float_Prezision
   {
       // deklaration der Privaten member
       private int _bias;               //

       public const int konstante = 127;
       ArrayList value = new ArrayList();
       ArrayList biasbinaere = new ArrayList();

       /** Berechnung der Exponent**/
       public void exponent(float werte)
       {
           if (werte < 0)
               werte -= werte;
           int ganzzahl = Convert.ToInt32(Math.Truncate(Math.Log(werte,2)));
           _bias = ganzzahl + konstante;
           string biaswert = Convert.ToString(_bias,2);
           biasbinaere.Add(biaswert);
           Console.Write(" {0} ",biasbinaere[0]);
       }

       //Methode zur Berechnung der Mantisse
        public void mantisse(float werte)
       {
           int ganzzahlteil = (int)werte;
           Single kommawert = werte - ganzzahlteil;

           int ganzzahlwert;

           for (int i = 0; i < 23; i++)
           {
               Single ergebnis = kommawert * 2;
               ganzzahlwert = (int)ergebnis;
               value.Add(ganzzahlwert);
               kommawert = ergebnis - ganzzahlwert;
               Console.Write("{0}", value[i]);
           }
           Console.WriteLine();
       }
             // Look a sign
       public void vorzeichen(float werte)
       {
           if (werte > 0)
               Console.Write("0");
           else
               Console.Write("1");
       }


      //made a calculation
       public void berechnung(float werte)
       {

           vorzeichen(werte);
           exponent(werte);
           mantisse(werte);
       }



当我编译时,例如当我给出数字18,4
时 结果是
010000011 01100110011001100110000
我需要有一个
010000011 00100110011001100110011
请你能帮我吗?
thx Stefan



when i compile for example when i give a number 18,4
the result is
010000011 01100110011001100110000
bit i need to have
010000011 00100110011001100110011
please can u help me?
thx Stefan

推荐答案

浮点数已经是二进制了.

一切都是二进制的,但您的结果是少了很多二进制",因为您直接进入控制台的是字符串或字符链,即人类可以理解的二进制表示形式.出于某种原因,让我们假设这是您真正需要的.从某种意义上说,它仍然是二进制的,您的"1"或"0"字符在内存中表示为Unicode代码点,以16位UTF-16LE字词表示:0x30和0x31.
以这种简化的方式可以找到大小不超过System.Int32的任何数据类型的位.像这样的东西:
Float is already binary, period.

Everything is binary, but your result is "much less binary", because what you right into console is a string or a chain of characters, a human-readable representation of the binary. Let''s assume this is what you really need, by some reason. It''s still binary in that sense that your ''1'' or ''0'' characters are represented in memory as Unicode code points represented as 16-bit UTF-16LE words: 0x30 and 0x31.

The bits of any data type of the size no more then the size of of System.Int32 can be found in this simplified way. Something like this:
static string ToBinaryString(float value) {

    int bitCount = sizeof(float) * 8; // never rely on your knowledge of the size
    char[] result = new char[bitCount]; // better not use string, to avoid ineffective string concatenation repeated in a loop

    // now, most important thing: (int)value would be "semantic" cast of the same
    // mathematical value (with possible rounding), something we don't want; so:
    int intValue = System.BitConverter.ToInt32(BitConverter.GetBytes(value), 0);

    for (int bit = 0; bit < bitCount; ++bit) {
        int maskedValue = intValue & (1 << bit); // this is how shift and mask is done.
        if (maskedValue > 0)
            maskedValue = 1;
        // at this point, masked value is either int 0 or 1
        result[bitCount - bit - 1] = maskedValue.ToString()[0]; // bits go right-to-left in usual Western Arabic-based notation
    }

    return new string(result); // string from character array
}



如果是任意大小的数据,则需要使用System.BitConverter序列化为字节,并在两个嵌套循环中使用单个字节.

另请参阅: http://msdn.microsoft.com/en-us/library/system.bitconverter .aspx [^ ].

要查看尾数,指数等的位置,请访问: http://en.wikipedia.org/wiki/IEEE_floating_point [< ^ ].

学习基础知识.

祝你好运,

—SA



If the case of arbitrary-size data, you need to serialize to bytes using System.BitConverter and work with individual bytes, in two nested loops.

See also: http://msdn.microsoft.com/en-us/library/system.bitconverter.aspx[^].

To see where are mantissa, exponent, etc., please see: http://en.wikipedia.org/wiki/IEEE_floating_point[^].

Learn the basics.

Good luck,

—SA


您忘记在尾数内包含整数部分(例如18).
也就是说,在Wikipedia 食谱中:
You forgot to include the integer part (e.g. 18) inside the mantissa.
That is, of the Wikipedia recipe:
在这里,我们可以显示如何使用以下轮廓将以10为底的实数转换为IEEE 754 binary32格式:
考虑具有整数和小数部分(例如12.375
)的实数
Here we can show how to convert a base 10 real number into an IEEE 754 binary32 format using the following outline :
consider a real number with an integer and a fraction part such as 12.375
  1. 将整数部分转换并归一化为二进制
  2. 使用如下所示的技术转换分数部分
  3. 将两个结果相加并进行调整以产生合适的结果最终转化



您错过了步骤1.

我更改了mantisse方法,使其包含了这样的部分(作为证明:您可能会发现一种与编程风格更加协调一致"的方法).



you missed step 1.

I changed the mantisse method to include such part (just as proof: you may find a way more ''in tune'' with your programming style).

//Methode zur Berechnung der Mantisse
    public void mantisse(float werte)
    {
      int ganzzahlteil = (int)werte;
      Single kommawert = werte - ganzzahlteil;

      int norm = 1;
      int mask = 1;
      int ganzzahlteil_copy = ganzzahlteil;
      while ((ganzzahlteil_copy >>= 1 ) > 1)
      {
        norm++;
        mask <<= 1;
      }
      for(int i=0; i<norm;i ++)
      {
        value.Add (((ganzzahlteil & mask) == mask ) ? 1 : 0);
         ganzzahlteil <<= 1;
        Console.Write("{0}", value[i]);
      }

      int ganzzahlwert;

      for (int i = norm; i < 23; i++)
      {
        Single ergebnis = kommawert * 2;
        ganzzahlwert = (int)ergebnis;
        value.Add(ganzzahlwert);
        kommawert = ergebnis - ganzzahlwert;
        Console.Write("{0}", value[i]);
      }
      Console.WriteLine();
    }


这篇关于将float转换为二进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆