Read binary file header and data array in VB 2005
-
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.
-
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.
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