Skip to content

Arrays

Function: ArrayToHexString

string ArrayToHexString(byte[] data)

Description:
Converts a byte array to a hex string representation.

Parameters:
- data: Byte array to convert.

Return Value:
Returns a string representation of the byte array in hexadecimal format.

Usage Example
1
2
3
byte[] sampleData = { 0x01, 0x0A, 0xFF };
string hexString = ArrayToHexString(sampleData);
PrintLine($"Converted array to hex: {hexString}"); // Converted array to hex: 010AFF

Function: HexStringToArray

byte[] HexStringToArray(string hex)

Description:
Converts a hex string to its byte array representation.

Parameters:
- hex: Hexadecimal string to convert.

Return Value:
Returns the corresponding byte array.

Usage Example
1
2
3
string sampleHex = "010AFF";
byte[] byteArray = HexStringToArray(sampleHex);
PrintLine($"Converted hex to array: {byteArray.Length} bytes.");

Function: ByteArrayToString

string ByteArrayToString(byte[] bytes)

Description:
Converts a byte array to its string representation.

Parameters:
- bytes: Byte array to convert.

Return Value:
Returns the string representation of the byte array.

Usage Example
1
2
3
byte[] sampleBytes = { 65, 66, 67 }; // ABC in ASCII
string result = ByteArrayToString(sampleBytes);
PrintLine($"Byte array to string: {result}");

Function: StringToByteArray

byte[] StringToByteArray(string text)

Description:
Converts a string to its byte array representation.

Parameters:
- text: String to convert.

Return Value:
Returns the byte array representation of the string.

Usage Example
1
2
3
string sampleText = "ABC";
byte[] resultBytes = StringToByteArray(sampleText);
PrintLine($"String to byte array: {resultBytes.Length} bytes.");

Function: SubArray

byte[] SubArray(byte[] array, int offset, int count = 0, bool reverse = false)

Description:
Extracts a sub-array from the given array starting from the specified offset.

Parameters:
- array: The source array. - offset: The starting position in the source array. - count: (Optional) The number of elements to extract. Defaults to 0. - reverse: (Optional) If set to true, the sub-array is reversed before returning. Defaults to false.

Return Value:
Returns the extracted sub-array.

Usage Example
1
2
3
byte[] sourceArray = { 1, 2, 3, 4, 5 };
byte[] subArray = SubArray(sourceArray, 1, 3);
PrintLine($"Extracted sub-array: {ArrayToHexString(subArray)}");

Function: MergeArrays

byte[] MergeArrays(params byte[][] arrs)

Description:
Merges multiple byte arrays into a single array.

Parameters:
- arrs: Arrays to merge.

Return Value:
Returns the merged array.

Usage Example
1
2
3
4
byte[] array1 = { 1, 2 };
byte[] array2 = { 3, 4 };
byte[] merged = MergeArrays(array1, array2);
PrintLine($"Merged arrays: {ArrayToHexString(merged)}");

Procedure: ReverseArray

void ReverseArray(byte[] arr)

Description:
Reverses the order of elements in the given byte array.

Parameters:
- arr: Array to reverse.

Usage Example
1
2
3
byte[] sampleArray = { 1, 2, 3, 4 };
ReverseArray(sampleArray);
PrintLine($"Reversed array: {ArrayToHexString(sampleArray)}"); // 04030201

Function: GenerateFilledArray

byte[] GenerateFilledArray(byte fill, int length)

Description:
Generates a byte array of the specified length, filled with the given byte value.

Parameters:
- fill: Byte value to fill the array with. - length: Length of the array to generate.

Return Value:
Returns the generated byte array.

Usage Example
byte[] filledArray = GenerateFilledArray(0xFF, 5);
PrintLine($"Generated filled array: {ArrayToHexString(filledArray)}");

Function: GetInt16FromArray

short GetInt16FromArray(byte[] dataBytes, int offset, bool reverse = false)

Description:
Extracts a 16-bit integer from the byte array starting from the specified offset.

Parameters:
- dataBytes: Source byte array. - offset: Offset from where to start the extraction. - reverse: (Optional) If set to true, the bytes are reversed before conversion.

Return Value:
Returns the extracted 16-bit integer.

