|
1 | | -// * |
| 1 | +// * |
2 | 2 | // ***************************************************************************** |
3 | 3 | // * |
4 | 4 | // * Copyright (c) 2025 Deskasoft International |
@@ -112,5 +112,61 @@ public static ulong ToUInt64LittleEndian(byte[] buffer, int offset) |
112 | 112 | ((ulong)buffer[offset + 6] << 48) | |
113 | 113 | ((ulong)buffer[offset + 7] << 56); |
114 | 114 | } |
| 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 | + } |
115 | 171 | } |
116 | 172 | } |
0 commit comments