C# BinaryReader ReadUInt16

Description

BinaryReader ReadUInt16 Reads a 2-byte unsigned integer from the current stream using little-endian encoding and advances the position of the stream by two bytes.

Syntax

BinaryReader.ReadUInt16 has the following syntax.


[CLSCompliantAttribute(false)]
public virtual ushort ReadUInt16()

Returns

BinaryReader.ReadUInt16 method returns A 2-byte unsigned integer read from this stream.

Example


using System;//from w w w .  j  a  v  a  2s .c  o m
using System.IO;

class ConsoleApplication
{
    const string fileName = "data.dat";

    static void Main()
    {
        using (BinaryWriter writer = new BinaryWriter(File.Open(fileName, FileMode.Create)))
        {
            writer.Write((UInt16)123);
        }
        using (BinaryReader reader = new BinaryReader(File.Open(fileName, FileMode.Open)))
        {
            Console.WriteLine(reader.ReadUInt16());
        }
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.IO »




BinaryReader
BinaryWriter
Directory
DirectoryInfo
DriveInfo
File
FileInfo
FileStream
MemoryStream
Path
StreamReader
StreamWriter
StringReader
StringWriter