Usage Example
1
2
3
byte[] data = { 0x01, 0x02, 0x03, 0x04 };
short result = GetInt16FromArray(data, 1); // Will convert 0x0203 to int16 value = 515
PrintLine($"Extracted 16-bit integer: {result}");

Function: GetUInt16FromArray

ushort GetUInt16FromArray(byte[] dataBytes, int offset, bool reverse = false)

Description:
Extracts an unsigned 16-bit integer from the byte array starting from the specified offset.

Parameters:
- dataBytes: Source byte array. - offset: Offset from where to start the extraction. - reverse: (Optional) If set to true, the bytes are reversed before conversion.

Return Value:
Returns the extracted unsigned 16-bit integer.

Usage Example
1
2
3
byte[] data = { 0x01, 0x02, 0x03, 0x04 };
ushort result = GetUInt16FromArray(data, 1);
PrintLine($"Extracted unsigned 16-bit integer: {result}");

Function: GetInt32FromArray

int GetInt32FromArray(byte[] dataBytes, int offset, bool reverse = false)

Description:
Extracts a 32-bit integer from the byte array starting from the specified offset.

Parameters:
- dataBytes: Source byte array. - offset: Offset from where to start the extraction. - reverse: (Optional) If set to true, the bytes are reversed before conversion.

Return Value:
Returns the extracted 32-bit integer.

Usage Example
1
2
3
byte[] data = { 0x01, 0x02, 0x03, 0x04 };
int result = GetInt32FromArray(data, 0); // Will convert 0x01020304 to int32 value = 16909060
PrintLine($"Extracted 32-bit integer: {result}");

Function: GetUInt32FromArray

uint GetUInt32FromArray(byte[] dataBytes, int offset, bool reverse = false)

Description:
Extracts an unsigned 32-bit integer from the byte array starting from the specified offset.

Parameters:
- dataBytes: Source byte array. - offset: Offset from where to start the extraction. - reverse: (Optional) If set to true, the bytes are reversed before conversion.

Return Value:
Returns the extracted unsigned 32-bit integer.

Usage Example
1
2
3
byte[] data = { 0x01, 0x02, 0x03, 0x04 };
uint result = GetUInt32FromArray(data, 0); 
PrintLine($"Extracted unsigned 32-bit integer: {result}");

Function: ApplyMaskedValues

bool ApplyMaskedValues(byte[] dataBytes, byte[] valueBytes, byte[] maskBytes, int offset = 0)

Description:
Applies masked values from the value byte array to the target data byte array based on the provided mask byte array.

Parameters:
- dataBytes: Target byte array to which the values will be applied. - valueBytes: Byte array containing values to apply. - maskBytes: Mask byte array that dictates which values should be applied. - offset: (Optional) Starting position in the target data byte array.

Return Value:
Returns true if the values were successfully applied, otherwise false.

Usage Example
1
2
3
4
5
byte[] data = { 0x00, 0x00 };
byte[] values = { 0x0A, 0x0B };
byte[] mask = { 0xFF, 0x00 };
bool applied = ApplyMaskedValues(data, values, mask);
PrintLine($"Values applied: {applied}, resulting data: {ArrayToHexString(data)}");

Function: ArrayCopy

bool ArrayCopy(byte[] source, int sourceOffset, byte[] destination, int destinationOffset, int count)
Description:
Copies a specified number of bytes from a source array starting at a particular offset to a destination array starting at a particular offset.

Parameters:

  • source (byte[]): The source array from which bytes will be copied.
  • sourceOffset (int): The zero-based byte offset in the source array at which copying begins.
  • destination (byte[]): The destination array into which the bytes will be copied.
  • destinationOffset (int): The zero-based byte offset in the destination array at which storing begins.
  • count (int): The number of bytes to be copied.

Return Value:
Returns true if the operation was successful; otherwise, false.

Example Usage:

To copy 5 bytes from sourceArray starting from offset 2 to destinationArray starting at offset 3:

1
2
3
byte[] sourceArray = new byte[] {1, 2, 3, 4, 5, 6, 7};
byte[] destinationArray = new byte[10];
bool isSuccess = ArrayCopy(sourceArray, 2, destinationArray, 3, 5);

After the above operation, destinationArray would have its values modified based on the bytes copied from sourceArray.