2 // Mono.Security.BitConverterLE.cs
3 // Like System.BitConverter but always little endian
11 namespace Mono.Security
13 internal sealed class BitConverterLE
15 private BitConverterLE ()
19 unsafe private static byte[] GetUShortBytes (byte *bytes)
21 if (BitConverter.IsLittleEndian)
22 return new byte [] { bytes [0], bytes [1] };
24 return new byte [] { bytes [1], bytes [0] };
27 unsafe private static byte[] GetUIntBytes (byte *bytes)
29 if (BitConverter.IsLittleEndian)
30 return new byte [] { bytes [0], bytes [1], bytes [2], bytes [3] };
32 return new byte [] { bytes [3], bytes [2], bytes [1], bytes [0] };
35 unsafe private static byte[] GetULongBytes (byte *bytes)
37 if (BitConverter.IsLittleEndian)
38 return new byte [] { bytes [0], bytes [1], bytes [2], bytes [3],
39 bytes [4], bytes [5], bytes [6], bytes [7] };
41 return new byte [] { bytes [7], bytes [6], bytes [5], bytes [4],
42 bytes [3], bytes [2], bytes [1], bytes [0] };
45 unsafe internal static byte[] GetBytes (bool value)
47 return new byte [] { value ? (byte)1 : (byte)0 };
50 unsafe internal static byte[] GetBytes (char value)
52 return GetUShortBytes ((byte *) &value);
55 unsafe internal static byte[] GetBytes (short value)
57 return GetUShortBytes ((byte *) &value);
60 unsafe internal static byte[] GetBytes (int value)
62 return GetUIntBytes ((byte *) &value);
65 unsafe internal static byte[] GetBytes (long value)
67 return GetULongBytes ((byte *) &value);
70 unsafe internal static byte[] GetBytes (ushort value)
72 return GetUShortBytes ((byte *) &value);
75 unsafe internal static byte[] GetBytes (uint value)
77 return GetUIntBytes ((byte *) &value);
80 unsafe internal static byte[] GetBytes (ulong value)
82 return GetULongBytes ((byte *) &value);
85 unsafe internal static byte[] GetBytes (float value)
87 return GetUIntBytes ((byte *) &value);
90 unsafe internal static byte[] GetBytes (double value)
92 return GetULongBytes ((byte *) &value);
95 unsafe private static void UShortFromBytes (byte *dst, byte[] src, int startIndex)
97 if (BitConverter.IsLittleEndian) {
98 dst [0] = src [startIndex];
99 dst [1] = src [startIndex + 1];
101 dst [0] = src [startIndex + 1];
102 dst [1] = src [startIndex];
106 unsafe private static void UIntFromBytes (byte *dst, byte[] src, int startIndex)
108 if (BitConverter.IsLittleEndian) {
109 dst [0] = src [startIndex];
110 dst [1] = src [startIndex + 1];
111 dst [2] = src [startIndex + 2];
112 dst [3] = src [startIndex + 3];
114 dst [0] = src [startIndex + 3];
115 dst [1] = src [startIndex + 2];
116 dst [2] = src [startIndex + 1];
117 dst [3] = src [startIndex];
121 unsafe private static void ULongFromBytes (byte *dst, byte[] src, int startIndex)
123 if (BitConverter.IsLittleEndian) {
124 for (int i = 0; i < 8; ++i)
125 dst [i] = src [startIndex + i];
127 for (int i = 0; i < 8; ++i)
128 dst [i] = src [startIndex + (7 - i)];
132 unsafe internal static bool ToBoolean (byte[] value, int startIndex)
134 return value [startIndex] != 0;
137 unsafe internal static char ToChar (byte[] value, int startIndex)
141 UShortFromBytes ((byte *) &ret, value, startIndex);
146 unsafe internal static short ToInt16 (byte[] value, int startIndex)
150 UShortFromBytes ((byte *) &ret, value, startIndex);
155 unsafe internal static int ToInt32 (byte[] value, int startIndex)
159 UIntFromBytes ((byte *) &ret, value, startIndex);
164 unsafe internal static long ToInt64 (byte[] value, int startIndex)
168 ULongFromBytes ((byte *) &ret, value, startIndex);
173 unsafe internal static ushort ToUInt16 (byte[] value, int startIndex)
177 UShortFromBytes ((byte *) &ret, value, startIndex);
182 unsafe internal static uint ToUInt32 (byte[] value, int startIndex)
186 UIntFromBytes ((byte *) &ret, value, startIndex);
191 unsafe internal static ulong ToUInt64 (byte[] value, int startIndex)
195 ULongFromBytes ((byte *) &ret, value, startIndex);
200 unsafe internal static float ToSingle (byte[] value, int startIndex)
204 UIntFromBytes ((byte *) &ret, value, startIndex);
209 unsafe internal static double ToDouble (byte[] value, int startIndex)
213 ULongFromBytes ((byte *) &ret, value, startIndex);