如何在c#中将浮点数转换为二进制?

how to convert float into binary in c#?(如何在c#中将浮点数转换为二进制?)

本文介绍了如何在c#中将浮点数转换为二进制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 2 个花车 f1 &f2.我需要将它们转换为二进制(例如 01010101 10000001 11110000 01011110,即 8 位 *4),然后通过串行端口发送它们.在接收端,我接收到这些二进制信号后,需要将它们转换成浮点数显示为字符串.

I have 2 floats f1 & f2. I need to convert them into binary(e.g. 01010101 10000001 11110000 01011110, i.e.8 bits*4) then send them through a serial port. At the receiving end, after receiving these signals in binary, I need to convert them into float and display them as a string.

我的问题是:

  1. 我从浮点数到二进制的转换正确吗?

  1. Is my conversion from float to binary correct?

收到信号后,textbox3 只显示一些随机码,而textbox4 为空.这是为什么呢?

When the signal is received, textbox3 only displays some random code while textbox4 is empty. Why is this?

我目前的代码如下.

byte[] b1 = System.BitConverter.GetBytes(f1), b2 = System.BitConverter.GetBytes(f2);
        if (serialPort.IsOpen)
        {
            serialPort.Write(b1, 0, 4);
            serialPort.Write(b2, 0, 4);
        }

接收:

private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
    {
        SerialPort sp = (SerialPort)sender;
        byte[] b = new byte[sp.BytesToRead];
        sp.Read(b, 0, sp.BytesToRead);
        Invoke(new Action(() => textBox3.AppendText(Encoding.UTF8.GetString(b))));
        float newf = System.BitConverter.ToSingle(b, 0);
        textBox4.Text = newf.ToString();
     }

推荐答案

有几个错误,希望你会收到发送的整个包裹".串行端口不能那样工作.您必须等到收到所有 8 个字节.Encoding.UTF8 非常不合适,它不是文本.像这样写:

Several mistakes, you hope that you'll get the entire 'package' that was sent. Serial ports do not work that way. You'll have to wait until all 8 bytes were received. Encoding.UTF8 is quite inappropriate, it isn't text. Write something like this:

private byte[] Buffer = new byte[8];
private int Rcved = 0;

private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
    SerialPort sp = (SerialPort)sender;
    Rcved += sp.Read(Buffer, Rcved, 8 - Rcved);
    if (Rcved < 8) return;
    float f1 = BitConverter.ToSingle(Buffer, 0);
    float f2 = BitConverter.ToSingle(Buffer, 4);
    string txt3 = BitConverter.ToString(Buffer);
    string txt4 = String.Format("{0}, {1}", f1, f2);
    this.BeginInvoke(new Action(() => {
        textBox3.AppendText(txt3 + Environment.NewLine);
        textBox4.Text = txt4;
    }));
    Rcved = 0;
}

当心同步问题,当设备忙于发送时,您的程序无法正常启动,它只有八分之一的机会从正确的字节开始读取.解决这个问题需要一个协议,一个非常简单的协议是主从,设备在明确要求它之前不会发送任何东西.

Beware a synchronization problem, your program can't properly start up when the device is busy sending, it will only have a 1 in 8 chance to start reading at the correct byte. Solving this requires a protocol, a very simple one is master-slave where the device doesn't send anything until it is explicitly asked to do so.

请注意消防水管问题,如果 BeginInvoke() 调用过于频繁,您的 UI 可能会冻结.在这种情况下,您需要改进 UI,没有人会喜欢查看飞速飞逝的数字列表,而不是他可能阅读的速度.

And beware of a fire-hose problem, your UI can freeze on the BeginInvoke() calls if they occur too frequently. In which case you need to improve the UI, no human is going to enjoy looking at a list of numbers flying by faster than he can possibly read anyway.

这篇关于如何在c#中将浮点数转换为二进制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何在c#中将浮点数转换为二进制?

基础教程推荐