473,503 Members | 1,739 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Binary string (4) to Double

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
Mar 28 '07 #1
4 2552
>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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Mar 28 '07 #2
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
"Mattias Sjögren" <ma********************@mvps.orgwrote in message
news:uu****************@TK2MSFTNGP03.phx.gbl...
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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Mar 28 '07 #3
Freddy Coal wrote:
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
"Mattias Sjögren" <ma********************@mvps.orgwrote in message
news:uu****************@TK2MSFTNGP03.phx.gbl...
>>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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


--
Göran Andersson
_____
http://www.guffa.com
Mar 28 '07 #4
>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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Mar 30 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
9501
by: Yeow | last post by:
hello, i was trying to use the fread function on SunOS and ran into some trouble. i made a simple test as follows: i'm trying to read in a binary file (generated from a fortran code) that...
4
3666
by: knapak | last post by:
Hello I'm a self instructed amateur attempting to read a huge file from disk... so bear with me please... I just learned that reading a file in binary is faster than text. So I wrote the...
4
2115
by: Thomas Scheiderich | last post by:
The following code works fine. It opens file reads the data and then closes. **************************************************************************** **************** Dim objFileInfo As...
4
17111
by: David Buchan | last post by:
Hello, I wonder if anyone could help me. I'm using vb.NET and I'd like to read a binary file, byte by byte, and then write to another file (making a duplicate, identical file). I'd then...
8
8912
by: Mark | last post by:
Hello. I am attempting to write binary data from a file to an OLE Object field, and then write the file back out from the database. I am reading and writing the files in binary mode, and using...
23
2789
by: ShaneO | last post by:
Hello, I wish to extract embedded string data from a file using a Binary Read method. The following code sample is used in VB.NET and similar code is used in VB6 - (Assume variable...
3
2809
by: The Cool Giraffe | last post by:
Regarding the following code i have a problem. void read () { fstream file; ios::open_mode opMode = ios::in; file.open ("some.txt", opMode); char *ch = new char; vector <charv; while...
11
4260
by: David Mathog | last post by:
In the beginning (Kernighan & Ritchie 1978) there was fprintf, and unix write, but no fwrite. That is, no portable C method for writing binary data, only system calls which were OS specific. At...
22
11723
by: xiao | last post by:
Can I fullfill this task? Using fred and fwrite?
0
7087
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7281
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7334
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6993
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7462
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5579
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3156
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
737
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.