从double到byte数组的转换,反之亦然

时间:2012-03-06 06:26:20

标签: c#

我想将双数组转换为字节数组,将字节数组转换为双数组

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


namespace BitConversion
{
    class Program
    {
        static void Main(string[] args)
        {
            double[] Array = new double[] { 10.0, 20.0, 30.0, 40.0 };
            byte[] byteArray = GetBytesAlt(Array);
            for (int i = 0; i < byteArray.Length; i++)
            {
                Console.Write(byteArray[i]);
                Console.Write(",");

            }

            Console.WriteLine();

            double[] doubleArray = GetDoublesAlt(byteArray);
            for (int i = 0; i < doubleArray.Length; i++)
            {
                Console.Write(doubleArray[i]);
                Console.Write(",");
            }

            Console.WriteLine();

            Console.ReadLine();
        }

        /// <summary>
        /// convert to bytes
        /// </summary>
        /// <param name="values"></param>
        /// <returns></returns>
        static byte[] GetBytesAlt(double[] values)
        {
            /*var result = new byte[values.Length * sizeof(double)];
            Buffer.BlockCopy(values, 0, result, 0, result.Length); 
            return result;*/

            return values.SelectMany(value => BitConverter.GetBytes(value)).ToArray();

            //return values.SelectMany(value => BitConverter.GetBytes(value)).ToArray(); 

        }

        static double[] GetDoublesAlt(byte[] bytes)
        {
           /* var result = new double[bytes.Length / sizeof(double)];
            Buffer.BlockCopy(bytes, 0, result, 0, bytes.Length);
            return result;*/
            return Enumerable.Range(0, bytes.Length / sizeof(double)).Select(offset => BitConverter.ToDouble(bytes, offset * sizeof(double))).ToArray(); 

        }
    }
}

使用convert方法将字节转换回双精度数 这些数字是10.0,20.0,30.0,40.0 .....

如何找回相同的数字?

我的意思是输入的双精度是10.0,20.0,30.0,40.0 我得到的回答是相同的数字是10.0,20.0,30.0,40.0。

2 个答案:

答案 0 :(得分:1)

您的程序没有问题 - 如果您稍微修改程序并打印原始阵列和往返阵列,您确实会看到您获得相同的输出。 例如,

Original:       10.1,20.2,30,40,
Round-trip:     10.1,20.2,30,40,

这是稍微修改过的代码

    class Program
    {
        static void Main(string[] args)
        {
            double[] Array = new double[] { 10.1, 20.2, 30.0, 40.0 };
            Console.Write("Original:\t");
            PrintArray(Array);

            byte[] byteArray = GetBytesAlt(Array);
            double[] doubleArray = GetDoublesAlt(byteArray);
            Console.Write("Round-trip:\t");
            PrintArray(doubleArray);

            Console.ReadLine();
        }

        static void PrintArray(Double[] array)
        {
            for (int i = 0; i < array.Length; i++)
            {
                Console.Write(array[i]);
                Console.Write(",");
            }
            Console.WriteLine();
        }

            // rest of methods are same
            ...
     }

答案 1 :(得分:1)

你得到的是正确的数字,只有在写入控制台的时候才会丢失你的.0这两个数字是等价的。 10.0和10.这是你的doubleArray在写之前的样子。

尝试将Console.Write语句更改为:

for (int i = 0; i < doubleArray.Length; i++)
    {
      //Since you are forcing the .0 you need to make sure you have enough #'s 
      //for the level of precision you need. It is actually better to use the 
      //system default since 10.0 and 10 are functionaly equivalent.

        Console.Write( doubleArray[i].ToString("##.0####")); 
        Console.Write(",");
    }