@@ -36,7 +36,7 @@ public static bool TryWriteBytes(Span<byte> destination, bool value)
3636 if ( destination . Length < sizeof ( byte ) )
3737 return false ;
3838
39- Unsafe . WriteUnaligned ( ref destination . DangerousGetPinnableReference ( ) , value ? ( byte ) 1 : ( byte ) 0 ) ;
39+ Unsafe . WriteUnaligned ( ref destination . DangerousGetPinnableReference ( ) , value ? ( byte ) 1 : ( byte ) 0 ) ;
4040 return true ;
4141 }
4242
@@ -217,7 +217,7 @@ public static bool TryWriteBytes(Span<byte> destination, double value)
217217 }
218218
219219 // Converts an array of bytes into a char.
220- public static char ToChar ( byte [ ] value , int startIndex ) => unchecked ( ( char ) ReadInt16 ( value , startIndex ) ) ;
220+ public static char ToChar ( byte [ ] value , int startIndex ) => unchecked ( ( char ) ToInt16 ( value , startIndex ) ) ;
221221
222222 // Converts a Span into a char
223223 public static char ToChar ( ReadOnlySpan < byte > value )
@@ -227,7 +227,8 @@ public static char ToChar(ReadOnlySpan<byte> value)
227227 return Unsafe . ReadUnaligned < char > ( ref value . DangerousGetPinnableReference ( ) ) ;
228228 }
229229
230- private static short ReadInt16 ( byte [ ] value , int startIndex )
230+ // Converts an array of bytes into a short.
231+ public static short ToInt16 ( byte [ ] value , int startIndex )
231232 {
232233 if ( value == null )
233234 ThrowHelper . ThrowArgumentNullException ( ExceptionArgument . value ) ;
@@ -239,44 +240,27 @@ private static short ReadInt16(byte[] value, int startIndex)
239240 return Unsafe . ReadUnaligned < short > ( ref value [ startIndex ] ) ;
240241 }
241242
242- private static int ReadInt32 ( byte [ ] value , int startIndex )
243+ // Converts a Span into a short
244+ public static short ToInt16 ( ReadOnlySpan < byte > value )
243245 {
244- if ( value == null )
245- ThrowHelper . ThrowArgumentNullException ( ExceptionArgument . value ) ;
246- if ( unchecked ( ( uint ) startIndex ) >= unchecked ( ( uint ) value . Length ) )
247- ThrowHelper . ThrowArgumentOutOfRangeException ( ExceptionArgument . startIndex , ExceptionResource . ArgumentOutOfRange_Index ) ;
248- if ( startIndex > value . Length - sizeof ( int ) )
249- ThrowHelper . ThrowArgumentException ( ExceptionResource . Arg_ArrayPlusOffTooSmall , ExceptionArgument . value ) ;
250-
251- return Unsafe . ReadUnaligned < int > ( ref value [ startIndex ] ) ;
246+ if ( value . Length < sizeof ( short ) )
247+ ThrowHelper . ThrowArgumentOutOfRangeException ( ExceptionArgument . value ) ;
248+ return Unsafe . ReadUnaligned < short > ( ref value . DangerousGetPinnableReference ( ) ) ;
252249 }
253250
254- private static long ReadInt64 ( byte [ ] value , int startIndex )
251+ // Converts an array of bytes into an int.
252+ public static int ToInt32 ( byte [ ] value , int startIndex )
255253 {
256254 if ( value == null )
257255 ThrowHelper . ThrowArgumentNullException ( ExceptionArgument . value ) ;
258256 if ( unchecked ( ( uint ) startIndex ) >= unchecked ( ( uint ) value . Length ) )
259257 ThrowHelper . ThrowArgumentOutOfRangeException ( ExceptionArgument . startIndex , ExceptionResource . ArgumentOutOfRange_Index ) ;
260- if ( startIndex > value . Length - sizeof ( long ) )
258+ if ( startIndex > value . Length - sizeof ( int ) )
261259 ThrowHelper . ThrowArgumentException ( ExceptionResource . Arg_ArrayPlusOffTooSmall , ExceptionArgument . value ) ;
262260
263- return Unsafe . ReadUnaligned < long > ( ref value [ startIndex ] ) ;
264- }
265-
266- // Converts an array of bytes into a short.
267- public static short ToInt16 ( byte [ ] value , int startIndex ) => ReadInt16 ( value , startIndex ) ;
268-
269- // Converts a Span into a short
270- public static short ToInt16 ( ReadOnlySpan < byte > value )
271- {
272- if ( value . Length < sizeof ( short ) )
273- ThrowHelper . ThrowArgumentOutOfRangeException ( ExceptionArgument . value ) ;
274- return Unsafe . ReadUnaligned < short > ( ref value . DangerousGetPinnableReference ( ) ) ;
261+ return Unsafe . ReadUnaligned < int > ( ref value [ startIndex ] ) ;
275262 }
276263
277- // Converts an array of bytes into an int.
278- public static int ToInt32 ( byte [ ] value , int startIndex ) => ReadInt32 ( value , startIndex ) ;
279-
280264 // Converts a Span into an int
281265 public static int ToInt32 ( ReadOnlySpan < byte > value )
282266 {
@@ -286,7 +270,17 @@ public static int ToInt32(ReadOnlySpan<byte> value)
286270 }
287271
288272 // Converts an array of bytes into a long.
289- public static long ToInt64 ( byte [ ] value , int startIndex ) => ReadInt64 ( value , startIndex ) ;
273+ public static long ToInt64 ( byte [ ] value , int startIndex )
274+ {
275+ if ( value == null )
276+ ThrowHelper . ThrowArgumentNullException ( ExceptionArgument . value ) ;
277+ if ( unchecked ( ( uint ) startIndex ) >= unchecked ( ( uint ) value . Length ) )
278+ ThrowHelper . ThrowArgumentOutOfRangeException ( ExceptionArgument . startIndex , ExceptionResource . ArgumentOutOfRange_Index ) ;
279+ if ( startIndex > value . Length - sizeof ( long ) )
280+ ThrowHelper . ThrowArgumentException ( ExceptionResource . Arg_ArrayPlusOffTooSmall , ExceptionArgument . value ) ;
281+
282+ return Unsafe . ReadUnaligned < long > ( ref value [ startIndex ] ) ;
283+ }
290284
291285 // Converts a Span into a long
292286 public static long ToInt64 ( ReadOnlySpan < byte > value )
@@ -299,7 +293,7 @@ public static long ToInt64(ReadOnlySpan<byte> value)
299293 // Converts an array of bytes into an ushort.
300294 //
301295 [ CLSCompliant ( false ) ]
302- public static ushort ToUInt16 ( byte [ ] value , int startIndex ) => unchecked ( ( ushort ) ReadInt16 ( value , startIndex ) ) ;
296+ public static ushort ToUInt16 ( byte [ ] value , int startIndex ) => unchecked ( ( ushort ) ToInt16 ( value , startIndex ) ) ;
303297
304298 // Converts a Span into a ushort
305299 [ CLSCompliant ( false ) ]
@@ -313,7 +307,7 @@ public static ushort ToUInt16(ReadOnlySpan<byte> value)
313307 // Converts an array of bytes into an uint.
314308 //
315309 [ CLSCompliant ( false ) ]
316- public static uint ToUInt32 ( byte [ ] value , int startIndex ) => unchecked ( ( uint ) ReadInt32 ( value , startIndex ) ) ;
310+ public static uint ToUInt32 ( byte [ ] value , int startIndex ) => unchecked ( ( uint ) ToInt32 ( value , startIndex ) ) ;
317311
318312 // Convert a Span into a uint
319313 [ CLSCompliant ( false ) ]
@@ -327,7 +321,7 @@ public static uint ToUInt32(ReadOnlySpan<byte> value)
327321 // Converts an array of bytes into an unsigned long.
328322 //
329323 [ CLSCompliant ( false ) ]
330- public static ulong ToUInt64 ( byte [ ] value , int startIndex ) => unchecked ( ( ulong ) ReadInt64 ( value , startIndex ) ) ;
324+ public static ulong ToUInt64 ( byte [ ] value , int startIndex ) => unchecked ( ( ulong ) ToInt64 ( value , startIndex ) ) ;
331325
332326 // Converts a Span into an unsigned long
333327 [ CLSCompliant ( false ) ]
@@ -339,11 +333,7 @@ public static ulong ToUInt64(ReadOnlySpan<byte> value)
339333 }
340334
341335 // Converts an array of bytes into a float.
342- public static unsafe float ToSingle ( byte [ ] value , int startIndex )
343- {
344- int val = ReadInt32 ( value , startIndex ) ;
345- return * ( float * ) & val ;
346- }
336+ public static float ToSingle ( byte [ ] value , int startIndex ) => Int32BitsToSingle ( ToInt32 ( value , startIndex ) ) ;
347337
348338 // Converts a Span into a float
349339 public static float ToSingle ( ReadOnlySpan < byte > value )
@@ -354,11 +344,7 @@ public static float ToSingle(ReadOnlySpan<byte> value)
354344 }
355345
356346 // Converts an array of bytes into a double.
357- public static unsafe double ToDouble ( byte [ ] value , int startIndex )
358- {
359- long val = ReadInt64 ( value , startIndex ) ;
360- return * ( double * ) & val ;
361- }
347+ public static double ToDouble ( byte [ ] value , int startIndex ) => Int64BitsToDouble ( ToInt64 ( value , startIndex ) ) ;
362348
363349 // Converts a Span into a double
364350 public static double ToDouble ( ReadOnlySpan < byte > value )
0 commit comments