7

Currently looking to interface a Java and a C# application. In Java I can use getShort(), getFloat() etc, to get various different data types from the buffer.

In C# I am using a MemoryStream, but there is only a single get() function. Does anybody know of a datatype or even a class that would mimic this functionality?

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
Verlix
  • 71
  • 4

2 Answers2

3

You're looking for the BinaryReader class, which can read from any stream.

You can also use BitConverter, which operates directly on byte arrays.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
3

You can wrap your MemoryStream in a BinaryReader:

using(var reader = new BinaryReader(yourStream)) {
   int someInt = reader.ReadInt32();  
}

The BinaryReader can be found in the System.IO namespace.

See MSDN for details on which methods you can use. Keep in mind that the methods follow the pattern of Read + the CLR type. So ReadInt32() for int, ReadUInt16() for short, etc.

Erik van Brakel
  • 23,220
  • 2
  • 52
  • 66