Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Read binary file header and data array in VB 2005

Read binary file header and data array in VB 2005

Scheduled Pinned Locked Moved Visual Basic
helpcsharpc++data-structures
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dave_6
    wrote on last edited by
    #1

    The files I read and write must be compatible with legacy programs (VB6, C++). The file is header information followed by a data array. For example; 'This is the current file header. Private Structure CvsImageStructure Friend FileID As Byte Friend Rows As Short Friend Columns As Short Friend Images As Short Friend StoredDataType As Byte Friend CompressionSlope As Single Friend CompressionIntercept As Single Friend SerialNumber As String Friend DefectDescriptions As String End Structure The next part of the file is a data array. It may be Byte, Int16 or Single. The StoredDataType element in the header tells me what the data type is. I can read the data one element at a time in a for next loop (i.e. BR.ReadSingle). However, this is painfully slow. In some cases there will be more than 1,000,000 elements in the array. Is there a way to quickly read the entire data array into a Single precision array (or Byte or Int16)? Some of these files were created in VB6 using the Put command i.e. Put #1, header Put #1, sImageData() The legacy program uses the VB6 Get command to read the files. It has to be able to read the files I create in VB.Net 2005. ----- When the DefectDescriptions element of the structure in the file is blank, the binaryreader returns 73 characters (BR.ReadString), reading into the data array. I have a work around for this, but would appreciate some help correcting this problem.

    D 1 Reply Last reply
    0
    • D Dave_6

      The files I read and write must be compatible with legacy programs (VB6, C++). The file is header information followed by a data array. For example; 'This is the current file header. Private Structure CvsImageStructure Friend FileID As Byte Friend Rows As Short Friend Columns As Short Friend Images As Short Friend StoredDataType As Byte Friend CompressionSlope As Single Friend CompressionIntercept As Single Friend SerialNumber As String Friend DefectDescriptions As String End Structure The next part of the file is a data array. It may be Byte, Int16 or Single. The StoredDataType element in the header tells me what the data type is. I can read the data one element at a time in a for next loop (i.e. BR.ReadSingle). However, this is painfully slow. In some cases there will be more than 1,000,000 elements in the array. Is there a way to quickly read the entire data array into a Single precision array (or Byte or Int16)? Some of these files were created in VB6 using the Put command i.e. Put #1, header Put #1, sImageData() The legacy program uses the VB6 Get command to read the files. It has to be able to read the files I create in VB.Net 2005. ----- When the DefectDescriptions element of the structure in the file is blank, the binaryreader returns 73 characters (BR.ReadString), reading into the data array. I have a work around for this, but would appreciate some help correcting this problem.

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      Dave_6 wrote:

      Is there a way to quickly read the entire data array into a Single precision array (or Byte or Int16)?

      Use ReadBytes to get the data into a Byte array, then use the BitConverter class to convert each set of bytes in the array to an Int16 or Single element in your array. A quick and dirty example... Read a file containing 1,000,000 Singles and convert it to an array:

      Using fs As New FileStream("C:\test.dat", FileMode.Open)
      Using br As New BinaryReader(fs)
      Dim buffer() As Byte
      buffer = br.ReadBytes(fs.Length)

          ' A Single is 4 bytes long...
          Dim singles(1000000) As Single
          For x As Integer = 0 to 1000000 Step 4
              sa(x \\ 4) = BitConverter.ToSingle(buffer, x)
          Next
      End Using
      

      End Using

      The nice part about this method is that you can modify it to conver the Byte array into any size values you've read by simply changing the Step value and using a different conversion method on the BitConverter class.

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
           2006, 2007

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups