Using ByteArrays

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Anil Gupte

    #1

    Using ByteArrays

    I am learning how to read and write files. Text files were easy, but binary
    files have me confused. As per the tutorial, I used BinaryReader to read a
    file and put the contents into a ByteArray:

    Dim fsReadStream As New FileStream(MyFi leName, FileMode.Open,
    FileAccess.Read )
    Dim brReader As New BinaryReader(fs ReadStream)
    Dim ByteArray() As Byte
    While brReader.PeekCh ar() -1
    ByteArray = brReader.ReadBy tes(1)

    ' This is the part where I don't know how to do

    End While
    brReader.Close( )

    How do I actually use the data in that ByteArray. I have basically created
    a file containing some text, some images and some video (I plan to encrypt
    this later). Now I want to open the file and read it, then parse it, put
    the text into a label, put the image into a picturebox etc. Any tutorials
    you can point me to, or any information at all will be useful.

    Thanx,
    --
    Anil Gupte
    Innovate with UsLeading the Way in Advanced Technology Solutions Discover state-of-the-art services in logistics, databases, and AI designed for evolving

    www.icinema.com


  • Olie

    #2
    Re: Using ByteArrays

    It all depends on what type of data you want to read from the Binary
    file and the type of that data. The example you have quoted simply
    reads a single byte at a time out of the binary file which is not very
    usful and very inefficient if you want to read a whole string or image.

    I would recomend you look at some of the functions of BinaryReader as
    it is designed to perform the file parsing and can realy help you. I
    have to guess here as I do not know the format of your file but here is
    an example:

    UINT16 DataType
    UINT32 DataSize
    byte[] Data

    With this structure of data in the binary file I would call these
    functions on the BinaryReader object:

    DataType = brReader.ReadUI nt16()
    DataSize = brReader.ReadUI nt32()

    and assuming the data is a string

    string = brReader.ReadSt ring()

    or

    MyByteArray = brReader.ReadBy tes(DataSize)
    string = MyByteArray.ToS tring()

    You would obviously need to put in some checking to see when the end of
    the file has been reached.


    Anil Gupte wrote:
    I am learning how to read and write files. Text files were easy, but binary
    files have me confused. As per the tutorial, I used BinaryReader to read a
    file and put the contents into a ByteArray:
    >
    Dim fsReadStream As New FileStream(MyFi leName, FileMode.Open,
    FileAccess.Read )
    Dim brReader As New BinaryReader(fs ReadStream)
    Dim ByteArray() As Byte
    While brReader.PeekCh ar() -1
    ByteArray = brReader.ReadBy tes(1)
    >
    ' This is the part where I don't know how to do
    >
    End While
    brReader.Close( )
    >
    How do I actually use the data in that ByteArray. I have basically created
    a file containing some text, some images and some video (I plan to encrypt
    this later). Now I want to open the file and read it, then parse it, put
    the text into a label, put the image into a picturebox etc. Any tutorials
    you can point me to, or any information at all will be useful.
    >
    Thanx,
    --
    Anil Gupte
    Innovate with UsLeading the Way in Advanced Technology Solutions Discover state-of-the-art services in logistics, databases, and AI designed for evolving

    www.icinema.com

    Comment

    • Anil Gupte

      #3
      Re: Using ByteArrays

      Olie:

      I tried what you suggested and the string = brReader.ReadSt ring() worked
      fine for the first part of the file, because it was string data and I put it
      in a loop as below:

      While brReader.PeekCh ar() 31 And brReader.PeekCh ar() < 127
      strRead = strRead & brReader.ReadSt ring()
      Label1.Text = strRead
      End While
      However, the rest is not string data and I don't know how to treat it. As I
      mentioned in my original message, I am trying to create a composite file
      (like a package) consisting of text, pictures and other types of data (even
      a video). What I would ideally like is something like this:

      While brReader.PeekCh ar() -1 ' until EOF
      Read some data
      Figure out what is in it (Text, part of an image. whatever)
      Take the text part and put it in a label to display (as above)
      Store the binary part in a variable (or append if I already have some in
      there)
      ... and keep going in this loop till I reach the end of the binary part
      Put the binary part in a PictureBox (assuming for the moment it is an
      image)
      Back to the top of the loop to read some more data (could be more text
      or another picture or whatever)
      End While

      Of course, I am the creator of the file (I wrote another program to write
      these types of files), so I can put markers that tell me where the text ends
      and the image part starts etc.

      --
      Anil Gupte
      Innovate with UsLeading the Way in Advanced Technology Solutions Discover state-of-the-art services in logistics, databases, and AI designed for evolving

      www.icinema.com

      "Olie" <owaits@gmail.c omwrote in message
      news:1158310674 .072420.254240@ d34g2000cwd.goo glegroups.com.. .
      It all depends on what type of data you want to read from the Binary
      file and the type of that data. The example you have quoted simply
      reads a single byte at a time out of the binary file which is not very
      usful and very inefficient if you want to read a whole string or image.
      >
      I would recomend you look at some of the functions of BinaryReader as
      it is designed to perform the file parsing and can realy help you. I
      have to guess here as I do not know the format of your file but here is
      an example:
      >
      UINT16 DataType
      UINT32 DataSize
      byte[] Data
      >
      With this structure of data in the binary file I would call these
      functions on the BinaryReader object:
      >
      DataType = brReader.ReadUI nt16()
      DataSize = brReader.ReadUI nt32()
      >
      and assuming the data is a string
      >
      string = brReader.ReadSt ring()
      >
      or
      >
      MyByteArray = brReader.ReadBy tes(DataSize)
      string = MyByteArray.ToS tring()
      >
      You would obviously need to put in some checking to see when the end of
      the file has been reached.
      >
      >
      Anil Gupte wrote:
      >I am learning how to read and write files. Text files were easy, but
      >binary
      >files have me confused. As per the tutorial, I used BinaryReader to read
      >a
      >file and put the contents into a ByteArray:
      >>
      >Dim fsReadStream As New FileStream(MyFi leName, FileMode.Open,
      >FileAccess.Rea d)
      >Dim brReader As New BinaryReader(fs ReadStream)
      >Dim ByteArray() As Byte
      >While brReader.PeekCh ar() -1
      >ByteArray = brReader.ReadBy tes(1)
      >>
      >' This is the part where I don't know how to do
      >>
      >End While
      >brReader.Close ()
      >>
      >How do I actually use the data in that ByteArray. I have basically
      >created
      >a file containing some text, some images and some video (I plan to
      >encrypt
      >this later). Now I want to open the file and read it, then parse it, put
      >the text into a label, put the image into a picturebox etc. Any
      >tutorials
      >you can point me to, or any information at all will be useful.
      >>
      >Thanx,
      >--
      >Anil Gupte
      >www.keeninc.net
      >www.icinema.com
      >

      Comment

      • Anil Gupte

        #4
        Re: Using ByteArrays

        BTW, I forgot to mention:
        MyByteArray = brReader.ReadBy tes(DataSize)
        string = MyByteArray.ToS tring()

        does not work, because apparently there is no method called ToString for
        MyByteArray

        --
        Anil Gupte
        Innovate with UsLeading the Way in Advanced Technology Solutions Discover state-of-the-art services in logistics, databases, and AI designed for evolving

        www.icinema.com

        "Olie" <owaits@gmail.c omwrote in message
        news:1158310674 .072420.254240@ d34g2000cwd.goo glegroups.com.. .
        It all depends on what type of data you want to read from the Binary
        file and the type of that data. The example you have quoted simply
        reads a single byte at a time out of the binary file which is not very
        usful and very inefficient if you want to read a whole string or image.
        >
        I would recomend you look at some of the functions of BinaryReader as
        it is designed to perform the file parsing and can realy help you. I
        have to guess here as I do not know the format of your file but here is
        an example:
        >
        UINT16 DataType
        UINT32 DataSize
        byte[] Data
        >
        With this structure of data in the binary file I would call these
        functions on the BinaryReader object:
        >
        DataType = brReader.ReadUI nt16()
        DataSize = brReader.ReadUI nt32()
        >
        and assuming the data is a string
        >
        string = brReader.ReadSt ring()
        >
        or
        >
        MyByteArray = brReader.ReadBy tes(DataSize)
        string = MyByteArray.ToS tring()
        >
        You would obviously need to put in some checking to see when the end of
        the file has been reached.
        >
        >
        Anil Gupte wrote:
        >I am learning how to read and write files. Text files were easy, but
        >binary
        >files have me confused. As per the tutorial, I used BinaryReader to read
        >a
        >file and put the contents into a ByteArray:
        >>
        >Dim fsReadStream As New FileStream(MyFi leName, FileMode.Open,
        >FileAccess.Rea d)
        >Dim brReader As New BinaryReader(fs ReadStream)
        >Dim ByteArray() As Byte
        >While brReader.PeekCh ar() -1
        >ByteArray = brReader.ReadBy tes(1)
        >>
        >' This is the part where I don't know how to do
        >>
        >End While
        >brReader.Close ()
        >>
        >How do I actually use the data in that ByteArray. I have basically
        >created
        >a file containing some text, some images and some video (I plan to
        >encrypt
        >this later). Now I want to open the file and read it, then parse it, put
        >the text into a label, put the image into a picturebox etc. Any
        >tutorials
        >you can point me to, or any information at all will be useful.
        >>
        >Thanx,
        >--
        >Anil Gupte
        >www.keeninc.net
        >www.icinema.com
        >

        Comment

        • Olie

          #5
          Re: Using ByteArrays

          I explained in my previous post how to read a single UInt value out of
          the binary file. Just simply read out a value first to deternmine what
          type of data follows (string, picture, video). How you go about reading
          out the data depends on what variable you want to read the data to. In
          the case of a picture, the Bitmap constructor takes in a stream
          directly so you could just call the bitmap constructor with
          fsReadStream.

          Picture = New Bitmap(fsReadSt ream)

          I was guessing that there was a ToString() function on a byte array.
          The correct code to convert a byte array to a string is:

          Dim Encoder As New System.Text.UTF 7Encoding()
          Dim MyString As String = Encoder.GetStri ng(ByteArray)

          As for the video, I am not sure what to do with that and I think that
          reading a whole video into RAM is probably not the best idea. You could
          extract the video section to a seperate file and then just play the
          video from there.

          Anil Gupte wrote:
          Olie:
          >
          I tried what you suggested and the string = brReader.ReadSt ring() worked
          fine for the first part of the file, because it was string data and I put it
          in a loop as below:
          >
          While brReader.PeekCh ar() 31 And brReader.PeekCh ar() < 127
          strRead = strRead & brReader.ReadSt ring()
          Label1.Text = strRead
          End While
          However, the rest is not string data and I don't know how to treat it. As I
          mentioned in my original message, I am trying to create a composite file
          (like a package) consisting of text, pictures and other types of data (even
          a video). What I would ideally like is something like this:
          >
          While brReader.PeekCh ar() -1 ' until EOF
          Read some data
          Figure out what is in it (Text, part of an image. whatever)
          Take the text part and put it in a label to display (as above)
          Store the binary part in a variable (or append if I already have some in
          there)
          ... and keep going in this loop till I reach the end of the binary part
          Put the binary part in a PictureBox (assuming for the moment it is an
          image)
          Back to the top of the loop to read some more data (could be more text
          or another picture or whatever)
          End While
          >
          Of course, I am the creator of the file (I wrote another program to write
          these types of files), so I can put markers that tell me where the text ends
          and the image part starts etc.
          >
          --
          Anil Gupte
          Innovate with UsLeading the Way in Advanced Technology Solutions Discover state-of-the-art services in logistics, databases, and AI designed for evolving

          www.icinema.com
          >
          "Olie" <owaits@gmail.c omwrote in message
          news:1158310674 .072420.254240@ d34g2000cwd.goo glegroups.com.. .
          It all depends on what type of data you want to read from the Binary
          file and the type of that data. The example you have quoted simply
          reads a single byte at a time out of the binary file which is not very
          usful and very inefficient if you want to read a whole string or image.

          I would recomend you look at some of the functions of BinaryReader as
          it is designed to perform the file parsing and can realy help you. I
          have to guess here as I do not know the format of your file but here is
          an example:

          UINT16 DataType
          UINT32 DataSize
          byte[] Data

          With this structure of data in the binary file I would call these
          functions on the BinaryReader object:

          DataType = brReader.ReadUI nt16()
          DataSize = brReader.ReadUI nt32()

          and assuming the data is a string

          string = brReader.ReadSt ring()

          or

          MyByteArray = brReader.ReadBy tes(DataSize)
          string = MyByteArray.ToS tring()

          You would obviously need to put in some checking to see when the end of
          the file has been reached.


          Anil Gupte wrote:
          I am learning how to read and write files. Text files were easy, but
          binary
          files have me confused. As per the tutorial, I used BinaryReader to read
          a
          file and put the contents into a ByteArray:
          >
          Dim fsReadStream As New FileStream(MyFi leName, FileMode.Open,
          FileAccess.Read )
          Dim brReader As New BinaryReader(fs ReadStream)
          Dim ByteArray() As Byte
          While brReader.PeekCh ar() -1
          ByteArray = brReader.ReadBy tes(1)
          >
          ' This is the part where I don't know how to do
          >
          End While
          brReader.Close( )
          >
          How do I actually use the data in that ByteArray. I have basically
          created
          a file containing some text, some images and some video (I plan to
          encrypt
          this later). Now I want to open the file and read it, then parse it, put
          the text into a label, put the image into a picturebox etc. Any
          tutorials
          you can point me to, or any information at all will be useful.
          >
          Thanx,
          --
          Anil Gupte
          Innovate with UsLeading the Way in Advanced Technology Solutions Discover state-of-the-art services in logistics, databases, and AI designed for evolving

          www.icinema.com

          Comment

          • Anil Gupte

            #6
            Re: Using ByteArrays

            Thanx much, I will try it. Appreciate the help.

            --
            Anil Gupte
            Innovate with UsLeading the Way in Advanced Technology Solutions Discover state-of-the-art services in logistics, databases, and AI designed for evolving

            www.icinema.com

            "Olie" <owaits@gmail.c omwrote in message
            news:1158573703 .561672.147830@ i3g2000cwc.goog legroups.com...
            >I explained in my previous post how to read a single UInt value out of
            the binary file. Just simply read out a value first to deternmine what
            type of data follows (string, picture, video). How you go about reading
            out the data depends on what variable you want to read the data to. In
            the case of a picture, the Bitmap constructor takes in a stream
            directly so you could just call the bitmap constructor with
            fsReadStream.
            >
            Picture = New Bitmap(fsReadSt ream)
            >
            I was guessing that there was a ToString() function on a byte array.
            The correct code to convert a byte array to a string is:
            >
            Dim Encoder As New System.Text.UTF 7Encoding()
            Dim MyString As String = Encoder.GetStri ng(ByteArray)
            >
            As for the video, I am not sure what to do with that and I think that
            reading a whole video into RAM is probably not the best idea. You could
            extract the video section to a seperate file and then just play the
            video from there.
            >
            Anil Gupte wrote:
            >Olie:
            >>
            >I tried what you suggested and the string = brReader.ReadSt ring() worked
            >fine for the first part of the file, because it was string data and I put
            >it
            >in a loop as below:
            >>
            >While brReader.PeekCh ar() 31 And brReader.PeekCh ar() < 127
            >strRead = strRead & brReader.ReadSt ring()
            >Label1.Text = strRead
            >End While
            >However, the rest is not string data and I don't know how to treat it.
            >As I
            >mentioned in my original message, I am trying to create a composite file
            >(like a package) consisting of text, pictures and other types of data
            >(even
            >a video). What I would ideally like is something like this:
            >>
            >While brReader.PeekCh ar() -1 ' until EOF
            > Read some data
            > Figure out what is in it (Text, part of an image. whatever)
            > Take the text part and put it in a label to display (as above)
            > Store the binary part in a variable (or append if I already have some
            >in
            >there)
            > ... and keep going in this loop till I reach the end of the binary
            >part
            > Put the binary part in a PictureBox (assuming for the moment it is an
            >image)
            > Back to the top of the loop to read some more data (could be more
            >text
            >or another picture or whatever)
            >End While
            >>
            >Of course, I am the creator of the file (I wrote another program to write
            >these types of files), so I can put markers that tell me where the text
            >ends
            >and the image part starts etc.
            >>
            >--
            >Anil Gupte
            >www.keeninc.net
            >www.icinema.com
            >>
            >"Olie" <owaits@gmail.c omwrote in message
            >news:115831067 4.072420.254240 @d34g2000cwd.go oglegroups.com. ..
            It all depends on what type of data you want to read from the Binary
            file and the type of that data. The example you have quoted simply
            reads a single byte at a time out of the binary file which is not very
            usful and very inefficient if you want to read a whole string or image.
            >
            I would recomend you look at some of the functions of BinaryReader as
            it is designed to perform the file parsing and can realy help you. I
            have to guess here as I do not know the format of your file but here is
            an example:
            >
            UINT16 DataType
            UINT32 DataSize
            byte[] Data
            >
            With this structure of data in the binary file I would call these
            functions on the BinaryReader object:
            >
            DataType = brReader.ReadUI nt16()
            DataSize = brReader.ReadUI nt32()
            >
            and assuming the data is a string
            >
            string = brReader.ReadSt ring()
            >
            or
            >
            MyByteArray = brReader.ReadBy tes(DataSize)
            string = MyByteArray.ToS tring()
            >
            You would obviously need to put in some checking to see when the end of
            the file has been reached.
            >
            >
            Anil Gupte wrote:
            >I am learning how to read and write files. Text files were easy, but
            >binary
            >files have me confused. As per the tutorial, I used BinaryReader to
            >read
            >a
            >file and put the contents into a ByteArray:
            >>
            >Dim fsReadStream As New FileStream(MyFi leName, FileMode.Open,
            >FileAccess.Rea d)
            >Dim brReader As New BinaryReader(fs ReadStream)
            >Dim ByteArray() As Byte
            >While brReader.PeekCh ar() -1
            >ByteArray = brReader.ReadBy tes(1)
            >>
            >' This is the part where I don't know how to do
            >>
            >End While
            >brReader.Close ()
            >>
            >How do I actually use the data in that ByteArray. I have basically
            >created
            >a file containing some text, some images and some video (I plan to
            >encrypt
            >this later). Now I want to open the file and read it, then parse it,
            >put
            >the text into a label, put the image into a picturebox etc. Any
            >tutorials
            >you can point me to, or any information at all will be useful.
            >>
            >Thanx,
            >--
            >Anil Gupte
            >www.keeninc.net
            >www.icinema.com
            >
            >

            Comment

            Working...