Class ByteArrayProtocolExtensions
- Namespace
- Plugin.ByteArrays
- Assembly
- Plugin.ByteArrays.dll
Protocol-specific extensions for byte arrays including TLV parsing, framing, and checksums.
public static class ByteArrayProtocolExtensions
- Inheritance
-
ByteArrayProtocolExtensions
- Inherited Members
Methods
AddLengthPrefixedFrame(byte[])
Adds a length-prefixed frame to the data. Format: 2-byte length + data.
public static byte[] AddLengthPrefixedFrame(this byte[] data)
Parameters
data
byte[]The data to frame.
Returns
- byte[]
The length-prefixed frame.
AddSimpleFrame(byte[], byte, byte)
Adds a simple frame around the data with start/end markers.
public static byte[] AddSimpleFrame(this byte[] data, byte startMarker = 126, byte endMarker = 126)
Parameters
data
byte[]The data to frame.
startMarker
byteThe start marker byte (default 0x7E).
endMarker
byteThe end marker byte (default 0x7E).
Returns
- byte[]
The framed data.
AppendChecksum(byte[], Func<byte[], byte>)
Appends a checksum to the data.
public static byte[] AppendChecksum(this byte[] data, Func<byte[], byte> checksumFunction)
Parameters
data
byte[]The data to add checksum to.
checksumFunction
Func<byte[], byte>The function to calculate the checksum.
Returns
- byte[]
The data with checksum appended.
CalculateSimpleChecksum(byte[])
Calculates a simple 8-bit checksum (sum of all bytes).
public static byte CalculateSimpleChecksum(this byte[] data)
Parameters
data
byte[]The data to calculate checksum for.
Returns
- byte
The 8-bit checksum.
CalculateXorChecksum(byte[])
Calculates a XOR checksum of all bytes.
public static byte CalculateXorChecksum(this byte[] data)
Parameters
data
byte[]The data to calculate checksum for.
Returns
- byte
The XOR checksum.
CreateTlvRecord(byte, byte[])
Creates a TLV record as a byte array.
public static byte[] CreateTlvRecord(byte type, byte[] value)
Parameters
Returns
- byte[]
A byte array representing the TLV record.
ParseAllTlvRecords(byte[])
Parses all TLV records from the byte array.
public static IEnumerable<TlvRecord> ParseAllTlvRecords(this byte[] array)
Parameters
array
byte[]The byte array containing TLV data.
Returns
- IEnumerable<TlvRecord>
An enumerable of TLV records.
ParseTlvRecord(byte[], ref int)
Parses a TLV record from the byte array at the specified position. Format: 1-byte type + 2-byte length + variable-length value.
public static TlvRecord ParseTlvRecord(this byte[] array, ref int position)
Parameters
array
byte[]The byte array containing TLV data.
position
intThe position to start parsing from. Will be advanced by the TLV record size.
Returns
- TlvRecord
The parsed TLV record.
Exceptions
- ArgumentException
Thrown when the array doesn't contain enough data for a complete TLV record.
RemoveLengthPrefixedFrame(byte[])
Removes a length-prefixed frame from the data.
public static byte[] RemoveLengthPrefixedFrame(this byte[] framedData)
Parameters
framedData
byte[]The length-prefixed framed data.
Returns
- byte[]
The data without the length prefix.
Exceptions
- ArgumentException
Thrown when the frame format is invalid.
RemoveSimpleFrame(byte[], byte, byte)
Removes a simple frame from the data.
public static byte[] RemoveSimpleFrame(this byte[] framedData, byte startMarker = 126, byte endMarker = 126)
Parameters
framedData
byte[]The framed data.
startMarker
byteThe expected start marker byte (default 0x7E).
endMarker
byteThe expected end marker byte (default 0x7E).
Returns
- byte[]
The data without framing.
Exceptions
- ArgumentException
Thrown when frame markers are invalid.
ValidateChecksum(byte[], Func<byte[], byte>)
Validates data against its checksum.
public static bool ValidateChecksum(this byte[] dataWithChecksum, Func<byte[], byte> checksumFunction)
Parameters
dataWithChecksum
byte[]The data including the checksum at the end.
checksumFunction
Func<byte[], byte>The function to calculate the checksum.
Returns
- bool
True if the checksum is valid, false otherwise.