0

I'm using a memory reader to read a file and put it in a MemoryStream. The MemoryStream is populated, and I'm trying to read with the BinaryReader because the endianness of the file change, so I use a Binary reader that can detect the endianness.

When I use BinaryReader.Read or BinaryReader.ReadString or BinaryReader.ReadByte, it reads the correct data, but when I use ReadInt16 or the other like that, I don't get the correct data. Is it possible to use those, I made a mistake, or should I make something to use Read() instead?

The code:

  MemoryStream ms = new MemoryStream();
  object[] binMakerNoteObj = exif.findTag(37500).data;
  byte[] binMakerNote = binMakerNoteObj.Cast<byte>().ToArray();
  ms.Write(binMakerNote, 0, binMakerNote.Length);
  ms.Position = 0;

  // Read the header
  string t = "";
  for(int i = 0; i < 6; i ++)
        t += fileStream.ReadChar();
  version = fileStream.ReadUInt16();
  unknow = fileStream.ReadUInt16();
  byteOrder = fileStream.ReadUInt16();
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Arimhan
  • 31
  • 1
  • 3
  • have you tried readallbytes? – MethodMan Oct 08 '14 at 18:48
  • Well I can use Read but I just want to use ReadUint16, I don't think readAllbtes will be useful. – Arimhan Oct 08 '14 at 18:50
  • If the file stores its data as big-endian, then ReadInt16 or ReadUint16 won't work correctly because they assume little-endian. – competent_tech Oct 08 '14 at 18:50
  • I know, that's why I want to use Readint16, it will do the work but it have nothing to do with the endian, just to function returning two different things – Arimhan Oct 08 '14 at 18:52
  • ReadInt16 (actually, any of the Read* methods that reads more than one byte) reads in little-endian order. You will need to swap the bytes in order to get the correct values. – allonym Oct 08 '14 at 19:30
  • See http://stackoverflow.com/questions/8620885/c-sharp-binary-reader-in-big-endian might help you. – Erti-Chris Eelmaa Oct 08 '14 at 21:12
  • That's not the problème, the readint16 read the correct data when on a filestreal but not a memorystream. – Arimhan Oct 09 '14 at 04:08
  • The first caracter are "System.byte[]" if i read the char with readint16 and I transform tem to char, si tjere is a problem somewhere. – Arimhan Oct 09 '14 at 04:10

0 Answers0