简体   繁体   English

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

[英]conversion from double to byte array and vice versa

i want to convert double array to byte array and byte array to double array 我想将双数组转换为字节数组,将字节数组转换为双数组

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(); 

        }
    }
}

with the convert approach the when bytes are converted back to the doubles the numbers are getting as 10.0,20.0, 30.0, 40.0..... 使用转换方法,当字节转换回双精度时,数字变为10.0,20.0,30.0,40.0 ......

How to get back the same numbers? 如何找回相同的数字?

I mean if the double entered is 10.0, 20.0 , 30.0 , 40.0 and i have get back the same number is 10.0,20.0,30.0,40.0. 我的意思是如果输入的双倍是10.0,20.0,30.0,40.0并且我得到了相同的数字是10.0,20.0,30.0,40.0。

There is no issue with your program - if you slightly modify your program and print the original as well as round-tripped array, you will indeed see that you get the same out-put. 您的程序没有问题 - 如果您稍微修改程序并打印原始阵列和往返阵列,您确实会看到您获得相同的输出。 For example, 例如,

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

Here's the slightly modified code 这是稍微修改过的代码

    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
            ...
     }

You are getting the correct numbers, it is only in writing to the console that you are loosing your .0 the two numbers are equivalant. 你得到的是正确的数字,只有在写入控制台的时候才会丢失你的.0这两个数字是等价的。 10.0 and 10. This is what your doubleArray looks like before writing. 10.0和10.这是你的doubleArray在写之前的样子。

Try changing your Console.Write statement to this: 尝试将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(",");
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM