Suppose there is only one single double value written into a file in binary format. How can I read that value using C# or Java?
If I have to find a double value from a huge binary file, what techniques should I use to find that?
6 Answers
Double is 8 bytes. To read single double from binary file you can use BitConverter
class:
var fileContent = File.ReadAllBytes("C:\\1.bin");
double value = BitConverter.ToDouble(fileContent, 0);
If you need to read double from the middle of the file, replace 0 with the byte offset.
If you don't know the offset, you can't possibly tell that certain value in the byte array is double, integer or string.
Another approach is:
using (var fileStream = File.OpenRead("C:\\1.bin"))
using (var binaryReader = new BinaryReader(fileStream))
{
// fileStream.Seek(0, SeekOrigin.Begin); // uncomment this line and set offset if the double is in the middle of the file
var value = binaryReader.ReadDouble();
}
The second approach is better for big files, as it does not load whole file content to the memory.

- 76,499
- 26
- 155
- 134
-
@Barun - did you find the answer to your question? – Alex Aza Aug 12 '11 at 05:58
You can use the BinaryReader
class.
double value;
using( Stream stream = File.OpenRead(fileName) )
using( BinaryReader reader = new BinaryReader(stream) )
{
value = reader.ReadDouble();
}
For the second point, if you know the offset, simply use the Stream.Seek
method.

- 21,903
- 4
- 56
- 63
It seems that we would need to know how the double value was encoded in the file before we could go about finding it.

- 3,719
- 1
- 35
- 43
-
-
-
Sure, IEEE 754 is the standard encoding but the double value could be encoded in another form. How much do you know about the source file? As @AlexAza pointed out, unless you know the offset, you'll have a hard time knowing whether you're getting a "correct" double. If you don't know anything about the format of the file, I don't know if finding arbitrary double values is possible. – Timothy Lee Russell Jun 21 '11 at 04:07
-
-
Unfortunately, I think to get useful information out of the file, you're going to need to know how it was encoded as well as the format (i.e. how the encoded information is organized in the file). – Timothy Lee Russell Jun 21 '11 at 04:20
-
I would suggest contacting the WinPilot people and ask. If you're gracious, they will likely send you the format which will make your job a lot easier. – Timothy Lee Russell Jun 21 '11 at 04:57
1)
double theDouble;
using (Stream sr = new FileStream(@"C:\delme.dat", FileMode.Open, FileAccess.Read))
{
byte[] buffer = new byte[8];
sr.Read(buffer, 0, 8);
theDouble = BitConverter.ToDouble(buffer, 0);
}
2) You can't.

- 91,536
- 11
- 82
- 95
-
Cant I use bit shifting and create a 64 bits each time and read that value ? – Barun Jun 21 '11 at 04:02
-
If you know that the binary file contains only doubles and nothing else, then yes, but I thought your question is "I have a random binary file. How do I find a double somewhere inside?" – Petar Ivanov Jun 21 '11 at 04:04
-
-
No. But you can always read one byte and extract the bits using bit operators – Petar Ivanov Jun 21 '11 at 04:19
Here's how to read (and write for testing purposes) a double:
// Write Double
FileStream FS = new FileStream(@"C:\Test.bin", FileMode.Create);
BinaryWriter BW = new BinaryWriter(FS);
double Data = 123.456;
BW.Write(Data);
BW.Close();
// Read Double
FS = new FileStream(@"C:\Test.bin", FileMode.Open);
BinaryReader BR = new BinaryReader(FS);
Data = BR.ReadDouble();
BR.Close();
Getting it out of a large file depends on how the data is laid out in the file.

- 20,506
- 2
- 28
- 69
-
1
-
'Teaching' code is different than production code. In production code, I handle ALL errors and plan for ALL contingencies. In teaching code, I keep it as simple as possible to convey concepts. – Steve Wellens Jun 21 '11 at 14:54
-
Although error handling is another matter, I strongly believe following certain best practices in code samples like this is very important, so you don't teach people bad habits. The `using` keyword definitely falls under that category. Plus, imho `using` actually makes the code simpler in this case. – Sven Jun 21 '11 at 14:59
using (FileStream filestream = new FileStream(filename, FileMode.Open))
using (BinaryReader reader = new BinaryReader(filestream))
{
float x = reader.ReadSingle();
}

- 24,725
- 16
- 62
- 87