| | 1 | | namespace NLightning.Infrastructure.Converters; |
| | 2 | |
|
| | 3 | | public static class EndianBitConverter |
| | 4 | | { |
| | 5 | | #region GetBytesBE |
| | 6 | | /// <summary> |
| | 7 | | /// Converts a ulong to a byte array in big-endian order. |
| | 8 | | /// </summary> |
| | 9 | | /// <param name="value">The ulong to convert.</param> |
| | 10 | | /// <param name="trimToMinimumLenght">If true, the byte array will be trimmed to the minimum length.</param> |
| | 11 | | /// <returns>The byte array representation of the ulong.</returns> |
| | 12 | | /// <remarks>Trimming to minimum length is useful when the byte array is used in a context where the length is known |
| | 13 | | public static byte[] GetBytesBigEndian(ulong value, bool trimToMinimumLenght = false) |
| | 14 | | { |
| 136 | 15 | | var bytes = BitConverter.GetBytes(value); |
| 136 | 16 | | if (BitConverter.IsLittleEndian) |
| | 17 | | { |
| 136 | 18 | | Array.Reverse(bytes); |
| | 19 | | } |
| | 20 | |
|
| 136 | 21 | | if (!trimToMinimumLenght) |
| | 22 | | { |
| 128 | 23 | | return bytes; |
| | 24 | | } |
| | 25 | |
|
| 68 | 26 | | var firstNonZeroIndex = Array.FindIndex(bytes, b => b != 0); |
| 8 | 27 | | return firstNonZeroIndex == -1 ? [0] : bytes[firstNonZeroIndex..]; |
| | 28 | | } |
| | 29 | |
|
| | 30 | | /// <summary> |
| | 31 | | /// Converts a long to a byte array in big-endian order. |
| | 32 | | /// </summary> |
| | 33 | | /// <param name="value">The long to convert.</param> |
| | 34 | | /// <param name="trimToMinimumLenght">If true, the byte array will be trimmed to the minimum length.</param> |
| | 35 | | /// <returns>The byte array representation of the long.</returns> |
| | 36 | | /// <remarks>Trimming to minimum length is useful when the byte array is used in a context where the length is known |
| | 37 | | public static byte[] GetBytesBigEndian(long value, bool trimToMinimumLenght = false) |
| | 38 | | { |
| 80 | 39 | | var bytes = BitConverter.GetBytes(value); |
| 80 | 40 | | if (BitConverter.IsLittleEndian) |
| | 41 | | { |
| 80 | 42 | | Array.Reverse(bytes); |
| | 43 | | } |
| | 44 | |
|
| 80 | 45 | | if (!trimToMinimumLenght) |
| | 46 | | { |
| 72 | 47 | | return bytes; |
| | 48 | | } |
| | 49 | |
|
| 68 | 50 | | var firstNonZeroIndex = Array.FindIndex(bytes, b => b != 0); |
| 8 | 51 | | return firstNonZeroIndex == -1 ? [0] : bytes[firstNonZeroIndex..]; |
| | 52 | | } |
| | 53 | |
|
| | 54 | | /// <summary> |
| | 55 | | /// Converts a uint to a byte array in big-endian order. |
| | 56 | | /// </summary> |
| | 57 | | /// <param name="value">The uint to convert.</param> |
| | 58 | | /// <param name="trimToMinimumLenght">If true, the byte array will be trimmed to the minimum length.</param> |
| | 59 | | /// <returns>The byte array representation of the uint.</returns> |
| | 60 | | /// <remarks>Trimming to minimum length is useful when the byte array is used in a context where the length is known |
| | 61 | | public static byte[] GetBytesBigEndian(uint value, bool trimToMinimumLenght = false) |
| | 62 | | { |
| 92 | 63 | | var bytes = BitConverter.GetBytes(value); |
| 92 | 64 | | if (BitConverter.IsLittleEndian) |
| | 65 | | { |
| 92 | 66 | | Array.Reverse(bytes); |
| | 67 | | } |
| | 68 | |
|
| 92 | 69 | | if (!trimToMinimumLenght) |
| | 70 | | { |
| 84 | 71 | | return bytes; |
| | 72 | | } |
| | 73 | |
|
| 36 | 74 | | var firstNonZeroIndex = Array.FindIndex(bytes, b => b != 0); |
| 8 | 75 | | return firstNonZeroIndex == -1 ? [0] : bytes[firstNonZeroIndex..]; |
| | 76 | | } |
| | 77 | |
|
| | 78 | | /// <summary> |
| | 79 | | /// Converts a int to a byte array in big-endian order. |
| | 80 | | /// </summary> |
| | 81 | | /// <param name="value">The int to convert.</param> |
| | 82 | | /// <param name="trimToMinimumLenght">If true, the byte array will be trimmed to the minimum length.</param> |
| | 83 | | /// <returns>The byte array representation of the int.</returns> |
| | 84 | | /// <remarks>Trimming to minimum length is useful when the byte array is used in a context where the length is known |
| | 85 | | public static byte[] GetBytesBigEndian(int value, bool trimToMinimumLenght = false) |
| | 86 | | { |
| 12 | 87 | | var bytes = BitConverter.GetBytes(value); |
| 12 | 88 | | if (BitConverter.IsLittleEndian) |
| | 89 | | { |
| 12 | 90 | | Array.Reverse(bytes); |
| | 91 | | } |
| | 92 | |
|
| 12 | 93 | | if (!trimToMinimumLenght) |
| | 94 | | { |
| 4 | 95 | | return bytes; |
| | 96 | | } |
| | 97 | |
|
| 36 | 98 | | var firstNonZeroIndex = Array.FindIndex(bytes, b => b != 0); |
| 8 | 99 | | return firstNonZeroIndex == -1 ? [0] : bytes[firstNonZeroIndex..]; |
| | 100 | | } |
| | 101 | |
|
| | 102 | | /// <summary> |
| | 103 | | /// Converts a ushort to a byte array in big-endian order. |
| | 104 | | /// </summary> |
| | 105 | | /// <param name="value">The ushort to convert.</param> |
| | 106 | | /// <param name="trimToMinimumLenght">If true, the byte array will be trimmed to the minimum length.</param> |
| | 107 | | /// <returns>The byte array representation of the ushort.</returns> |
| | 108 | | /// <remarks>Trimming to minimum length is useful when the byte array is used in a context where the length is known |
| | 109 | | public static byte[] GetBytesBigEndian(ushort value, bool trimToMinimumLenght = false) |
| | 110 | | { |
| 252 | 111 | | var bytes = BitConverter.GetBytes(value); |
| 252 | 112 | | if (BitConverter.IsLittleEndian) |
| | 113 | | { |
| 252 | 114 | | Array.Reverse(bytes); |
| | 115 | | } |
| | 116 | |
|
| 252 | 117 | | if (!trimToMinimumLenght) |
| | 118 | | { |
| 244 | 119 | | return bytes; |
| | 120 | | } |
| | 121 | |
|
| 24 | 122 | | var firstNonZeroIndex = Array.FindIndex(bytes, b => b != 0); |
| 8 | 123 | | return firstNonZeroIndex == -1 ? [0] : bytes[firstNonZeroIndex..]; |
| | 124 | | } |
| | 125 | |
|
| | 126 | | /// <summary> |
| | 127 | | /// Converts a short to a byte array in big-endian order. |
| | 128 | | /// </summary> |
| | 129 | | /// <param name="value">The short to convert.</param> |
| | 130 | | /// <param name="trimToMinimumLenght">If true, the byte array will be trimmed to the minimum length.</param> |
| | 131 | | /// <returns>The byte array representation of the short.</returns> |
| | 132 | | /// <remarks>Trimming to minimum length is useful when the byte array is used in a context where the length is known |
| | 133 | | public static byte[] GetBytesBigEndian(short value, bool trimToMinimumLenght = false) |
| | 134 | | { |
| 12 | 135 | | var bytes = BitConverter.GetBytes(value); |
| 12 | 136 | | if (BitConverter.IsLittleEndian) |
| | 137 | | { |
| 12 | 138 | | Array.Reverse(bytes); |
| | 139 | | } |
| | 140 | |
|
| 12 | 141 | | if (!trimToMinimumLenght) |
| | 142 | | { |
| 4 | 143 | | return bytes; |
| | 144 | | } |
| | 145 | |
|
| 24 | 146 | | var firstNonZeroIndex = Array.FindIndex(bytes, b => b != 0); |
| 8 | 147 | | return firstNonZeroIndex == -1 ? [0] : bytes[firstNonZeroIndex..]; |
| | 148 | | } |
| | 149 | | #endregion |
| | 150 | |
|
| | 151 | | #region GetBytesLE |
| | 152 | | /// <summary> |
| | 153 | | /// Converts a ulong to a byte array in little-endian order. |
| | 154 | | /// </summary> |
| | 155 | | /// <param name="value">The ulong to convert.</param> |
| | 156 | | /// <param name="trimToMinimumLenght">If true, the byte array will be trimmed to the minimum length.</param> |
| | 157 | | /// <returns>The byte array representation of the ulong.</returns> |
| | 158 | | /// <remarks>Trimming to minimum length is useful when the byte array is used in a context where the length is known |
| | 159 | | public static byte[] GetBytesLittleEndian(ulong value, bool trimToMinimumLenght = false) |
| | 160 | | { |
| 24 | 161 | | var bytes = BitConverter.GetBytes(value); |
| 24 | 162 | | if (!BitConverter.IsLittleEndian) |
| | 163 | | { |
| 0 | 164 | | Array.Reverse(bytes); |
| | 165 | | } |
| | 166 | |
|
| 24 | 167 | | if (!trimToMinimumLenght) |
| | 168 | | { |
| 8 | 169 | | return bytes; |
| | 170 | | } |
| | 171 | |
|
| 88 | 172 | | var firstNonZeroIndex = Array.FindIndex(bytes, b => b != 0); |
| 16 | 173 | | return firstNonZeroIndex == -1 ? [0] : bytes[firstNonZeroIndex..]; |
| | 174 | | } |
| | 175 | |
|
| | 176 | | /// <summary> |
| | 177 | | /// Converts a uint to a byte array in little-endian order. |
| | 178 | | /// </summary> |
| | 179 | | /// <param name="value">The uint to convert.</param> |
| | 180 | | /// <param name="trimToMinimumLenght">If true, the byte array will be trimmed to the minimum length.</param> |
| | 181 | | /// <returns>The byte array representation of the uint.</returns> |
| | 182 | | /// <remarks>Trimming to minimum length is useful when the byte array is used in a context where the length is known |
| | 183 | | public static byte[] GetBytesLittleEndian(uint value, bool trimToMinimumLenght = false) |
| | 184 | | { |
| 12 | 185 | | var bytes = BitConverter.GetBytes(value); |
| 12 | 186 | | if (!BitConverter.IsLittleEndian) |
| | 187 | | { |
| 0 | 188 | | Array.Reverse(bytes); |
| | 189 | | } |
| | 190 | |
|
| 12 | 191 | | if (!trimToMinimumLenght) |
| | 192 | | { |
| 4 | 193 | | return bytes; |
| | 194 | | } |
| | 195 | |
|
| 28 | 196 | | var firstNonZeroIndex = Array.FindIndex(bytes, b => b != 0); |
| 8 | 197 | | return firstNonZeroIndex == -1 ? [0] : bytes[firstNonZeroIndex..]; |
| | 198 | | } |
| | 199 | |
|
| | 200 | | /// <summary> |
| | 201 | | /// Converts a ushort to a byte array in little-endian order. |
| | 202 | | /// </summary> |
| | 203 | | /// <param name="value">The ushort to convert.</param> |
| | 204 | | /// <param name="trimToMinimumLenght">If true, the byte array will be trimmed to the minimum length.</param> |
| | 205 | | /// <returns>The byte array representation of the ushort.</returns> |
| | 206 | | /// <remarks>Trimming to minimum length is useful when the byte array is used in a context where the length is known |
| | 207 | | public static byte[] GetBytesLittleEndian(ushort value, bool trimToMinimumLenght = false) |
| | 208 | | { |
| 24 | 209 | | var bytes = BitConverter.GetBytes(value); |
| 24 | 210 | | if (!BitConverter.IsLittleEndian) |
| | 211 | | { |
| 0 | 212 | | Array.Reverse(bytes); |
| | 213 | | } |
| | 214 | |
|
| 24 | 215 | | if (!trimToMinimumLenght) |
| | 216 | | { |
| 8 | 217 | | return bytes; |
| | 218 | | } |
| | 219 | |
|
| 40 | 220 | | var firstNonZeroIndex = Array.FindIndex(bytes, b => b != 0); |
| 16 | 221 | | return firstNonZeroIndex == -1 ? [0] : bytes[firstNonZeroIndex..]; |
| | 222 | | } |
| | 223 | | #endregion |
| | 224 | |
|
| | 225 | | #region Back From LE Bytes |
| | 226 | | /// <summary> |
| | 227 | | /// Converts a byte array to an ulong in little-endian order. |
| | 228 | | /// </summary> |
| | 229 | | /// <param name="bytes">The byte array to convert.</param> |
| | 230 | | /// <param name="padWithZero">If true, the byte array will be padded with zero if the length is less than 8.</param> |
| | 231 | | /// <returns>The ulong representation of the byte array.</returns> |
| | 232 | | /// <remarks>Padding with zero is useful when the byte array is used in a context where the length is known to be le |
| | 233 | | public static ulong ToUInt64LittleEndian(ReadOnlySpan<byte> bytes, bool padWithZero = false) |
| | 234 | | { |
| | 235 | | // pad with zero if the length is less than 8 |
| 16 | 236 | | var paddedBytes = bytes.ToArray(); |
| 16 | 237 | | if (padWithZero && bytes.Length < 8) |
| | 238 | | { |
| 0 | 239 | | paddedBytes = new byte[8]; |
| 0 | 240 | | bytes.CopyTo(paddedBytes.AsSpan()[(8 - bytes.Length)..]); |
| | 241 | | } |
| | 242 | |
|
| 16 | 243 | | if (!BitConverter.IsLittleEndian) |
| | 244 | | { |
| 0 | 245 | | Array.Reverse(paddedBytes); |
| | 246 | | } |
| 16 | 247 | | return BitConverter.ToUInt64(paddedBytes, 0); |
| | 248 | | } |
| | 249 | |
|
| | 250 | | /// <summary> |
| | 251 | | /// Converts a byte array to a ulong in little-endian order. |
| | 252 | | /// </summary> |
| | 253 | | /// <param name="bytes">The byte array to convert.</param> |
| | 254 | | /// <param name="padWithZero">If true, the byte array will be padded with zero if the length is less than 8.</param> |
| | 255 | | /// <returns>The ulong representation of the byte array.</returns> |
| | 256 | | /// <remarks>Padding with zero is useful when the byte array is used in a context where the length is known to be le |
| | 257 | | public static long ToInt64LittleEndian(ReadOnlySpan<byte> bytes, bool padWithZero = false) |
| | 258 | | { |
| | 259 | | // pad with zero if the length is less than 8 |
| 16 | 260 | | var paddedBytes = bytes.ToArray(); |
| 16 | 261 | | if (padWithZero && bytes.Length < 8) |
| | 262 | | { |
| 0 | 263 | | paddedBytes = new byte[8]; |
| 0 | 264 | | bytes.CopyTo(paddedBytes.AsSpan()[(8 - bytes.Length)..]); |
| | 265 | | } |
| | 266 | |
|
| 16 | 267 | | if (!BitConverter.IsLittleEndian) |
| | 268 | | { |
| 0 | 269 | | Array.Reverse(paddedBytes); |
| | 270 | | } |
| 16 | 271 | | return BitConverter.ToInt64(paddedBytes, 0); |
| | 272 | | } |
| | 273 | |
|
| | 274 | | /// <summary> |
| | 275 | | /// Converts a byte array to a uint in little-endian order. |
| | 276 | | /// </summary> |
| | 277 | | /// <param name="bytes">The byte array to convert.</param> |
| | 278 | | /// <param name="padWithZero">If true, the byte array will be padded with zero if the length is less than 4.</param> |
| | 279 | | /// <returns>The uint representation of the byte array.</returns> |
| | 280 | | /// <remarks>Padding with zero is useful when the byte array is used in a context where the length is known to be le |
| | 281 | | public static uint ToUInt32LittleEndian(ReadOnlySpan<byte> bytes, bool padWithZero = false) |
| | 282 | | { |
| | 283 | | // pad with zero if the length is less than 4 |
| 16 | 284 | | var paddedBytes = bytes.ToArray(); |
| 16 | 285 | | if (padWithZero && bytes.Length < 4) |
| | 286 | | { |
| 0 | 287 | | paddedBytes = new byte[4]; |
| 0 | 288 | | bytes.CopyTo(paddedBytes.AsSpan()[(4 - bytes.Length)..]); |
| | 289 | | } |
| | 290 | |
|
| 16 | 291 | | if (!BitConverter.IsLittleEndian) |
| | 292 | | { |
| 0 | 293 | | Array.Reverse(paddedBytes); |
| | 294 | | } |
| 16 | 295 | | return BitConverter.ToUInt32(paddedBytes, 0); |
| | 296 | | } |
| | 297 | |
|
| | 298 | | /// <summary> |
| | 299 | | /// Converts a byte array to a uint in little-endian order. |
| | 300 | | /// </summary> |
| | 301 | | /// <param name="bytes">The byte array to convert.</param> |
| | 302 | | /// <param name="padWithZero">If true, the byte array will be padded with zero if the length is less than 4.</param> |
| | 303 | | /// <returns>The uint representation of the byte array.</returns> |
| | 304 | | /// <remarks>Padding with zero is useful when the byte array is used in a context where the length is known to be le |
| | 305 | | public static int ToInt32LittleEndian(ReadOnlySpan<byte> bytes, bool padWithZero = false) |
| | 306 | | { |
| | 307 | | // pad with zero if the length is less than 4 |
| 16 | 308 | | var paddedBytes = bytes.ToArray(); |
| 16 | 309 | | if (padWithZero && bytes.Length < 4) |
| | 310 | | { |
| 0 | 311 | | paddedBytes = new byte[4]; |
| 0 | 312 | | bytes.CopyTo(paddedBytes.AsSpan()[(4 - bytes.Length)..]); |
| | 313 | | } |
| | 314 | |
|
| 16 | 315 | | if (!BitConverter.IsLittleEndian) |
| | 316 | | { |
| 0 | 317 | | Array.Reverse(paddedBytes); |
| | 318 | | } |
| 16 | 319 | | return BitConverter.ToInt32(paddedBytes, 0); |
| | 320 | | } |
| | 321 | |
|
| | 322 | | /// <summary> |
| | 323 | | /// Converts a byte array to a ushort in little-endian order. |
| | 324 | | /// </summary> |
| | 325 | | /// <param name="bytes">The byte array to convert.</param> |
| | 326 | | /// <param name="padWithZero">If true, the byte array will be padded with zero if the length is less than 2.</param> |
| | 327 | | /// <returns>The ushort representation of the byte array.</returns> |
| | 328 | | /// <remarks>Padding with zero is useful when the byte array is used in a context where the length is known to be le |
| | 329 | | public static ushort ToUInt16LittleEndian(ReadOnlySpan<byte> bytes, bool padWithZero = false) |
| | 330 | | { |
| | 331 | | // pad with zero if the length is less than 2 |
| 16 | 332 | | var paddedBytes = bytes.ToArray(); |
| 16 | 333 | | if (padWithZero && bytes.Length < 2) |
| | 334 | | { |
| 0 | 335 | | paddedBytes = new byte[2]; |
| 0 | 336 | | bytes.CopyTo(paddedBytes.AsSpan()[(2 - bytes.Length)..]); |
| | 337 | | } |
| | 338 | |
|
| 16 | 339 | | if (!BitConverter.IsLittleEndian) |
| | 340 | | { |
| 0 | 341 | | Array.Reverse(paddedBytes); |
| | 342 | | } |
| 16 | 343 | | return BitConverter.ToUInt16(paddedBytes, 0); |
| | 344 | | } |
| | 345 | |
|
| | 346 | | /// <summary> |
| | 347 | | /// Converts a byte array to a ushort in little-endian order. |
| | 348 | | /// </summary> |
| | 349 | | /// <param name="bytes">The byte array to convert.</param> |
| | 350 | | /// <param name="padWithZero">If true, the byte array will be padded with zero if the length is less than 2.</param> |
| | 351 | | /// <returns>The ushort representation of the byte array.</returns> |
| | 352 | | /// <remarks>Padding with zero is useful when the byte array is used in a context where the length is known to be le |
| | 353 | | public static short ToInt16LittleEndian(ReadOnlySpan<byte> bytes, bool padWithZero = false) |
| | 354 | | { |
| | 355 | | // pad with zero if the length is less than 2 |
| 16 | 356 | | var paddedBytes = bytes.ToArray(); |
| 16 | 357 | | if (padWithZero && bytes.Length < 2) |
| | 358 | | { |
| 0 | 359 | | paddedBytes = new byte[2]; |
| 0 | 360 | | bytes.CopyTo(paddedBytes.AsSpan()[(2 - bytes.Length)..]); |
| | 361 | | } |
| | 362 | |
|
| 16 | 363 | | if (!BitConverter.IsLittleEndian) |
| | 364 | | { |
| 0 | 365 | | Array.Reverse(paddedBytes); |
| | 366 | | } |
| 16 | 367 | | return BitConverter.ToInt16(paddedBytes, 0); |
| | 368 | | } |
| | 369 | | #endregion |
| | 370 | |
|
| | 371 | | #region Back From BE Bytes |
| | 372 | | /// <summary> |
| | 373 | | /// Converts a byte array to a ulong in big-endian order. |
| | 374 | | /// </summary> |
| | 375 | | /// <param name="bytes">The byte array to convert.</param> |
| | 376 | | /// <param name="padWithZero">If true, the byte array will be padded with zero if the length is less than 8.</param> |
| | 377 | | /// <returns>The ulong representation of the byte array.</returns> |
| | 378 | | /// <remarks>Padding with zero is useful when the byte array is used in a context where the length is known to be le |
| | 379 | | public static ulong ToUInt64BigEndian(ReadOnlySpan<byte> bytes, bool padWithZero = false) |
| | 380 | | { |
| | 381 | | // pad with zero if the length is less than 8 |
| 232 | 382 | | var paddedBytes = bytes.ToArray(); |
| 232 | 383 | | if (padWithZero && bytes.Length < 8) |
| | 384 | | { |
| 4 | 385 | | paddedBytes = new byte[8]; |
| 4 | 386 | | bytes.CopyTo(paddedBytes.AsSpan()[(8 - bytes.Length)..]); |
| | 387 | | } |
| | 388 | |
|
| 232 | 389 | | if (BitConverter.IsLittleEndian) |
| | 390 | | { |
| 232 | 391 | | Array.Reverse(paddedBytes); |
| | 392 | | } |
| 232 | 393 | | return BitConverter.ToUInt64(paddedBytes, 0); |
| | 394 | | } |
| | 395 | |
|
| | 396 | | /// <summary> |
| | 397 | | /// Converts a byte array to a ulong in big-endian order. |
| | 398 | | /// </summary> |
| | 399 | | /// <param name="bytes">The byte array to convert.</param> |
| | 400 | | /// <param name="padWithZero">If true, the byte array will be padded with zero if the length is less than 8.</param> |
| | 401 | | /// <returns>The ulong representation of the byte array.</returns> |
| | 402 | | /// <remarks>Padding with zero is useful when the byte array is used in a context where the length is known to be le |
| | 403 | | public static long ToInt64BigEndian(byte[] bytes, bool padWithZero = false) |
| | 404 | | { |
| | 405 | | // pad with zero if the length is less than 8 |
| 28 | 406 | | var paddedBytes = bytes.ToArray(); |
| 28 | 407 | | if (padWithZero && bytes.Length < 8) |
| | 408 | | { |
| 4 | 409 | | paddedBytes = new byte[8]; |
| 4 | 410 | | bytes.CopyTo(paddedBytes.AsSpan()[(8 - bytes.Length)..]); |
| | 411 | | } |
| | 412 | |
|
| 28 | 413 | | if (BitConverter.IsLittleEndian) |
| | 414 | | { |
| 28 | 415 | | Array.Reverse(paddedBytes); |
| | 416 | | } |
| 28 | 417 | | return BitConverter.ToInt64(paddedBytes, 0); |
| | 418 | | } |
| | 419 | |
|
| | 420 | | /// <summary> |
| | 421 | | /// Converts a byte array to a uint in big-endian order. |
| | 422 | | /// </summary> |
| | 423 | | /// <param name="bytes">The byte array to convert.</param> |
| | 424 | | /// <param name="padWithZero">If true, the byte array will be padded with zero if the length is less than 4.</param> |
| | 425 | | /// <returns>The uint representation of the byte array.</returns> |
| | 426 | | /// <remarks>Padding with zero is useful when the byte array is used in a context where the length is known to be le |
| | 427 | | public static uint ToUInt32BigEndian(ReadOnlySpan<byte> bytes, bool padWithZero = false) |
| | 428 | | { |
| | 429 | | // pad with zero if the length is less than 4 |
| 124 | 430 | | var paddedBytes = bytes.ToArray(); |
| 124 | 431 | | if (padWithZero && bytes.Length < 4) |
| | 432 | | { |
| 4 | 433 | | paddedBytes = new byte[4]; |
| 4 | 434 | | bytes.CopyTo(paddedBytes.AsSpan()[(4 - bytes.Length)..]); |
| | 435 | | } |
| | 436 | |
|
| 124 | 437 | | if (BitConverter.IsLittleEndian) |
| | 438 | | { |
| 124 | 439 | | Array.Reverse(paddedBytes); |
| | 440 | | } |
| 124 | 441 | | return BitConverter.ToUInt32(paddedBytes, 0); |
| | 442 | | } |
| | 443 | |
|
| | 444 | | /// <summary> |
| | 445 | | /// Converts a byte array to a uint in big-endian order. |
| | 446 | | /// </summary> |
| | 447 | | /// <param name="bytes">The byte array to convert.</param> |
| | 448 | | /// <param name="padWithZero">If true, the byte array will be padded with zero if the length is less than 4.</param> |
| | 449 | | /// <returns>The uint representation of the byte array.</returns> |
| | 450 | | /// <remarks>Padding with zero is useful when the byte array is used in a context where the length is known to be le |
| | 451 | | public static int ToInt32BigEndian(ReadOnlySpan<byte> bytes, bool padWithZero = false) |
| | 452 | | { |
| | 453 | | // pad with zero if the length is less than 4 |
| 16 | 454 | | var paddedBytes = bytes.ToArray(); |
| 16 | 455 | | if (padWithZero && bytes.Length < 4) |
| | 456 | | { |
| 4 | 457 | | paddedBytes = new byte[4]; |
| 4 | 458 | | bytes.CopyTo(paddedBytes.AsSpan()[(4 - bytes.Length)..]); |
| | 459 | | } |
| | 460 | |
|
| 16 | 461 | | if (BitConverter.IsLittleEndian) |
| | 462 | | { |
| 16 | 463 | | Array.Reverse(paddedBytes); |
| | 464 | | } |
| 16 | 465 | | return BitConverter.ToInt32(paddedBytes, 0); |
| | 466 | | } |
| | 467 | |
|
| | 468 | | /// <summary> |
| | 469 | | /// Converts a byte array to a ushort in big-endian order. |
| | 470 | | /// </summary> |
| | 471 | | /// <param name="bytes">The byte array to convert.</param> |
| | 472 | | /// <param name="padWithZero">If true, the byte array will be padded with zero if the length is less than 2.</param> |
| | 473 | | /// <returns>The ushort representation of the byte array.</returns> |
| | 474 | | /// <remarks>Padding with zero is useful when the byte array is used in a context where the length is known to be le |
| | 475 | | public static ushort ToUInt16BigEndian(ReadOnlySpan<byte> bytes, bool padWithZero = false) |
| | 476 | | { |
| | 477 | | // pad with zero if the length is less than 2 |
| 284 | 478 | | var paddedBytes = bytes.ToArray(); |
| 284 | 479 | | if (padWithZero && bytes.Length < 2) |
| | 480 | | { |
| 4 | 481 | | paddedBytes = new byte[2]; |
| 4 | 482 | | bytes.CopyTo(paddedBytes.AsSpan()[(2 - bytes.Length)..]); |
| | 483 | | } |
| | 484 | |
|
| 284 | 485 | | if (BitConverter.IsLittleEndian) |
| | 486 | | { |
| 284 | 487 | | Array.Reverse(paddedBytes); |
| | 488 | | } |
| 284 | 489 | | return BitConverter.ToUInt16(paddedBytes, 0); |
| | 490 | | } |
| | 491 | |
|
| | 492 | | /// <summary> |
| | 493 | | /// Converts a byte array to a ushort in big-endian order. |
| | 494 | | /// </summary> |
| | 495 | | /// <param name="bytes">The byte array to convert.</param> |
| | 496 | | /// <param name="padWithZero">If true, the byte array will be padded with zero if the length is less than 2.</param> |
| | 497 | | /// <returns>The ushort representation of the byte array.</returns> |
| | 498 | | /// <remarks>Padding with zero is useful when the byte array is used in a context where the length is known to be le |
| | 499 | | public static short ToInt16BigEndian(ReadOnlySpan<byte> bytes, bool padWithZero = false) |
| | 500 | | { |
| | 501 | | // pad with zero if the length is less than 2 |
| 16 | 502 | | var paddedBytes = bytes.ToArray(); |
| 16 | 503 | | if (padWithZero && bytes.Length < 2) |
| | 504 | | { |
| 4 | 505 | | paddedBytes = new byte[2]; |
| 4 | 506 | | bytes.CopyTo(paddedBytes.AsSpan()[(2 - bytes.Length)..]); |
| | 507 | | } |
| | 508 | |
|
| 16 | 509 | | if (BitConverter.IsLittleEndian) |
| | 510 | | { |
| 16 | 511 | | Array.Reverse(paddedBytes); |
| | 512 | | } |
| 16 | 513 | | return BitConverter.ToInt16(paddedBytes, 0); |
| | 514 | | } |
| | 515 | | #endregion |
| | 516 | | } |