Solved

Convert Byte[] to double[] in .NET

Posted on 2011-09-04
6
350 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
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 16

Expert Comment

by:Easwaran Paramasivam
ID: 36482568
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 19

Accepted Solution

by:
Manoj Patil earned 250 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

786 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