Class BytesUtil
Utility class for byte array operations and memory size conversions.
public static class BytesUtil
- Inheritance
-
BytesUtil
- Inherited Members
Methods
ConcatByteArray(IEnumerable<byte[]>)
Concatenates multiple byte arrays into a single byte array.
public static byte[] ConcatByteArray(this IEnumerable<byte[]> src_)
Parameters
src_
IEnumerable<byte[]>The source byte arrays to concatenate.
Returns
- byte[]
A single byte array containing all the bytes from the source arrays.
FromBytes<T>(byte[])
Converts a byte array to a structure.
public static T FromBytes<T>(byte[] arr) where T : new()
Parameters
arr
byte[]The byte array containing the structure data.
Returns
- T
The structure created from the byte array.
Type Parameters
T
The type of the structure to create.
GetMemorySizeValueUnit(long)
Gets the value and unit for a memory size in bytes.
public static (double val, string unit) GetMemorySizeValueUnit(long num)
Parameters
num
longThe size in bytes.
Returns
SplitByteArray(IEnumerable<byte>, int, bool)
Splits a byte array into multiple arrays of a specified size.
public static byte[][] SplitByteArray(this IEnumerable<byte> src_, int sliceSize, bool allowReferenceBySource = false)
Parameters
src_
IEnumerable<byte>The source byte enumerable to split.
sliceSize
intThe size of each slice.
allowReferenceBySource
boolIf true and the source array is smaller than or equal to the slice size, returns the source array directly.
Returns
- byte[][]
An array of byte arrays, each containing a slice of the original array.
ToBytes(BitArray)
Converts a BitArray to a byte array.
public static byte[] ToBytes(this BitArray bitArray)
Parameters
bitArray
BitArrayThe BitArray to convert.
Returns
- byte[]
A byte array representing the BitArray.
ToBytes<T>(T)
Converts a structure to a byte array.
public static byte[] ToBytes<T>(T str)
Parameters
str
TThe structure to convert.
Returns
- byte[]
A byte array containing the structure data.
Type Parameters
T
The type of the structure to convert.
ToLongByMemorySizeString(string)
Parses a memory size string (e.g., “10MB”, “2.5GB”) into a long value representing bytes.
public static long ToLongByMemorySizeString(this string memorySizeString)
Parameters
memorySizeString
stringThe memory size string to parse.
Returns
- long
The parsed value in bytes.
Exceptions
- FormatException
Thrown when the parsing fails.
ToMemorySizeString(int, string)
Converts an integer value representing bytes to a formatted memory size string.
public static string ToMemorySizeString(this int num, string format = "{0,6:###.00} {1,-2:##}")
Parameters
Returns
- string
A formatted string representing the memory size.
ToMemorySizeString(long, string)
Converts a long value representing bytes to a formatted memory size string.
public static string ToMemorySizeString(this long num, string format = "{0,6:###.00} {1,-2:##}")
Parameters
Returns
- string
A formatted string representing the memory size.
TryParseLongByMemorySizeString(string, out long)
Tries to parse a memory size string (e.g., “10MB”, “2.5GB”) into a long value representing bytes.
public static bool TryParseLongByMemorySizeString(this string memorySizeString, out long dst)
Parameters
memorySizeString
stringThe memory size string to parse.
dst
longWhen this method returns, contains the parsed value if the parsing succeeded, or long.MaxValue if the parsing failed.
Returns
- bool
true if the parsing succeeded; otherwise, false.