Class ByteUtils
Utility class containing methods to work with bytes and byte arrays and several data type conversions.
Inheritance
Inherited Members
Namespace: XBeeLibrary.Core.Utils
Assembly: XBeeLibrary.Core.dll
Syntax
public static class ByteUtils
Methods
ByteArrayToInt(Byte[])
Converts the given byte array (4 bytes length max) into an integer.
Declaration
public static int ByteArrayToInt(byte[] byteArray)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | byteArray | Byte array to convert to integer (4 bytes length max). |
Returns
Type | Description |
---|---|
System.Int32 | Converted integer value. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | If |
See Also
ByteArrayToLong(Byte[])
Converts the given byte array (8 bytes length max) into a long.
Declaration
public static long ByteArrayToLong(byte[] byteArray)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | byteArray | Byte array to convert to long (8 bytes length max). |
Returns
Type | Description |
---|---|
System.Int64 | Converted long value. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | If |
See Also
ByteArrayToShort(Byte[])
Converts the given byte array (2 bytes length max) to short.
Declaration
public static short ByteArrayToShort(byte[] byteArray)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | byteArray | Byte array to convert to short (2 bytes length max). |
Returns
Type | Description |
---|---|
System.Int16 | Converted short value. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | If |
See Also
ByteArrayToString(Byte[])
Converts the given byte array into a string.
Declaration
public static string ByteArrayToString(byte[] value)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | value | Byte array to convert to string. |
Returns
Type | Description |
---|---|
System.String | Converted string. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | If |
ByteToInt(Byte)
Converts the given byte into an integer.
Declaration
public static int ByteToInt(byte b)
Parameters
Type | Name | Description |
---|---|---|
System.Byte | b | Byte to convert to integer. |
Returns
Type | Description |
---|---|
System.Int32 | Converted byte into integer. |
IntToByteArray(Int32)
Converts the given integer value into a byte array.
Declaration
public static byte[] IntToByteArray(int value)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | value | Integer value to convert to byte array. |
Returns
Type | Description |
---|---|
System.Byte[] | Byte array of the given integer (4 bytes length). |
See Also
IsBitEnabled(Int32, Int32)
Returns whether the specified bit of the given integer is set to 1 or not.
Declaration
public static bool IsBitEnabled(int containerInteger, int bitPosition)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | containerInteger | Integer to check the given bit position enablement state. |
System.Int32 | bitPosition | Position of the bit to check its enablement state. |
Returns
Type | Description |
---|---|
System.Boolean |
|
LongToByteArray(Int64)
Converts the given long value into a byte array.
Declaration
public static byte[] LongToByteArray(long value)
Parameters
Type | Name | Description |
---|---|---|
System.Int64 | value | Long value to convert to byte array. |
Returns
Type | Description |
---|---|
System.Byte[] | Byte array of the given long value (8 bytes length). |
See Also
NewByteArray(Byte[], Int32)
Generates a new byte array of the given size using the given data and filling with ASCII zeros (0x48) the remaining space.
Declaration
public static byte[] NewByteArray(byte[] data, int finalSize)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | data | Data to use in the new array. |
System.Int32 | finalSize | Final size of the array. |
Returns
Type | Description |
---|---|
System.Byte[] | Final byte array of the given size containing the given data and replacing with zeros the remaining space. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | If |
ReadBooleanFromByte(Byte, Int32)
Reads a boolean value from the given byte at the given bit position.
Declaration
public static bool ReadBooleanFromByte(byte containerByte, int bitOffset)
Parameters
Type | Name | Description |
---|---|---|
System.Byte | containerByte | Byte to read boolean value from. |
System.Int32 | bitOffset | Offset inside the byte to read the boolean value. |
Returns
Type | Description |
---|---|
System.Boolean | The read boolean value. |
ReadBytes(Int32, Stream)
Reads the given amount of bytes from the given byte array input stream.
Declaration
public static Task<byte[]> ReadBytes(int numBytes, Stream inputStream)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | numBytes | Number of bytes to read. |
System.IO.Stream | inputStream | Byte array input stream to read bytes from. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<System.Byte[]> | An array with the read bytes. |
Exceptions
Type | Condition |
---|---|
System.ArgumentOutOfRangeException | If |
System.ArgumentException | If |
System.ArgumentNullException | If |
ReadIntegerFromByte(Byte, Int32, Int32)
Reads an integer value from the given byte using the given bit offset and the given bit size.
Declaration
public static int ReadIntegerFromByte(byte containerByte, int bitOffset, int bitLength)
Parameters
Type | Name | Description |
---|---|---|
System.Byte | containerByte | Byte to read the integer from. |
System.Int32 | bitOffset | Offset inside the byte to start reading integer value. |
System.Int32 | bitLength | Size in bits of the integer value to read. |
Returns
Type | Description |
---|---|
System.Int32 | The integer read value. |
ReadString(Stream)
Reads a null-terminated string from the given byte array input stream.
Declaration
public static string ReadString(Stream inputStream)
Parameters
Type | Name | Description |
---|---|---|
System.IO.Stream | inputStream | Byte array input stream to read string from. |
Returns
Type | Description |
---|---|
System.String | The read string from the given |
Exceptions
Type | Condition |
---|---|
System.ArgumentException | If |
System.ArgumentNullException | If |
ReadUntilCR(Stream)
Reads from the given byte array input stream until a CR character is found or the end of stream is reached. Read bytes are returned.
Declaration
public static byte[] ReadUntilCR(Stream inputStream)
Parameters
Type | Name | Description |
---|---|---|
System.IO.Stream | inputStream | Byte array input stream to read from. |
Returns
Type | Description |
---|---|
System.Byte[] | An array with the read bytes. |
Exceptions
Type | Condition |
---|---|
System.ArgumentException | If |
System.ArgumentNullException | If |
See Also
ShortToByteArray(Int16)
Converts the given short value into a byte array.
Declaration
public static byte[] ShortToByteArray(short value)
Parameters
Type | Name | Description |
---|---|---|
System.Int16 | value | Short value to convert to byte array. |
Returns
Type | Description |
---|---|
System.Byte[] | Byte array of the given short (2 bytes length). |
See Also
StringToByteArray(String)
Converts the given string into a byte array.
Declaration
public static byte[] StringToByteArray(string value)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | String to convert to byte array. |
Returns
Type | Description |
---|---|
System.Byte[] | Byte array of the given string. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | If |
See Also
SwapByteArray(Byte[])
Swaps the given byte array order.
Declaration
public static byte[] SwapByteArray(byte[] source)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | source | Byte array to swap. |
Returns
Type | Description |
---|---|
System.Byte[] | The swapped byte array. |