locked
Binary Reader Error (Reading char) RRS feed

  • Question

  • Hi, I am getting this error and I have no idea how to fix it.

     

    The output char buffer is too small to contain the decoded characters, encoding 'Unicode (UTF-8)' fallback 'System.Text.DecoderReplacementFallback'.

    Parameter name: chars

     

     

    This is my code:

     

    BinaryReader br = new BinaryReader(File.OpenRead(ofd.FileName));
            br.BaseStream.Position = 0x37E;
            if (br.ReadByte() == 0x01) listStartOffset = 0xD000;
            else if (br.ReadByte() == 0x02) listStartOffset = 0xE000;
            tableReaderOffset = listStartOffset;
            br.BaseStream.Position = listStartOffset;
            char[] firstFileHolder = br.ReadChars(28);
            string firstFile = new string(firstFileHolder);
            string fileName = String.Empty;
            while (true)
            {
              fileName = "";
              //The offset of the reader
              br.BaseStream.Position = tableReaderOffset;
              //Read FileName
              for (int i = 0; i <= 28; i++)
              {
                br.BaseStream.Position = tableReaderOffset + i;
                fileName += br.ReadChar().ToString();//<-----------The error is here
              }
    

    Any help would be appreciated, thanks.

    If you need any more info, just ask.

    Saturday, January 15, 2011 11:02 PM

Answers

  • An MSFT guy is in the thread below talking about this issue and then I see some people last year asking about this issue.

     

    http://social.msdn.microsoft.com/forums/en-US/clr/thread/7bd62472-b4ff-47c7-9bbd-7af9512d0301/

    http://www.johnsoer.com/blog/?p=493

    Why language are these charecters?  If just ascii for a workaround why not just read the byte and convert to char.  Could be a problem with invalid data.  Post your file somewhere and someone can debug for you.  If there invalid data it should become apparent like this.

          br.BaseStream.Position = tableReaderOffset + i;
    fileName += ((char)br.BaseStream.ReadByte()).ToString()

    • Marked as answer by Neddy Ren Monday, January 24, 2011 4:50 AM
    Sunday, January 16, 2011 3:06 AM
  • Sounds like the ofd file contains double byte characters.

    http://msdn.microsoft.com/en-us/library/system.io.binaryreader.readchar.aspx

     

    "If the ReadChar method attempts to read a surrogate character in the stream an exception will be raised ... If surrogate characters can be expected in the stream, use the ReadChars method instead"


    May we all make money in the sequel.
    • Marked as answer by Neddy Ren Monday, January 24, 2011 4:51 AM
    Sunday, January 16, 2011 3:47 AM

All replies

  • An MSFT guy is in the thread below talking about this issue and then I see some people last year asking about this issue.

     

    http://social.msdn.microsoft.com/forums/en-US/clr/thread/7bd62472-b4ff-47c7-9bbd-7af9512d0301/

    http://www.johnsoer.com/blog/?p=493

    Why language are these charecters?  If just ascii for a workaround why not just read the byte and convert to char.  Could be a problem with invalid data.  Post your file somewhere and someone can debug for you.  If there invalid data it should become apparent like this.

          br.BaseStream.Position = tableReaderOffset + i;
    fileName += ((char)br.BaseStream.ReadByte()).ToString()

    • Marked as answer by Neddy Ren Monday, January 24, 2011 4:50 AM
    Sunday, January 16, 2011 3:06 AM
  • Sounds like the ofd file contains double byte characters.

    http://msdn.microsoft.com/en-us/library/system.io.binaryreader.readchar.aspx

     

    "If the ReadChar method attempts to read a surrogate character in the stream an exception will be raised ... If surrogate characters can be expected in the stream, use the ReadChars method instead"


    May we all make money in the sequel.
    • Marked as answer by Neddy Ren Monday, January 24, 2011 4:51 AM
    Sunday, January 16, 2011 3:47 AM
  • Hi Experiment5X:

    Welcome to the MSDN Forum.

    I suggest you to see the page below first:
    http://msdn.microsoft.com/en-us/library/system.io.binaryreader.readchar(VS.71).aspx
    I think this may help you to analysis your error.

    As AdbElRaheim said, could you give us more infomation to help you to solve your issue. Such as writting a small demon and tell us how the binary format file you should read。。。。etc.

    If you have any conerns, please feel free to tell us.

    Best Regards

     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help. Welcome to the C# Forum! If you have any feedback, please tell us.
    Wednesday, January 19, 2011 9:54 AM