C# hexadecimal de cuatro bytes y conversión de punto flotante de precisión simple

La conversión de punto flotante de precisión simple y hexadecimal de cuatro bytes de C# puede usar la función integrada, o puede escribirla usted mismo

Los ejemplos son los siguientes:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace floatDemo
{
    class Program
    {
        //首先设置:项目->属性->生成->常规->允许不安全代码 勾选即可
        //由四个字节的十六机制数组转浮点数 自定义
        public static float ToFloat(byte[] data)
        {
            float a = 0;
            byte i;
            byte[] x = data;
            unsafe
            {
                void* pf;
                fixed (byte* px = x)
                {
                    pf = &a;
                    for (i = 0; i < data.Length; i++)
                    {
                        *((byte*)pf + i) = *(px + i);
                    }
                }
            }


            return a;
        }
   

Supongo que te gusta

Origin blog.csdn.net/qq_30725967/article/details/132251192
Recomendado
Clasificación