19

How do I convert a double[] array to a byte[] array and vice versa?

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(sizeof(double));
        Console.WriteLine(double.MaxValue);

        double[] array = new double[] { 10.0, 20.0, 30.0, 40.0 };
        byte[] convertedarray = ?

        Console.Read();
    }
}
5
  • Do you want the conversion of the value (ie. 10.0 -> 10) or the eight bytes of the underlying machine representation (eg. for serialisation)? Commented Aug 5, 2011 at 7:37
  • 1
    What sort of conversion are we talking about here? You want to take the corresponding double and "cast" it to an integral type to get the byte value? Or do you want to get the byte representation of each of the double values? You need to clarify. Commented Aug 5, 2011 at 7:37
  • intention is to use BinaryWriter.Write() function. But this accepts only bytes. I want to get the byte representation of each of the double values? Commented Aug 5, 2011 at 7:40
  • @Raghaav: In that case you want the byte representation of each double. Commented Aug 5, 2011 at 7:42
  • Yes. How to do this? The array size will be 1,00,000 Commented Aug 5, 2011 at 7:45

7 Answers 7

27

Assuming you want the doubles placed in the corresponding byte array one after the other, LINQ can make short work out of this:

static byte[] GetBytes(double[] values)
{
    return values.SelectMany(value => BitConverter.GetBytes(value)).ToArray();
}

Alternatively, you could use Buffer.BlockCopy():

static byte[] GetBytesAlt(double[] values)
{
    var result = new byte[values.Length * sizeof(double)];
    Buffer.BlockCopy(values, 0, result, 0, result.Length);
    return result;
}

To convert back:

static double[] GetDoubles(byte[] bytes)
{
    return Enumerable.Range(0, bytes.Length / sizeof(double))
        .Select(offset => BitConverter.ToDouble(bytes, offset * sizeof(double)))
        .ToArray();
}

static double[] GetDoublesAlt(byte[] bytes)
{
    var result = new double[bytes.Length / sizeof(double)];
    Buffer.BlockCopy(bytes, 0, result, 0, bytes.Length);
    return result;
}
Sign up to request clarification or add additional context in comments.

Comments

6

You can use the Select and ToArray methods to convert one array to another:

oneArray = anotherArray.Select(n => {
  // the conversion of one item from one type to another goes here
}).ToArray();

To convert from double to byte:

byteArray = doubleArray.Select(n => {
  return Convert.ToByte(n);
}).ToArray();

To convert from byte to double you just change the conversion part:

doubleArray = byteArray.Select(n => {
  return Convert.ToDouble(n);
}).ToArray();

If you want to convert each double to a multi-byte representation, you can use the SelectMany method and the BitConverter class. As each double will result in an array of bytes, the SelectMany method will flatten them into a single result.

byteArray = doubleArray.SelectMany(n => {
  return BitConverter.GetBytes(n);
}).ToArray();

To convert back to doubles, you would need to loop the bytes eight at a time:

doubleArray = Enumerable.Range(0, byteArray.Length / 8).Select(i => {
  return BitConverter.ToDouble(byteArray, i * 8);
}).ToArray();

Comments

2

Use the Bitconverter class.

Comments

2
double[] array = new double[] { 10.0, 20.0, 30.0, 40.0 };
byte[] convertedarray = array.Select(x => Convert.ToByte(x)).ToArray();

1 Comment

how to convert byte[] back to double[].
1

You should use Buffer.BlockCopy method.

Look at the pages example, you will clearly understand.

doubleArray = byteArray.Select(n => {return Convert.ToDouble(n);}).ToArray();

Comments

0

You can use something like this, I think:

byte[] byteArray = new byteArray[...];  
...
byteArray.SetValue(Convert.ToByte(d), index);

Comments

-1
var byteArray = (from d in doubleArray
                 select (byte)d)
                .ToArray();

var doubleArray = (from b in byteArray
                   select (double)b)
                  .ToArray();

Cheers.

2 Comments

This will remove some data from big doubles.
@VMAtm: Though not a reason to condemn him for this. Other answers here have the same problem only because it wasn't clear in the first place.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.