ReadUInt16 Method
ReadUInt16(Int32, Boolean)
Read the TLV at the current position, return the value as an unsigned short (a two-byte unsigned integer), and move the position to the byte beyond the current TLV.
public ushort ReadUInt16(int expectedTag, bool bigEndian = true)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | expectedTag | The tag that should be at the current position. |
System.Boolean | bigEndian | Specifies whether the result should be returned as big endian (true or no argument given) or little endian (false). |
Returns
The unsigned short value.
Exceptions
Type | Condition |
---|---|
TlvException | An unexpected error occurred while reading from the TLV. |
Remarks
The method will verify that the tag is expected. If it is, it will read the length, verify that it is two, then read and return that two-byte unsigned value.
If the length of the value is not 2 (even if it is 0 or 1), this method will not advance the reader and throw an exception.
The method will return the value in big endian (most significant byte of the short is taken from value[0]), unless the bigEndian argument is false. If it is false, the result is returned in little endian (least significant byte of the short is taken from value[0]). The bigEndian argument has a default of true. This means you can call this method and leave out the bigEndian argument, and the method will return the unsigned short in big endian format.
If the tag at the current position in the encoding is not what was given as the expectedTag, or if the tag and/or length make up an invalid encoding, or if there are not enough bytes left in the encoding to read the tag, length, and value, this method will not advance the reader and throw an exception.