Answered Binary File Read

AussieBoy

Well-known member
Joined
Sep 7, 2020
Messages
78
Programming Experience
Beginner
Hi,
I need to read in values from a binary file.
The header is made up of.
4 bytes, 1 byte, 1 byte, 2 bytes, 4 bytes, 4 bytes, 4 bytes
I have a int32, byte, byte, inst16, int32, int32, int32.
Which should read as 484 5 30 1595434298 15 58.
(However, it doesn't at the moment)
I had started off with Python, but need to switch to C# for other reasons.

the python code looked like

with open(filename, "rb") as file:
buffer = file.read(20)
size, variation, objectType, pointIndex, startTime, sampleRate, count = struct.unpack('>IBBHiII', buffer)
print(size, variation, objectType, startTime, sampleRate, count)

The start on C# is below.

C#:
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;


namespace InsertHistoryCOMAPI
{
       class Program
       {
             static void Main(string[] args)
             {
                    // Specifing a file
                    string path = @"C:\DNP3File\XXXXXXXXXX";

                    // Calling the ReadAllBytes() function
                    byte[] readText = File.ReadAllBytes(path);

                    byte[] temp;

                    int[] values = new int[3];

                    temp = new byte[4];
                    Array.Copy(readText, 0, temp, 0, 4);
                    values[0] = BitConverter.ToInt32(temp, 0);
                    Console.WriteLine(values[0]);

                    temp = new byte[4];
                    Array.Copy(readText, 4, temp, 0, 1);
                    values[0] = BitConverter.ToInt32(temp, 0);
                    Console.WriteLine(temp);

                    temp = new byte[4];
                    Array.Copy(readText, 5, temp, 0, 1);
                    values[0] = BitConverter.ToInt32(temp, 0);
                    Console.WriteLine(temp);

                    temp = new byte[4];
                    Array.Copy(readText, 6, temp, 0, 2);
                    values[0] = BitConverter.ToInt16(temp, 0);
                    Console.WriteLine(values[0]);

                    temp = new byte[4];
                    Array.Copy(readText, 8, temp, 0, 4);
                    values[0] = BitConverter.ToInt32(temp, 0);
                    Console.WriteLine(values[0]);

                    temp = new byte[4];
                    Array.Copy(readText, 12, temp, 0, 4);
                    values[0] = BitConverter.ToInt32(temp, 0);
                    Console.WriteLine(values[0]);

                    temp = new byte[4];
                    Array.Copy(readText, 16, temp, 0, 4);
                    values[0] = BitConverter.ToInt32(temp, 0);
                    Console.WriteLine(values[0]);

                    Console.ReadLine();
             }
 
I thought,
ToSingle() Converts a specified value to a single-precision floating-point number?
And you'd be right, but if your method is supposed to return a Single/float, then shouldn't the method declaration specify that? Yours doesn't. Looks like a case of "copy, paste and forget to edit".
 
Back
Top Bottom