BinaryRead /BinaryWriter issue RRS feed

  • Question

  • Hey guys,

    I have some problems with the binaryreader and writer. I am trying to get the bytes of a f.e. a pdf file, store the byte in another file and then read it back out. It does some things I find strange...

               'BinaryReader. Get the file
                Dim fileContent As Byte() = Nothing
                Dim f As New FileInfo("c:\template.pdf")
                Dim utf8 As New UTF8Encoding()
                Using fileStream As Stream = f.OpenRead
                    Dim bin As New BinaryReader(fileStream, utf8)
                    fileContent = bin.ReadBytes(CInt(f.Length))
                End Using
                'BinaryWriter. Put it in another format
                Using binWriter As New BinaryWriter(IO.File.Open("c:\template.xxl", FileMode.Create))
                End Using

    Then I read the new file back out and use the bytes to create the same file again.

               Dim fc As Byte() = Nothing
                Dim readStream As FileStream
                readStream = New FileStream("c:\template.xxl", FileMode.Open)
                Dim readBinary As New BinaryReader(readStream, utf8)
                fc = readBinary.ReadBytes(CInt(readStream.Length))

    How come when I do some handling like converting the bytes to a string and back, I get another length and thus another kind a file..

                'byte to string
                Dim enc As New System.Text.UTF8Encoding()
                strContents = enc.GetString(fc)
                fc = enc.GetBytes(strContents)

    which then off course wont generate the template. Now I know that when I don't do the converting to string and back that it will work, but how come it doesn't ? :)

               Using bw As New BinaryWriter(IO.File.Open("c:\NewTemplate.pdf", FileMode.Create))
                    bw.Write(fc, 20, fileContent.Length)
                End Using

    thanks in advance
    • Edited by Today2012 Thursday, July 5, 2012 6:39 AM
    Thursday, July 5, 2012 6:38 AM

All replies

  • PDF is a binary file format and you are reading it with UTF-8 encoding, which makes sense for text files.

    What is it that you are trying to achieve?

    Thursday, July 5, 2012 6:59 AM
  • Hey Andreas,

    thanks for the response. I have already solved the problem myself.

    kind regards

    Thursday, July 5, 2012 8:21 PM
  • Hi today2012,

    Welcome to MSDN Forum,

    I’m glad to hear that you found a good idea to resolve your  problem.

    If you find out the root cause finally and share your solutions or experience here, it will be very beneficial for other community members who have similar  problems.

    Best regards,

    Tommy Zhi [MSFT]
    MSDN Community Support | Feedback to us

    Friday, July 6, 2012 2:04 AM
  • Did you know that the .Net base class library and related is system Object. 

    Some think it is the base of windows and therefore windows forms, but that is not the case. 

    However for your question is a very good forum where many specialist in .Net an VB answer your kind of questions.


    Friday, July 6, 2012 5:54 AM