?
Solved

Convert Byte[] to double[] in .NET

Posted on 2011-09-04
6
Medium Priority
?
378 Views
Last Modified: 2012-05-12
The Binary File class in .NET has a nice method to read all the bytes in a binary file into a byte[].
In C++ I could just use a pointer a double and assign it to the address the first byte in the byte[] to get a double[].
How could I do such a thing in C# in .NET?
0
Comment
Question by:ManionG
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 15

Expert Comment

by:x77
ID: 36482549
The standard method to convert Byte Array to Net Types (Char, string, Date, Double, long ....) is the BinaryReader.
Note that you can create a memorystream from a byte array and create a BinaryReader from memorystream.

BinaryReader contains all methods to get each primary type from basestream.
Note also that it is normalized (grand endian / litle endian).
You says about c++ ... On C++ this conversion is not normalized. You can use also not normalized conversion (bitconverter).

With BitConverter you can get a Double from a ByteArray and also get Bytes from a Double, but result is system dependent (grand endian / litle endian).
0
 
LVL 15

Expert Comment

by:x77
ID: 36482587
Other class that allows get bytes / put Bytes from primary types is  Marshall.
This class allows work with memory and allows write and read methods.

Note that you can need not normalized (endian dependent) when you read information saved with old C++ programs.

On Vb.Net, some time ago I make a Convert Program to get binary information saved on a c program:
Sample code line:
    nCte = BitConverter.ToUInt16(bf, p) : p += 2

The marshall class works with memory pointers (IntPtr).
0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
LVL 19

Accepted Solution

by:
Manoj Patil earned 750 total points
ID: 36484472
Try with this

double[] doubles = new double[bytes.Length / 8];
Buffer.BlockCopy(bytes, 0, doubles, 0, bytes.Length);

Open in new window


or need to use BitConverter.ToDouble

OR USE FOLLOWING CODE

public void ConvertDoubleByte(double doubleVal) {
			byte	byteVal = 0;

			// Double to byte conversion can overflow.
			try {
				byteVal = System.Convert.ToByte(doubleVal);
				System.Console.WriteLine("{0} as a byte is: {1}.",
					doubleVal, byteVal);
			} 
			catch (System.OverflowException) {
				System.Console.WriteLine(
					"Overflow in double-to-byte conversion.");
			}

			// Byte to double conversion cannot overflow.
			doubleVal = System.Convert.ToDouble(byteVal);
			System.Console.WriteLine("{0} as a double is: {1}.",
				byteVal, doubleVal);
		}

Open in new window

0
 
LVL 19

Expert Comment

by:Manoj Patil
ID: 36484476
0
 

Author Closing Comment

by:ManionG
ID: 36486157
Many thanks for bringing the Buffer class to my attention. The perfect solution.
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question