Skip to content

Commit 67ea5c7

Browse files
authored
Add even more functions to Endianness Utility
Signed-off-by: Xen <lordofxen@deskasoft.com>
1 parent 76ce780 commit 67ea5c7

1 file changed

Lines changed: 113 additions & 0 deletions

File tree

HashifyNet/Core/Utilities/Endianness.cs

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,31 @@ public static ulong ToUInt64LittleEndian(byte[] buffer, int offset)
304304
((ulong)buffer[offset + 7] << 56);
305305
}
306306

307+
/// <summary>
308+
/// Combines eight bytes into a 64-bit unsigned integer, interpreting the bytes in little-endian order.
309+
/// </summary>
310+
/// <param name="v1">The least significant byte of the resulting 64-bit integer.</param>
311+
/// <param name="v2">The second least significant byte of the resulting 64-bit integer.</param>
312+
/// <param name="v3">The third least significant byte of the resulting 64-bit integer.</param>
313+
/// <param name="v4">The fourth least significant byte of the resulting 64-bit integer.</param>
314+
/// <param name="v5">The fifth least significant byte of the resulting 64-bit integer.</param>
315+
/// <param name="v6">The sixth least significant byte of the resulting 64-bit integer.</param>
316+
/// <param name="v7">The seventh least significant byte of the resulting 64-bit integer.</param>
317+
/// <param name="v8">The most significant byte of the resulting 64-bit integer.</param>
318+
/// <returns>A 64-bit unsigned integer constructed from the specified bytes in little-endian order.</returns>
319+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
320+
public static ulong ToUInt64LittleEndian(byte v1, byte v2, byte v3, byte v4, byte v5, byte v6, byte v7, byte v8)
321+
{
322+
return v1 |
323+
((ulong)v2 << 8) |
324+
((ulong)v3 << 16) |
325+
((ulong)v4 << 24) |
326+
((ulong)v5 << 32) |
327+
((ulong)v6 << 40) |
328+
((ulong)v7 << 48) |
329+
((ulong)v8 << 56);
330+
}
331+
307332
/// <summary>
308333
/// Converts a sequence of bytes from the specified buffer, starting at the given offset, into a 64-bit unsigned
309334
/// integer using little-endian byte order.
@@ -328,6 +353,18 @@ public static ulong ToUInt64LittleEndian(ReadOnlySpan<byte> buffer, int offset)
328353
((ulong)buffer[offset + 7] << 56);
329354
}
330355

356+
/// <summary>
357+
/// Converts two bytes to a 16-bit unsigned integer, assuming little-endian byte order.
358+
/// </summary>
359+
/// <param name="v1">The least significant byte of the 16-bit unsigned integer.</param>
360+
/// <param name="v2">The most significant byte of the 16-bit unsigned integer.</param>
361+
/// <returns>A 16-bit unsigned integer constructed from the specified bytes in little-endian order.</returns>
362+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
363+
public static ushort ToUInt16LittleEndian(byte v1, byte v2)
364+
{
365+
return (ushort)(v1 | (v2 << 8));
366+
}
367+
331368
/// <summary>
332369
/// Converts a sequence of two bytes from the specified buffer, starting at the given offset, into a 16-bit unsigned
333370
/// integer using little-endian byte order.
@@ -342,6 +379,23 @@ public static ushort ToUInt16LittleEndian(byte[] buffer, int offset)
342379
return (ushort)(buffer[offset] | (buffer[offset + 1] << 8));
343380
}
344381

382+
/// <summary>
383+
/// Converts four bytes to a 32-bit unsigned integer using little-endian byte order.
384+
/// </summary>
385+
/// <param name="v1">The least significant byte of the resulting integer.</param>
386+
/// <param name="v2">The second least significant byte of the resulting integer.</param>
387+
/// <param name="v3">The second most significant byte of the resulting integer.</param>
388+
/// <param name="v4">The most significant byte of the resulting integer.</param>
389+
/// <returns>A 32-bit unsigned integer constructed from the specified bytes in little-endian order.</returns>
390+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
391+
public static uint ToUInt32LittleEndian(byte v1, byte v2, byte v3, byte v4)
392+
{
393+
return v1 |
394+
((uint)v2 << 8) |
395+
((uint)v3 << 16) |
396+
((uint)v4 << 24);
397+
}
398+
345399
/// <summary>
346400
/// Converts a sequence of four bytes from the specified buffer, starting at the given offset, into a 32-bit unsigned
347401
/// integer using little-endian byte order.
@@ -423,6 +477,46 @@ public static ulong ToUInt64BigEndian(ReadOnlySpan<byte> buffer, int offset)
423477
buffer[offset + 7];
424478
}
425479

