Solved

Convert Byte[] to double[] in .NET

Posted on 2011-09-04
6
343 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
Comment Utility
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
Comment Utility
0
 
LVL 15

Expert Comment

by:x77
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 19

Accepted Solution

by:
Manoj Patil earned 250 total points
Comment Utility
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
Comment Utility
0
 

Author Closing Comment

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

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
Normally the drop down box control found in the .Net framework tools is able to select just one data and value at a time, which is displayed on the text area.   But what if you want to have multiple values to be selected in the drop down box? As …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now