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,

Dani AI

Generated

As @Unhnd pointed out, the general approach is correct: treat the first two bytes as a 16‑bit integer in the file’s byte order and then parse the rest field‑by‑field. A few practical notes before the decode below: make sure you respect endianness (the file is little‑endian; on typical x86/x64 Windows that matches BitConverter/BinaryReader, but if you ever run on a big‑endian platform you must reverse the byte order), and always verify the record-level checksum.

Interpreting your hex line

  • Hex: F2 5D 39 46 02 00 26 96 52 F5 03 24 45 40 1F 42 28 D7 9B CC 54 C0 9A 3A 27 43 B4 AD E7 3F 2A 83
  • Timestamp (bytes 0–3): 0x46395DF2 = 1,178,164,722 → 2007‑05‑03 03:58:42 UTC.
  • Fix (4–5): 0x0002 → SPS (valid fix).
  • Latitude (6–13, IEEE‑754 double, little‑endian): ≈ 42.281352018°
  • Longitude (14–21, double): ≈ −83.1970117°
  • Height (22–25, IEEE‑754 float): 167.2288818359375 m
  • Speed (26–29, float): ≈ 1.80999 km/h
  • Marker (30): 0x2A; Checksum (31): 0x83 — verified (XOR of bytes 0–29 = 0x83).

Quick checks / snippets

  • Convert Unix seconds to DateTime (VB.NET):
    Dim secs As Integer = BitConverter.ToInt32(bytes, 0) 
    Dim dt As DateTime = DateTimeOffset.FromUnixTimeSeconds(secs).UtcDateTime
  • Verify XOR checksum (VB.NET):
    Dim chk As Byte = 0
    For i As Integer = 0 To bytes.Length - 3  ' exclude marker + checksum
      chk = chk Xor bytes(i)
    Next
    Debug.Assert(chk = bytes(bytes.Length - 1))

Troubleshooting tips: double/float decoding must follow IEEE‑754 and file endianness; if values look wrong, reverse each multi‑byte field before conversion and recheck the checksum. If parsing many records, read record length first (or use a fixed stride) and validate each checksum as you go.

Recommended Answers

All 4 Replies

Member Avatar for Member #857553
' 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 Member #857553

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.