Binary string (4) to Double

F

Freddy Coal

Hi, I'm recovery data from an spectrum analizer, the equipment send me data
in binary, each data in the trace have 4 bytes, I would like convert that to
double, how make that?.

I'm trying extract the data in substrings of 4 chars, and convert this with
Cdbl(my substring), but I get an InvalidCastException in the conversion.

Example of the spectrum data:

ÿþÃÑÿþÉLÿþÃÿþË:ÿþ²øÿþÂ&ÿþÆAÿþ®ÿþÄ5ÿþÎ%ÿþ¹»ÿþÊ

How transform substrings of 4 bytes in a double number?.

Thanks in advance.

Freddy Coal
 
M

Mattias Sjögren

How transform substrings of 4 bytes in a double number?.

Well first you have to tell us in which format you're getting the
data, i.e. what does the four byte represent. A Double itself is 8
bytes, so clearly it's not a simple binary copy of that.

Maybe the BitConverter class will be useful.


Mattias
 
F

Freddy Coal

Mattias, thanks for your answer, I know that the double variable in Vb.net
have 8 bytes, and take maximun values of 2^64, the binary data lenght is of
4 bytes, I would like catch this binary string and transform in a number.

The four bytes represent a negative number, the range is between -10000 and
-140000, the binary data are in Motorola order (The Motorola byte order is
Big Endian where the most significant byte is first), my other question is
how convert Motorola to Intel format.

Thanks in advance.

Freddy Coal
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Freddy said:
Mattias, thanks for your answer, I know that the double variable in Vb.net
have 8 bytes, and take maximun values of 2^64, the binary data lenght is of
4 bytes, I would like catch this binary string and transform in a number.

The four bytes represent a negative number, the range is between -10000 and
-140000,

How is the number stored? As an int (32 bit two-complement)? As a single
(32 bit IEEE)?
the binary data are in Motorola order (The Motorola byte order is
Big Endian where the most significant byte is first), my other question is
how convert Motorola to Intel format.

You just reverse the bytes.
Thanks in advance.

Freddy Coal
 
M

Mattias Sjögren

Göran, The data type is signed 32 bit, the IEEE 32 bit is for floats, but my
numbers are integers between -10000 and -140000, represented by a string of
4 bytes, how change the string to double in Vb.net is the question.

My other question is how reverse the bytes (Motorola to Intel) of an binary
file (with 2bytes or 4 bytes data)?.

I send you a binary file of the data trace (*.fc is my binary), and other
TXT with the trace in the expected result for that trace; The binary data
have a offset of 6 bytes (#42404), the first number (4) is the lenght of the
rest of header, the 2404 represent the number of bytes in the trace, we have
601 points, each point have 4bytes.


From your other thread I guess you might already have figured this
out. But just in case...

Bytes 6 - 9 in the binary file are hex FF FE C3 D1. That is the big
endian storage of the two's complement representation of the decimal
number -80943. So one way of getting that number is to just reverse
the order of the bytes and then call BitConverter.ToInt32. Once you
have it as an Integer you can simply assign it to a Single or Double
as you wish.


Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top