5 // Matt Kimball (matt@kimball.net)
11 public sealed class BitConverter {
13 private BitConverter () {}
15 static bool AmILittleEndian()
17 byte[] one = GetBytes((int)1);
21 public static readonly bool IsLittleEndian = AmILittleEndian ();
23 public static long DoubleToInt64Bits(double value)
25 return ToInt64(GetBytes(value), 0);
28 public static double Int64BitsToDouble(long value)
30 return ToDouble(GetBytes(value), 0);
33 unsafe static byte[] GetBytes(byte *ptr, int count)
35 byte[] ret = new byte[count];
37 for (int i = 0; i < count; i++) {
44 unsafe public static byte[] GetBytes(bool value)
46 return GetBytes((byte *)&value, 1);
49 unsafe public static byte[] GetBytes(char value)
51 return GetBytes((byte *)&value, 2);
54 unsafe public static byte[] GetBytes(short value)
56 return GetBytes((byte *)&value, 2);
59 unsafe public static byte[] GetBytes(int value)
61 return GetBytes((byte *)&value, 4);
64 unsafe public static byte[] GetBytes(long value)
66 return GetBytes((byte *)&value, 8);
70 unsafe public static byte[] GetBytes(ushort value)
72 return GetBytes((byte *)&value, 2);
76 unsafe public static byte[] GetBytes(uint value)
78 return GetBytes((byte *)&value, 4);
82 unsafe public static byte[] GetBytes(ulong value)
84 return GetBytes((byte *)&value, 8);
87 unsafe public static byte[] GetBytes(float value)
89 return GetBytes((byte *)&value, 4);
92 unsafe public static byte[] GetBytes(double value)
94 return GetBytes((byte *)&value, 8);
97 unsafe static void PutBytes(byte *dst, byte[] src, int start_index, int count)
100 throw new ArgumentNullException();
103 if (src.Length < start_index + count) {
105 // the docs say it should be ArgumentOutOfRangeException, but
106 // the mscorlib throws an ArgumentException.
107 throw new ArgumentException();
110 for (int i = 0; i < count; i++) {
111 dst[i] = src[i + start_index];
115 unsafe public static bool ToBoolean(byte[] value, int start_index)
119 PutBytes((byte *)&ret, value, start_index, 1);
124 unsafe public static char ToChar(byte[] value, int start_index)
128 PutBytes((byte *)&ret, value, start_index, 2);
133 unsafe public static short ToInt16(byte[] value, int start_index)
137 PutBytes((byte *)&ret, value, start_index, 2);
142 unsafe public static int ToInt32(byte[] value, int start_index)
146 PutBytes((byte *)&ret, value, start_index, 4);
151 unsafe public static long ToInt64(byte[] value, int start_index)
155 PutBytes((byte *)&ret, value, start_index, 8);
160 [CLSCompliant(false)]
161 unsafe public static ushort ToUInt16(byte[] value, int start_index)
165 PutBytes((byte *)&ret, value, start_index, 2);
170 [CLSCompliant(false)]
171 unsafe public static uint ToUInt32(byte[] value, int start_index)
175 PutBytes((byte *)&ret, value, start_index, 4);
180 [CLSCompliant(false)]
181 unsafe public static ulong ToUInt64(byte[] value, int start_index)
185 PutBytes((byte *)&ret, value, start_index, 8);
190 unsafe public static float ToSingle(byte[] value, int start_index)
194 PutBytes((byte *)&ret, value, start_index, 4);
199 unsafe public static double ToDouble(byte[] value, int start_index)
203 PutBytes((byte *)&ret, value, start_index, 8);
208 public static string ToString(byte[] value)
211 throw new ArgumentNullException();
214 return ToString(value, 0, value.Length);
217 public static string ToString(byte[] value, int start_index)
220 throw new ArgumentNullException("value");
221 if (start_index < 0 || start_index > value.Length - 1)
222 throw new ArgumentOutOfRangeException("start_index");
224 return ToString(value, start_index, value.Length - start_index);
227 public static string ToString(byte[] value, int start_index, int length)
230 throw new ArgumentNullException();
233 // The 4th and last clause (start_index >= value.Length)
234 // was added as a small fix to a very obscure bug.
235 // It makes a small difference when start_index is
236 // outside the range and length==0.
237 if (start_index < 0 || length < 0 || start_index + length > value.Length || start_index >= value.Length) {
238 throw new ArgumentOutOfRangeException();
241 int end = start_index + length;
243 for (int i = start_index; i < end; i++) {
247 char high = (char)((value[i] >> 4) & 0x0f);
248 char low = (char)(value[i] & 0x0f);
264 ret = ret + high + low;