Solved

Convert Byte[] to double[] in .NET

Posted on 2011-09-04
6
361 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
Turn Insights into Action

Communication across every corner of your business is essential to increase the velocity of your application delivery and support pipeline. Automate, standardize, and contextualize your communication processes with xMatters.

 
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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

695 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