Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java's ByteBuffer nearest counterpart in C#?

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?

like image 296
Verlix Avatar asked Feb 23 '14 19:02

Verlix


1 Answers

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.

like image 92
Erik van Brakel Avatar answered Oct 06 '22 18:10

Erik van Brakel