c# - BinaryReader overwhelming me by padding byte array -


so have simple code reads file , spits data out in hex viewer fashion. here is:

using system; using system.collections.generic; using system.linq; using system.text; using system.io;  namespace hexviewer {     class program     {         static void main(string[] args)         {             binaryreader br = new binaryreader(new filestream("c:\\dump.bin", filemode.open));              (int = 0; < br.basestream.length; i+= 16)             {                 console.write(i.tostring("x") + ": ");                 byte[] data = new byte[16];                 br.read(data, i, 16);                 console.writeline(bitconverter.tostring(data).replace("-", " "));             }             console.readline();         }     } } 

the problem after first iteration, when do

br.read(data, 16, 16); 

the byte array padded 16 bytes, , filled data 15th byte 31st byte of file. because can't fit 32 bytes 16 byte large array, throws exception. can try code file larger 16 bytes. so, question is, wrong code?

just change br.read(data, i, 16); br.read(data, 0, 16);

you reading in new block of data each time, no need use i data buffer.

even better, change:

byte[] data = new byte[16]; br.read(data, 0, 16); 

to:

var data = br.readbytes(16); 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -