Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions src/libraries/System.Private.CoreLib/src/System/BitConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public static class BitConverter
/// </summary>
/// <param name="value">A Boolean value.</param>
/// <returns>A byte array with length 1.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte[] GetBytes(bool value)
{
byte[] r = new byte[1];
Expand Down Expand Up @@ -56,10 +57,11 @@ public static bool TryWriteBytes(Span<byte> destination, bool value)
/// </summary>
/// <param name="value">A Char value.</param>
/// <returns>An array of bytes with length 2.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte[] GetBytes(char value)
{
byte[] bytes = new byte[sizeof(char)];
Unsafe.As<byte, char>(ref bytes[0]) = value;
Unsafe.WriteUnaligned(ref MemoryMarshal.GetArrayDataReference(bytes), value);
return bytes;
}

Expand All @@ -83,10 +85,11 @@ public static bool TryWriteBytes(Span<byte> destination, char value)
/// </summary>
/// <param name="value">The number to convert.</param>
/// <returns>An array of bytes with length 2.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte[] GetBytes(short value)
{
byte[] bytes = new byte[sizeof(short)];
Unsafe.As<byte, short>(ref bytes[0]) = value;
Unsafe.WriteUnaligned(ref MemoryMarshal.GetArrayDataReference(bytes), value);
return bytes;
}

Expand All @@ -110,10 +113,11 @@ public static bool TryWriteBytes(Span<byte> destination, short value)
/// </summary>
/// <param name="value">The number to convert.</param>
/// <returns>An array of bytes with length 4.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte[] GetBytes(int value)
{
byte[] bytes = new byte[sizeof(int)];
Unsafe.As<byte, int>(ref bytes[0]) = value;
Unsafe.WriteUnaligned(ref MemoryMarshal.GetArrayDataReference(bytes), value);
return bytes;
}

Expand All @@ -137,10 +141,11 @@ public static bool TryWriteBytes(Span<byte> destination, int value)
/// </summary>
/// <param name="value">The number to convert.</param>
/// <returns>An array of bytes with length 8.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte[] GetBytes(long value)
{
byte[] bytes = new byte[sizeof(long)];
Unsafe.As<byte, long>(ref bytes[0]) = value;
Unsafe.WriteUnaligned(ref MemoryMarshal.GetArrayDataReference(bytes), value);
return bytes;
}

Expand All @@ -165,10 +170,11 @@ public static bool TryWriteBytes(Span<byte> destination, long value)
/// <param name="value">The number to convert.</param>
/// <returns>An array of bytes with length 2.</returns>
[CLSCompliant(false)]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte[] GetBytes(ushort value)
{
byte[] bytes = new byte[sizeof(ushort)];
Unsafe.As<byte, ushort>(ref bytes[0]) = value;
Unsafe.WriteUnaligned(ref MemoryMarshal.GetArrayDataReference(bytes), value);
return bytes;
}

Expand All @@ -194,10 +200,11 @@ public static bool TryWriteBytes(Span<byte> destination, ushort value)
/// <param name="value">The number to convert.</param>
/// <returns>An array of bytes with length 4.</returns>
[CLSCompliant(false)]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte[] GetBytes(uint value)
{
byte[] bytes = new byte[sizeof(uint)];
Unsafe.As<byte, uint>(ref bytes[0]) = value;
Unsafe.WriteUnaligned(ref MemoryMarshal.GetArrayDataReference(bytes), value);
return bytes;
}

Expand All @@ -223,10 +230,11 @@ public static bool TryWriteBytes(Span<byte> destination, uint value)
/// <param name="value">The number to convert.</param>
/// <returns>An array of bytes with length 8.</returns>
[CLSCompliant(false)]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte[] GetBytes(ulong value)
{
byte[] bytes = new byte[sizeof(ulong)];
Unsafe.As<byte, ulong>(ref bytes[0]) = value;
Unsafe.WriteUnaligned(ref MemoryMarshal.GetArrayDataReference(bytes), value);
return bytes;
}

Expand All @@ -251,10 +259,11 @@ public static bool TryWriteBytes(Span<byte> destination, ulong value)
/// </summary>
/// <param name="value">The number to convert.</param>
/// <returns>An array of bytes with length 2.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe byte[] GetBytes(Half value)
{
byte[] bytes = new byte[sizeof(Half)];
Unsafe.As<byte, Half>(ref bytes[0]) = value;
Unsafe.WriteUnaligned(ref MemoryMarshal.GetArrayDataReference(bytes), value);
return bytes;
}

Expand All @@ -278,10 +287,11 @@ public static unsafe bool TryWriteBytes(Span<byte> destination, Half value)
/// </summary>
/// <param name="value">The number to convert.</param>
/// <returns>An array of bytes with length 4.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte[] GetBytes(float value)
{
byte[] bytes = new byte[sizeof(float)];
Unsafe.As<byte, float>(ref bytes[0]) = value;
Unsafe.WriteUnaligned(ref MemoryMarshal.GetArrayDataReference(bytes), value);
return bytes;
}

Expand All @@ -305,10 +315,11 @@ public static bool TryWriteBytes(Span<byte> destination, float value)
/// </summary>
/// <param name="value">The number to convert.</param>
/// <returns>An array of bytes with length 8.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte[] GetBytes(double value)
{
byte[] bytes = new byte[sizeof(double)];
Unsafe.As<byte, double>(ref bytes[0]) = value;
Unsafe.WriteUnaligned(ref MemoryMarshal.GetArrayDataReference(bytes), value);
return bytes;
}

Expand Down