感觉这个方法很不错  但是调试出来的结果都是6.4打头的 不知道为什么 变换二进制得到的结果也是6.4打头  private void button1_Click(object sender, EventArgs e)
        {
            byte[] b = System.Text.ASCIIEncoding.ASCII.GetBytes("00000111101010101010101101010101"); 
            float a = BitConverter.ToSingle(b, 0);
            textBox1.Text = a.ToString();
        }
难道32位二进制转化成的浮点数全是6.4打头的?

解决方案 »

  1.   


    对的  那个是科学计数
    float ff = 52.368F;
    byte[] bb = BitConverter.GetBytes(ff);
    float f = BitConverter.ToSingle(bb, 0);
    this.textBox1.Text = f.ToString();
      

  2.   

    private void button1_Click(object sender, EventArgs e)
    {
      string s = "00000111101010101010101101010101";
      byte[] b = new byte[4];
      byte[] d = { 1, 2, 4, 8, 16, 32, 64, 128 };
      for (int i = 0; i < 4; i++)
      {
        b[i] = 0;
        for (int j = 0; j < 8; j++)
        {
          if (s[(3-i)*8 + (7-j)] == '1') b[i] += d[j];
        }
      }
      float a = BitConverter.ToSingle(b, 0);
      textBox1.Text = a.ToString();
    }
      

  3.   

    using System;
    using System.Text;class Program
    {
      static void Main()
      {
       float  ff = 52.368F;
        byte[] bb = BitConverter.GetBytes(ff);
        string s  = ToString(bb);
        Console.WriteLine(s);
        Console.WriteLine(ToSingle(s));
      }
      
      static string ToString(byte[] bb)
      {
       StringBuilder sb = new StringBuilder();
       foreach (byte b in bb)
       {
         sb.Append(Convert.ToString(b, 2).PadLeft(8, '0'));
       }
       return sb.ToString();
      }
      
      static float ToSingle(string s)
      {
        byte[] b = new byte[4];
        byte[] d = { 1, 2, 4, 8, 16, 32, 64, 128 };
        for (int i = 0; i < 4; i++)
        {
          b[i] = 0;
          for (int j = 0; j < 8; j++)
          {
            if (s[i*8 + (7-j)] == '1') b[i] += d[j];
          }
        }
        return BitConverter.ToSingle(b, 0);
      }
    }
      
      

  4.   


    请VMM解释一下,十六进制与浮点数的转换问题。大家的结果不一致,哪种方法正确?依据?我再贴上一段,下面是我根据MSDN整理的。// 数据类型 浮点数与十六进制转化
    public string FloatToHex(float floatValue)
    {
        uint uintValue = BitConverter.ToUInt32(BitConverter.GetBytes(floatValue), 0);
        byte[] byteValue = BitConverter.GetBytes(uintValue);
        Array.Reverse(byteValue);
        return BitConverter.ToString(byteValue).Replace("-","");
    }public float HexToFloat(String hexString)
    {
        uint num = uint.Parse(hexString, System.Globalization.NumberStyles.AllowHexSpecifier);
        byte[] floatVals = BitConverter.GetBytes(num);
        return BitConverter.ToSingle(floatVals, 0);
    }
      

  5.   

    多谢空军 问题已解决!9楼的程序太好了!!再次感谢!!!!
    多谢lzsh0622 帮忙!! 你改签名了 以前是永不言弃 帮我解答过许多问题!!呵呵