在NETMF转换'浮动'到'字节[4]“再回到”浮动“ [英] Converting 'float' to 'byte[4]' and back to 'float' in NETMF

查看:147
本文介绍了在NETMF转换'浮动'到'字节[4]“再回到”浮动“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是一个浮动转换为字节[4] 然后再返回到一个浮动的最佳方式'?

What's the best way to convert a float to a byte[4] and then back to a 'float'?

我在C#中 NETMF 这样做,所以没有 BitConverter 供我使用。

I am doing this in C# NETMF, so there is no BitConverter available for my use.

推荐答案

我修改从的 BitConverter 类=HTTP://forums.netduino .COM /的index.php?/主题/ 308-bitconverter相对=nofollow>一个Netduino实现允许字节序规范(这不是最好的方法,但它的作品)。如果字节数组是通过网络发送,我会使用大尾端。只是一个提醒,不安全不NETMF正式支持。

I've modified the BitConverter class from a Netduino implementation to allow endianness specification (it's not the "best way", but it works). If the byte array is sent over the network, I would use BigEndian. Just a reminder that unsafe is not officially supported in NETMF.

using System;
using System.Diagnostics;

namespace netduino
{
    public static class BitConverter
    {
        public static byte[] GetBytes(uint value)
        {
            return new byte[4] { 
                    (byte)(value & 0xFF), 
                    (byte)((value >> 8) & 0xFF), 
                    (byte)((value >> 16) & 0xFF), 
                    (byte)((value >> 24) & 0xFF) };
        }

        public static unsafe byte[] GetBytes(float value)
        {
            uint val = *((uint*)&value);
            return GetBytes(val);
        }

        public static unsafe byte[] GetBytes(float value, ByteOrder order)
        {
            byte[] bytes = GetBytes(value);
            if (order != ByteOrder.LittleEndian)
            {
                System.Array.Reverse(bytes);
            }
            return bytes;
        }

        public static uint ToUInt32(byte[] value, int index)
        {
            return (uint)(
                value[0 + index] << 0 |
                value[1 + index] << 8 |
                value[2 + index] << 16 |
                value[3 + index] << 24);
        }

        public static unsafe float ToSingle(byte[] value, int index)
        {
            uint i = ToUInt32(value, index);
            return *(((float*)&i));
        }

        public static unsafe float ToSingle(byte[] value, int index, ByteOrder order)
        {
            if (order != ByteOrder.LittleEndian)
            {
                System.Array.Reverse(value, index, value.Length);
            }
            return ToSingle(value, index);
        }

        public enum ByteOrder
        {
            LittleEndian,
            BigEndian
        }

        static public bool IsLittleEndian
        {
            get
            {
                unsafe
                {
                    int i = 1;
                    char* p = (char*)&i;

                    return (p[0] == 1);
                }
            }
        }
    }
}

namespace BitConverterTest
{
    class Program
    {
        static void Main(string[] args)
        {
            byte[] msbFirst = new byte[] { 0x42, 0xF6, 0xE9, 0xE0 };
            byte[] lsbFirst = new byte[] { 0xE0, 0xE9, 0xF6, 0x42 };
            const float f = 123.456789F;

            byte[] b = netduino.BitConverter.GetBytes(f, netduino.BitConverter.ByteOrder.BigEndian);
            for (int i = 0; i < b.Length; i++)
            {
                Debug.Assert(msbFirst[i] == b[i], "BitConverter.GetBytes(float, BigEndian) i=" + i);
            }

            Debug.Assert(f == netduino.BitConverter.ToSingle(msbFirst, 0, netduino.BitConverter.ByteOrder.BigEndian));

            Console.WriteLine("All tests passed");
            Console.ReadKey();
        }
    }
}

这篇关于在NETMF转换'浮动'到'字节[4]“再回到”浮动“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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