Reading bytes from a file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • toddw607
    New Member
    • Feb 2007
    • 41

    #1

    Reading bytes from a file

    Hello,
    I am reading binary in from a graphic file and storing it in a Byte Array and rewriting the binary data to a text file. Give that the bytes look something like:
    ff ff f3 34 00 00 a3 f4
    00 00 d3 a2 ff ff f2 ad
    I am trying to capture four bytes at a time, place them into a variable and convert them into a readable value in Decimal using h = Val("&H" & someVariable). I then want to store these values into a separate text file to use to construct a transparent layer over the graphic (the above hex values are coordinates). Could anyone give me any ideas on how to start this project? Thank you so much.
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    You could just read 4 bytes at a time from the file stream?

    Or keep an index varriable to keep track of where you are and use Array.Copy to copy parts of you main byte[] into a byte[4]

    Have you looked into what the Bitmap/Image objects can do? They handle a lot of that kinda thing

    Comment

    • toddw607
      New Member
      • Feb 2007
      • 41

      #3
      Originally posted by Plater
      You could just read 4 bytes at a time from the file stream?

      Or keep an index varriable to keep track of where you are and use Array.Copy to copy parts of you main byte[] into a byte[4]

      Have you looked into what the Bitmap/Image objects can do? They handle a lot of that kinda thing

      Hi Plater,
      Thanks for the RE: The only problem with doing those two steps is the amount of loops I have to be able to get exactly what I want from the initial Hex file. The following is how the program stores the read-in values:
      Code:
             '---Loop to get Coords out of intial Byte Array and place into secondary Byte Array
       				 For k = 0 To count								   'Loops through entire binary CGM Byte Array
       					 If (bytes(k) = 164) Then
       						 If (bytes(k - 4) = 34) Then
       							 If (bytes(k - 6) = 0) Then
       								 For z = -19 To -12
       									 'Finds the positions where the Coords are located
       									 bv.Write(bytes(k + z))	'Writes the bytes in the positions to the bv file
       									 'Increments counter for next loop to keep counters seperated
       									 yCoor(coorCount) = (bytes(k + z))
       									 'writes Coords into yCoor Byte Array
       								 Next
       								
       							 Else
       								 For z = -17 To -10
       									 bv.Write(bytes(k + z))
       								 Next
       							 End If
       						 End If
       					 End If
       				 Next
      I'm not sure how I can read in at 4 bytes at a time using this logic and because I'm running through all the files within the folder one after another when I try to use a counter for this, the number is completely off, much too high. Any ideas on how I could get around this?

      Comment

      • toddw607
        New Member
        • Feb 2007
        • 41

        #4
        Originally posted by Plater
        You could just read 4 bytes at a time from the file stream?

        Or keep an index varriable to keep track of where you are and use Array.Copy to copy parts of you main byte[] into a byte[4]

        Have you looked into what the Bitmap/Image objects can do? They handle a lot of that kinda thing
        Hi again Plater,

        I tried to read in the file I wanted into that file but it wouldn't work. I looked up the reasoning why it wouldn't work and it seems that you cannot create a binary file then read it on the fly within the same program so I created a new one to read the file after all the files within the folder have been created. So my question is now, how to read the 8 bytes at a time inside the reading function. I've done this before with readers but it seems that the binaryReader works very differently.
        Overall what I want to do is read in 8 bytes at a time, convert those eight bytes to decimal, then output each decimal number to a text file on it's own line. I'm sure I know how to do all the rest but reading in 8 bytes with the Binary reader is giving me more problems than it should. I'm sure this is an easy fix but I'm just not getting it.

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          You could use a regular filestream's read method and read in byte[] at 8 bytes a time.
          Then use BitConverter to change to a decimal and output to your text file.

          Comment

          • toddw607
            New Member
            • Feb 2007
            • 41

            #6
            Originally posted by Plater
            You could use a regular filestream's read method and read in byte[] at 8 bytes a time.
            Then use BitConverter to change to a decimal and output to your text file.
            I've been trying to do what you said but a regular filestream reader will not accept the byte property which is what my file consists of. I am using the following code:
            Code:
             For i = 0 To br.BaseStream.Length() - 1
             					 nBytesRead = br.Read(ByteArray, 0, nBytes)
             					 bw.Write(nBytesRead)
             				 Next
            When I run this code, it places an 8 (which is what nBytes is assigned to) into a binary file, however it places a single 8 in the new binary file for every 8 bytes in the original file. For example, if there were 104 bytes in the original file then the new file will contain 13 number 8's, so I know it's working someway but I can't seen to obtain the correct data type to address it. Any thoughts?

            Comment

            Working...