Dear All ,

The question is quite simple for all of you . I have a binary file . A description for this file says that :

"first 2 bytes in the header is the number of records"

I have opened this file and loaded it into bytes by this function

Public Shared Function ReadBinaryData(ByVal path As String) As Byte()

        ' Open the binary file.
        Dim streamBinary As New FileStream(path, FileMode.Open)

        ' Create a binary stream reader object.
        Dim readerInput As BinaryReader = New BinaryReader(streamBinary)

        ' Determine the number of bytes to read.
        Dim lengthFile As Integer = FileSize(path)

        ' Read the data in a byte array buffer.
        Dim inputData As Byte() = readerInput.ReadBytes(lengthFile)

        ' Close the file.
        streamBinary.Close()
        readerInput.Close()

        Return inputData

    End Function 'ReadBinaryData'

I need now to understand how to read the first 2 bytes and convert them to number.


Thanks,

Recommended Answers

All 4 Replies

Member Avatar for Unhnd_Exception
' Read the data in a byte array buffer.
        Dim inputData As Byte() = readerInput.ReadBytes(lengthFile)
        Dim mStream As New System.IO.MemoryStream(inputData)
        Dim bReader As New System.IO.BinaryReader(mStream)

        Dim TotalRecords = bReader.ReadInt16

        '
        'or
        '
        'TotalRecords = BitConverter.ToInt16(inputData, 0)

        ' Close the file.
        streamBinary.Close()
        readerInput.Close()

It is working , Amazing .
Thank you so much for your support.

Another question please .
How to convert 4 bytes to float as per the below example

fe 2b f0 3a ---> Speed in km/h, as float

how to do so .

Thanks,

Member Avatar for Unhnd_Exception

You can use the bit converter above or the binary reader.

BitConverter.ToSingle("your array", "the index")

assuming your hex data is in bytes

Thanks Unhnd , it works,

Another Quite length question , I have a binary file , the defincition of its content is as below L

Here is one of my 'log items' from the data.bin, all data is stored
in little endian (ie. least significant byte first)
11 63 39 46 --- Time, UTC in seconds since 1 Jan 1970.
01 00 --- 0001 = No Fix, 0002 = SPS
97 85 ff e0 7b db 4c 40 --- Latitude, as double
a1 d5 ce 56 8d 26 28 40 --- Longitude, as double
f0 37 e1 42 --- Height in meters, as float
fe 2b f0 3a --- Speed in km/h, as float
00 00 00 00 --- Heading (degrees ?), as float
01 00 --- RCR, log reason. 0001=Time, 0004=Distance
59 20 6a f3 4a 26 e3 3f --- Distance in meters, as double,
2a --- ? Don't know
a8 --- Checksum, xor of all bytes above not including 0x2a


the data from the Binary file "in HEX" is as below

"F25D39460200269652F5032445401F4228D79BCC54C09A3A2743B4ADE73F2A83"


I appreciate if you can support me to translate this data line based on the instruction before.


Thanks,

WSN

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.