实例代码

using System;
using System.Collections.Generic;
using System.IO;

namespace ConvertTest
{
    class Program
    {
        //将List<double> 转为 byte[]
        static byte[] ConvertDoubleArrayToBytes(List<double> matrix)
        {
            if (matrix == null)
            {
                return new byte[0];
            }
            using (MemoryStream stream = new MemoryStream())
            {
                BinaryWriter bw = new BinaryWriter(stream);
                foreach (var item in matrix)
                {
                    bw.Write(item);
                }
                return stream.ToArray();
            }
        }
        //将byte[] 转为 List<double>
        static List<double> ConvertBytesToDoubleArray(byte[] matrix)
        {
            if (matrix == null)
                return null;

            List<double> result = new List<double>();
            using (var br = new BinaryReader(new MemoryStream(matrix)))
            {
                var ptCount = matrix.Length/8;
                for (int i = 0; i < ptCount; i++)
                {
                    result.Add(br.ReadDouble());
                }
                return result;
            }          
        }
        
        static void Main(string[] args)
        {
            List<double> data = new List<double> { 1.0 ,2.0 ,3.0 ,4.0 ,5.0 ,6.0 ,7.0};
            byte[] bdata = ConvertDoubleArrayToBytes(data);
            data = ConvertBytesToDoubleArray(bdata);
            foreach (var item in data)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();
        }
    }
}

    运行结果

   C#将 List 转为 byte[]

相关文章: