Solved

Convert Byte[] to double[] in .NET

Posted on 2011-09-04
6
356 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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
start a process from a service 3 33
Help with converting xml file to excel using VB.NET 3 25
location of a form 2 25
Groupbox Control ? 2 20
Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

820 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