Convert float to its binary representation (using MemoryStream?)

痞子三分冷 提交于 2020-02-02 02:27:23

问题


I'd like to convert a given float into its binary representation. I tried to write the float value into a MemoryStream, read this MemoryStream byte by byte and convert the bytes into their binary representation. But every attempt failed.

  • "Can't read closed stream" (but I only closed the writer)
  • For test purposes I simply wrote an integer (I think four bytes in size) and the length of the MemoryStream was 0, when I didn't flush the StreamWriter, and 1, when I did.

I'm sure there is a better way to convert floats to binary, but I also wanted to learn a little bit about the MemoryStream class.


回答1:


You can use BitConverter.GetBytes(float) or use a BinaryWriter wrapping a MemoryStream and use BinaryWriter.Write(float). It's not clear exactly what you did with a MemoryStream before, but you don't want to use StreamWriter - that's for text.




回答2:


Using BitConverter, not MemoryStream:

        // -7 produces "1 10000001 11000000000000000000000"
        static string FloatToBinary(float f)
        {
            StringBuilder sb = new StringBuilder();
            Byte[] ba = BitConverter.GetBytes(f);
            foreach (Byte b in ba)
                for (int i = 0; i < 8; i++)
                {
                    sb.Insert(0,((b>>i) & 1) == 1 ? "1" : "0");
                }
            string s = sb.ToString();
            string r = s.Substring(0, 1) + " " + s.Substring(1, 8) + " " + s.Substring(9); //sign exponent mantissa
            return r;
        }



回答3:


You might have run into a pitfall when using StreamWriter, as the following code shows:

        // Write the float
        var f = 1.23456f;
        var ms = new MemoryStream();
        var writer = new StreamWriter(ms);
        writer.Write(f);
        writer.Flush();

        // Read 4 bytes to get the raw bytes (Ouch!)
        ms.Seek(0, SeekOrigin.Begin);
        var buffer = new char[4];
        var reader = new StreamReader(ms);
        reader.Read(buffer, 0, 4);
        for (int i = 0; i < 4; i++)
        {
            Console.Write("{0:X2}", (int)buffer[i]);
        }
        Console.WriteLine();

        // This is what you actually read: human readable text
        for (int i = 0; i < buffer.Length; i++)
        {
            Console.Write(buffer[i]);
        }
        Console.WriteLine();

        // This is what the float really looks like in memory.
        var bytes = BitConverter.GetBytes(f);
        for (int i = 0; i < bytes.Length; i++)
        {
            Console.Write("{0:X2}", (int)bytes[i]);
        }

        Console.ReadLine();

If you expect only 4 bytes to be in the stream and read those 4 bytes, everything looks fine at first sight. But actually the length is 7 and you have read only the first 4 bytes of the text representation of the float.

Comparing that to the output of the BitConverter reveals that using StreamWriter is not the correct thing here.




回答4:


To answer your first question: In .Net, when you close/dispose a reader/writer, the underlying stream is also closed/disposed.



来源:https://stackoverflow.com/questions/21244252/convert-float-to-its-binary-representation-using-memorystream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!