Skip to content

Commit 1a23efb

Browse files
authored
Extend Endianness class to include more functions for conversion
Signed-off-by: Xen <lordofxen@deskasoft.com>
1 parent f54e1a0 commit 1a23efb

1 file changed

Lines changed: 57 additions & 1 deletion

File tree

HashifyNet/Core/Utilities/Endianness.cs

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// *
1+
// *
22
// *****************************************************************************
33
// *
44
// * Copyright (c) 2025 Deskasoft International
@@ -112,5 +112,61 @@ public static ulong ToUInt64LittleEndian(byte[] buffer, int offset)
112112
((ulong)buffer[offset + 6] << 48) |
113113
((ulong)buffer[offset + 7] << 56);
114114
}
115+
116+
/// <summary>
117+
/// Converts a sequence of bytes from the specified buffer, starting at the given offset, into a 64-bit unsigned
118+
/// integer using little-endian byte order.
119+
/// </summary>
120+
/// <remarks>This method assumes that the bytes in the buffer are stored in little-endian format, where the
121+
/// least significant byte comes first.</remarks>
122+
/// <param name="buffer">The buffer containing the bytes to convert. Must have at least 8 bytes available starting from <paramref
123+
/// name="offset"/>.</param>
124+
/// <param name="offset">The zero-based index in <paramref name="buffer"/> at which to begin reading the bytes.</param>
125+
/// <returns>A 64-bit unsigned integer constructed from the 8 bytes starting at <paramref name="offset"/> in little-endian
126+
/// order.</returns>
127+
public static ulong ToUInt64LittleEndian(ReadOnlySpan<byte> buffer, int offset)
128+
{
129+
return buffer[offset] |
130+
((ulong)buffer[offset + 1] << 8) |
131+
((ulong)buffer[offset + 2] << 16) |
132+
((ulong)buffer[offset + 3] << 24) |
133+
((ulong)buffer[offset + 4] << 32) |
134+
((ulong)buffer[offset + 5] << 40) |
135+
((ulong)buffer[offset + 6] << 48) |
136+
((ulong)buffer[offset + 7] << 56);
137+
}
138+
139+
/// <summary>
140+
/// Converts a sequence of four bytes from the specified buffer, starting at the given offset, into a 32-bit unsigned
141+
/// integer using little-endian byte order.
142+
/// </summary>
143+
/// <param name="buffer">The byte array containing the data to convert. Must contain at least four bytes starting from <paramref
144+
/// name="offset"/>.</param>
145+
/// <param name="offset">The zero-based index in <paramref name="buffer"/> at which to begin reading the four bytes.</param>
146+
/// <returns>A 32-bit unsigned integer representing the value of the four bytes in little-endian order.</returns>
147+
public static uint ToUInt32LittleEndian(byte[] buffer, int offset)
148+
{
149+
return buffer[offset] |
150+
((uint)buffer[offset + 1] << 8) |
151+
((uint)buffer[offset + 2] << 16) |
152+
((uint)buffer[offset + 3] << 24);
153+
}
154+
155+
/// <summary>
156+
/// Converts a sequence of bytes from a specified offset in a buffer to a 32-bit unsigned integer, assuming
157+
/// little-endian byte order.
158+
/// </summary>
159+
/// <param name="buffer">The buffer containing the bytes to convert. Must have at least four bytes available starting at the specified
160+
/// offset.</param>
161+
/// <param name="offset">The zero-based index in the buffer at which to begin reading the bytes.</param>
162+
/// <returns>A 32-bit unsigned integer representing the value of the four bytes starting at the specified offset, interpreted
163+
/// as little-endian.</returns>
164+
public static uint ToUInt32LittleEndian(ReadOnlySpan<byte> buffer, int offset)
165+
{
166+
return buffer[offset] |
167+
((uint)buffer[offset + 1] << 8) |
168+
((uint)buffer[offset + 2] << 16) |
169+
((uint)buffer[offset + 3] << 24);
170+
}
115171
}
116172
}

0 commit comments

Comments
 (0)