480+
/// <summary>
481+
/// Combines eight bytes into a single 64-bit unsigned integer, interpreting the bytes in big-endian order.
482+
/// </summary>
483+
/// <remarks>This method shifts each byte to its appropriate position in the 64-bit integer, with the most
484+
/// significant byte placed at the highest position. It is optimized for performance and assumes the caller provides
485+
/// all eight bytes in the correct order.</remarks>
486+
/// <param name="v1">The most significant byte of the resulting 64-bit unsigned integer.</param>
487+
/// <param name="v2">The second most significant byte of the resulting 64-bit unsigned integer.</param>
488+
/// <param name="v3">The third most significant byte of the resulting 64-bit unsigned integer.</param>
489+
/// <param name="v4">The fourth most significant byte of the resulting 64-bit unsigned integer.</param>
490+
/// <param name="v5">The fifth most significant byte of the resulting 64-bit unsigned integer.</param>
491+
/// <param name="v6">The sixth most significant byte of the resulting 64-bit unsigned integer.</param>
492+
/// <param name="v7">The seventh most significant byte of the resulting 64-bit unsigned integer.</param>
493+
/// <param name="v8">The least significant byte of the resulting 64-bit unsigned integer.</param>
494+
/// <returns>A 64-bit unsigned integer constructed from the specified bytes in big-endian order.</returns>
495+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
496+
public static ulong ToUInt64BigEndian(byte v1, byte v2, byte v3, byte v4, byte v5, byte v6, byte v7, byte v8)
497+
{
498+
return ((ulong)v1 << 56) |
499+
((ulong)v2 << 48) |
500+
((ulong)v3 << 40) |
501+
((ulong)v4 << 32) |
502+
((ulong)v5 << 24) |
503+
((ulong)v6 << 16) |
504+
((ulong)v7 << 8) |
505+
v8;
506+
}
507+
508+
/// <summary>
509+
/// Converts two bytes to a 16-bit unsigned integer, interpreting the bytes in big-endian order.
510+
/// </summary>
511+
/// <param name="v1">The most significant byte of the 16-bit unsigned integer.</param>
512+
/// <param name="v2">The least significant byte of the 16-bit unsigned integer.</param>
513+
/// <returns>A 16-bit unsigned integer constructed from the specified bytes in big-endian order.</returns>
514+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
515+
public static ushort ToUInt16BigEndian(byte v1, byte v2)
516+
{
517+
return (ushort)((v1 << 8) | v2);
518+
}
519+
426520
/// <summary>
427521
/// Converts a sequence of two bytes from the specified buffer, starting at the given offset, into a 16-bit unsigned
428522
/// integer using big-endian byte order.
@@ -437,6 +531,25 @@ public static uint ToUInt16BigEndian(byte[] buffer, int offset)
437531
return (ushort)((buffer[offset] << 8) | buffer[offset + 1]);
438532
}
439533

534+
/// <summary>
535+
/// Converts four bytes to a 32-bit unsigned integer, interpreting the bytes in big-endian order.
536+
/// </summary>
537+
/// <remarks>This method assumes the input bytes are provided in big-endian order, where the most significant
538+
/// byte is first and the least significant byte is last.</remarks>
539+
/// <param name="v1">The most significant byte of the 32-bit unsigned integer.</param>
540+
/// <param name="v2">The second most significant byte of the 32-bit unsigned integer.</param>
541+
/// <param name="v3">The third most significant byte of the 32-bit unsigned integer.</param>
542+
/// <param name="v4">The least significant byte of the 32-bit unsigned integer.</param>
543+
/// <returns>A 32-bit unsigned integer constructed from the specified bytes in big-endian order.</returns>
544+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
545+
public static uint ToUInt32BigEndian(byte v1, byte v2, byte v3, byte v4)
546+
{
547+
return ((uint)v1 << 24) |
548+
((uint)v2 << 16) |
549+
((uint)v3 << 8) |
550+
v4;
551+
}
552+
440553
/// <summary>
441554
/// Converts a sequence of four bytes from the specified buffer, starting at the given offset, into a 32-bit unsigned
442555
/// integer using big-endian byte order.

0 commit comments

Comments
 (0)