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

查看:164
本文介绍了如何在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 可能会冻结.在这种情况下,您需要改进用户界面,没有人会喜欢看比他可能阅读的速度更快的数字列表.

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#中将浮点数转换为二进制数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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