Click here to Skip to main content
15,892,746 members
Home / Discussions / C#
   

C#

 
GeneralRe: (4/4)%2 = 1? wtf? Pin
leppie4-Mar-08 7:18
leppie4-Mar-08 7:18 
GeneralRe: (4/4)%2 = 1? wtf? Pin
jchalfant4-Mar-08 7:38
jchalfant4-Mar-08 7:38 
AnswerRe: (4/4)%2 = 1? wtf? Pin
Guffa4-Mar-08 8:13
Guffa4-Mar-08 8:13 
GeneralRe: (4/4)%2 = 1? wtf? Pin
jchalfant4-Mar-08 8:26
jchalfant4-Mar-08 8:26 
AnswerRe: (4/4)%2 = 1? wtf? Pin
Luc Pattyn4-Mar-08 13:13
sitebuilderLuc Pattyn4-Mar-08 13:13 
GeneralRe: (4/4)%2 = 1? wtf? Pin
jchalfant4-Mar-08 13:29
jchalfant4-Mar-08 13:29 
GeneralRe: (4/4)%2 = 1? wtf? Pin
Luc Pattyn4-Mar-08 14:07
sitebuilderLuc Pattyn4-Mar-08 14:07 
Generalreading a binary file Pin
ES Kim4-Mar-08 4:21
ES Kim4-Mar-08 4:21 
Hi,
I wrote a simple code to read a binary file.
It just shows contents of a file as hexadecimal digits.

<br />
using System;<br />
using System.IO;<br />
<br />
class Program<br />
{<br />
    static void Main(string[] args)<br />
    {<br />
        FileInfo fi = new FileInfo(args[0]);<br />
        BinaryReader br = new BinaryReader(fi.OpenRead());<br />
        int offset = 0;<br />
        while (br.PeekChar() != -1)  // exception thrown here<br />
        {<br />
            if (offset++ % 16 == 0)<br />
                Console.Write("\n{0:x8}:", offset);<br />
            Console.Write("{0,4:x2}", br.ReadByte());<br />
        }<br />
        Console.WriteLine();<br />
        br.Close();<br />
    }<br />
}<br />


And an exception is thrown at PeekChar() method showing messages as follows:

<br />
Unhandled exception: System.ArgumentException: Cannot include the encoded character because of insufficient buffer<br />
                  Encoding 'Unicode (UTF-8)' (fallback) 'System.Text.DecoderReplacementFallback'.<br />
Argumane name: chars<br />
   Location: System.Text.Encoding.ThrowCharsOverflow()<br />
   Location: System.Text.Encoding.ThrowCharsOverflow(DecoderNLS decoder, Boolean nothingDecoded)<br />
   Location: System.Text.UTF8Encoding.GetChars(Byte* bytes, Int32 byteCount, Char* chars, Int32 charCount, DecoderNLS baseDecoder)<br />
   Location: System.Text.DecoderNLS.GetChars(Byte* bytes, Int32 byteCount, Char* chars, Int32 charCount, Boolean flush)<br />
   Location: System.Text.DecoderNLS.GetChars(Byte[] bytes, Int32 byteIndex, Int32 byteCount, Char[] chars, Int32 charIndex, Boolean flush)<br />
   Location: System.Text.DecoderNLS.GetChars(Byte[] bytes, Int32 byteIndex, Int32 byteCount, Char[] chars, Int32 charIndex)<br />
   Location: System.IO.BinaryReader.InternalReadOneChar()<br />
   Location: System.IO.BinaryReader.Read()<br />
   Location: System.IO.BinaryReader.PeekChar()<br />
   Location: Program.Main(String[] args) File D:\CSharp\ConsoleApp\Program.cs:Line 11<br />


I'd like to know what's wrong with my code and what is the right way.
I'm using VS 2005 & .NET framework 2.0.50727
Thanks in advance.
AnswerRe: reading a binary file Pin
Eslam Afifi4-Mar-08 4:50
Eslam Afifi4-Mar-08 4:50 
GeneralAttempted to read and write protected memory… ActiveX on Vista Pin
anderslundsgard4-Mar-08 4:18
anderslundsgard4-Mar-08 4:18 
GeneralRe: Attempted to read and write protected memory… ActiveX on Vista Pin
leppie4-Mar-08 6:00
leppie4-Mar-08 6:00 
GeneralRe: Attempted to read and write protected memory… ActiveX on Vista Pin
Pete O'Hanlon4-Mar-08 10:08
mvePete O'Hanlon4-Mar-08 10:08 
GeneralRe: Attempted to read and write protected memory… ActiveX on Vista Pin
anderslundsgard4-Mar-08 19:59
anderslundsgard4-Mar-08 19:59 
GeneralRe: Attempted to read and write protected memory… ActiveX on Vista Pin
anderslundsgard4-Mar-08 20:10
anderslundsgard4-Mar-08 20:10 
GeneralRe: Attempted to read and write protected memory… ActiveX on Vista Pin
Pete O'Hanlon4-Mar-08 23:13
mvePete O'Hanlon4-Mar-08 23:13 
GeneralRe: Attempted to read and write protected memory… ActiveX on Vista Pin
anderslundsgard5-Mar-08 0:41
anderslundsgard5-Mar-08 0:41 
GeneralRe: Attempted to read and write protected memory… ActiveX on Vista Pin
mav.northwind5-Mar-08 3:21
mav.northwind5-Mar-08 3:21 
GeneralRe: Attempted to read and write protected memory… ActiveX on Vista Pin
anderslundsgard5-Mar-08 4:11
anderslundsgard5-Mar-08 4:11 
GeneralRe: Attempted to read and write protected memory… ActiveX on Vista Pin
Devil's Priest3-Jun-09 0:33
Devil's Priest3-Jun-09 0:33 
GeneralXML Element close tag appearing on new line Pin
Chazzysb4-Mar-08 4:12
Chazzysb4-Mar-08 4:12 
GeneralRe: XML Element close tag appearing on new line Pin
Pete O'Hanlon4-Mar-08 4:41
mvePete O'Hanlon4-Mar-08 4:41 
QuestionHelp in selecting attributes with XPath [modified] Pin
student_rhr4-Mar-08 4:02
student_rhr4-Mar-08 4:02 
GeneralRe: Help in selecting attributes with XPath Pin
Christian Graus4-Mar-08 9:22
protectorChristian Graus4-Mar-08 9:22 
QuestionAccess audio files from resources Pin
JustRonald4-Mar-08 4:00
JustRonald4-Mar-08 4:00 
AnswerRe: Access audio files from resources Pin
Eslam Afifi4-Mar-08 4:26
Eslam Afifi4-Mar-08 4:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.