mscorlib(4.0.0.0) API with additions
BitConverter.cs
1 using System.Security;
2 
3 namespace System
4 {
6  [__DynamicallyInvokable]
7  public static class BitConverter
8  {
12  [__DynamicallyInvokable]
13  public static readonly bool IsLittleEndian = true;
14 
18  [__DynamicallyInvokable]
19  public static byte[] GetBytes(bool value)
20  {
21  return new byte[1]
22  {
23  (byte)(value ? 1 : 0)
24  };
25  }
26 
30  [__DynamicallyInvokable]
31  public static byte[] GetBytes(char value)
32  {
33  return GetBytes((short)value);
34  }
35 
39  [SecuritySafeCritical]
40  [__DynamicallyInvokable]
41  public unsafe static byte[] GetBytes(short value)
42  {
43  byte[] array = new byte[2];
44  byte[] array2 = array;
45  fixed (byte* ptr = array2)
46  {
47  *(short*)ptr = value;
48  }
49  return array;
50  }
51 
55  [SecuritySafeCritical]
56  [__DynamicallyInvokable]
57  public unsafe static byte[] GetBytes(int value)
58  {
59  byte[] array = new byte[4];
60  byte[] array2 = array;
61  fixed (byte* ptr = array2)
62  {
63  *(int*)ptr = value;
64  }
65  return array;
66  }
67 
71  [SecuritySafeCritical]
72  [__DynamicallyInvokable]
73  public unsafe static byte[] GetBytes(long value)
74  {
75  byte[] array = new byte[8];
76  byte[] array2 = array;
77  fixed (byte* ptr = array2)
78  {
79  *(long*)ptr = value;
80  }
81  return array;
82  }
83 
87  [CLSCompliant(false)]
88  [__DynamicallyInvokable]
89  public static byte[] GetBytes(ushort value)
90  {
91  return GetBytes((short)value);
92  }
93 
97  [CLSCompliant(false)]
98  [__DynamicallyInvokable]
99  public static byte[] GetBytes(uint value)
100  {
101  return GetBytes((int)value);
102  }
103 
107  [CLSCompliant(false)]
108  [__DynamicallyInvokable]
109  public static byte[] GetBytes(ulong value)
110  {
111  return GetBytes((long)value);
112  }
113 
117  [SecuritySafeCritical]
118  [__DynamicallyInvokable]
119  public unsafe static byte[] GetBytes(float value)
120  {
121  return GetBytes(*(int*)(&value));
122  }
123 
127  [SecuritySafeCritical]
128  [__DynamicallyInvokable]
129  public unsafe static byte[] GetBytes(double value)
130  {
131  return GetBytes(*(long*)(&value));
132  }
133 
144  [__DynamicallyInvokable]
145  public static char ToChar(byte[] value, int startIndex)
146  {
147  if (value == null)
148  {
149  ThrowHelper.ThrowArgumentNullException(ExceptionArgument.value);
150  }
151  if ((uint)startIndex >= value.Length)
152  {
153  ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startIndex, ExceptionResource.ArgumentOutOfRange_Index);
154  }
155  if (startIndex > value.Length - 2)
156  {
157  ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall);
158  }
159  return (char)ToInt16(value, startIndex);
160  }
161 
172  [SecuritySafeCritical]
173  [__DynamicallyInvokable]
174  public unsafe static short ToInt16(byte[] value, int startIndex)
175  {
176  if (value == null)
177  {
178  ThrowHelper.ThrowArgumentNullException(ExceptionArgument.value);
179  }
180  if ((uint)startIndex >= value.Length)
181  {
182  ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startIndex, ExceptionResource.ArgumentOutOfRange_Index);
183  }
184  if (startIndex > value.Length - 2)
185  {
186  ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall);
187  }
188  fixed (byte* ptr = &value[startIndex])
189  {
190  if (startIndex % 2 == 0)
191  {
192  return *(short*)ptr;
193  }
194  if (IsLittleEndian)
195  {
196  return (short)(*ptr | (ptr[1] << 8));
197  }
198  return (short)((*ptr << 8) | ptr[1]);
199  }
200  }
201 
212  [SecuritySafeCritical]
213  [__DynamicallyInvokable]
214  public unsafe static int ToInt32(byte[] value, int startIndex)
215  {
216  if (value == null)
217  {
218  ThrowHelper.ThrowArgumentNullException(ExceptionArgument.value);
219  }
220  if ((uint)startIndex >= value.Length)
221  {
222  ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startIndex, ExceptionResource.ArgumentOutOfRange_Index);
223  }
224  if (startIndex > value.Length - 4)
225  {
226  ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall);
227  }
228  fixed (byte* ptr = &value[startIndex])
229  {
230  if (startIndex % 4 == 0)
231  {
232  return *(int*)ptr;
233  }
234  if (IsLittleEndian)
235  {
236  return *ptr | (ptr[1] << 8) | (ptr[2] << 16) | (ptr[3] << 24);
237  }
238  return (*ptr << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3];
239  }
240  }
241 
252  [SecuritySafeCritical]
253  [__DynamicallyInvokable]
254  public unsafe static long ToInt64(byte[] value, int startIndex)
255  {
256  if (value == null)
257  {
258  ThrowHelper.ThrowArgumentNullException(ExceptionArgument.value);
259  }
260  if ((uint)startIndex >= value.Length)
261  {
262  ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startIndex, ExceptionResource.ArgumentOutOfRange_Index);
263  }
264  if (startIndex > value.Length - 8)
265  {
266  ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall);
267  }
268  fixed (byte* ptr = &value[startIndex])
269  {
270  if (startIndex % 8 == 0)
271  {
272  return *(long*)ptr;
273  }
274  if (IsLittleEndian)
275  {
276  int num = *ptr | (ptr[1] << 8) | (ptr[2] << 16) | (ptr[3] << 24);
277  int num2 = ptr[4] | (ptr[5] << 8) | (ptr[6] << 16) | (ptr[7] << 24);
278  return (uint)num | ((long)num2 << 32);
279  }
280  int num3 = (*ptr << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3];
281  int num4 = (ptr[4] << 24) | (ptr[5] << 16) | (ptr[6] << 8) | ptr[7];
282  return (uint)num4 | ((long)num3 << 32);
283  }
284  }
285 
296  [CLSCompliant(false)]
297  [__DynamicallyInvokable]
298  public static ushort ToUInt16(byte[] value, int startIndex)
299  {
300  if (value == null)
301  {
302  ThrowHelper.ThrowArgumentNullException(ExceptionArgument.value);
303  }
304  if ((uint)startIndex >= value.Length)
305  {
306  ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startIndex, ExceptionResource.ArgumentOutOfRange_Index);
307  }
308  if (startIndex > value.Length - 2)
309  {
310  ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall);
311  }
312  return (ushort)ToInt16(value, startIndex);
313  }
314 
325  [CLSCompliant(false)]
326  [__DynamicallyInvokable]
327  public static uint ToUInt32(byte[] value, int startIndex)
328  {
329  if (value == null)
330  {
331  ThrowHelper.ThrowArgumentNullException(ExceptionArgument.value);
332  }
333  if ((uint)startIndex >= value.Length)
334  {
335  ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startIndex, ExceptionResource.ArgumentOutOfRange_Index);
336  }
337  if (startIndex > value.Length - 4)
338  {
339  ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall);
340  }
341  return (uint)ToInt32(value, startIndex);
342  }
343 
354  [CLSCompliant(false)]
355  [__DynamicallyInvokable]
356  public static ulong ToUInt64(byte[] value, int startIndex)
357  {
358  if (value == null)
359  {
360  ThrowHelper.ThrowArgumentNullException(ExceptionArgument.value);
361  }
362  if ((uint)startIndex >= value.Length)
363  {
364  ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startIndex, ExceptionResource.ArgumentOutOfRange_Index);
365  }
366  if (startIndex > value.Length - 8)
367  {
368  ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall);
369  }
370  return (ulong)ToInt64(value, startIndex);
371  }
372 
383  [SecuritySafeCritical]
384  [__DynamicallyInvokable]
385  public unsafe static float ToSingle(byte[] value, int startIndex)
386  {
387  if (value == null)
388  {
389  ThrowHelper.ThrowArgumentNullException(ExceptionArgument.value);
390  }
391  if ((uint)startIndex >= value.Length)
392  {
393  ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startIndex, ExceptionResource.ArgumentOutOfRange_Index);
394  }
395  if (startIndex > value.Length - 4)
396  {
397  ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall);
398  }
399  int num = ToInt32(value, startIndex);
400  return *(float*)(&num);
401  }
402 
413  [SecuritySafeCritical]
414  [__DynamicallyInvokable]
415  public unsafe static double ToDouble(byte[] value, int startIndex)
416  {
417  if (value == null)
418  {
419  ThrowHelper.ThrowArgumentNullException(ExceptionArgument.value);
420  }
421  if ((uint)startIndex >= value.Length)
422  {
423  ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startIndex, ExceptionResource.ArgumentOutOfRange_Index);
424  }
425  if (startIndex > value.Length - 8)
426  {
427  ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall);
428  }
429  long num = ToInt64(value, startIndex);
430  return *(double*)(&num);
431  }
432 
433  private static char GetHexValue(int i)
434  {
435  if (i < 10)
436  {
437  return (char)(i + 48);
438  }
439  return (char)(i - 10 + 65);
440  }
441 
453  [__DynamicallyInvokable]
454  public static string ToString(byte[] value, int startIndex, int length)
455  {
456  if (value == null)
457  {
458  throw new ArgumentNullException("value");
459  }
460  if (startIndex < 0 || (startIndex >= value.Length && startIndex > 0))
461  {
462  throw new ArgumentOutOfRangeException("startIndex", Environment.GetResourceString("ArgumentOutOfRange_StartIndex"));
463  }
464  if (length < 0)
465  {
466  throw new ArgumentOutOfRangeException("length", Environment.GetResourceString("ArgumentOutOfRange_GenericPositive"));
467  }
468  if (startIndex > value.Length - length)
469  {
470  throw new ArgumentException(Environment.GetResourceString("Arg_ArrayPlusOffTooSmall"));
471  }
472  if (length == 0)
473  {
474  return string.Empty;
475  }
476  if (length > 715827882)
477  {
478  throw new ArgumentOutOfRangeException("length", Environment.GetResourceString("ArgumentOutOfRange_LengthTooLarge", 715827882));
479  }
480  int num = length * 3;
481  char[] array = new char[num];
482  int num2 = 0;
483  int num3 = startIndex;
484  for (num2 = 0; num2 < num; num2 += 3)
485  {
486  byte b = value[num3++];
487  array[num2] = GetHexValue((int)b / 16);
488  array[num2 + 1] = GetHexValue((int)b % 16);
489  array[num2 + 2] = '-';
490  }
491  return new string(array, 0, array.Length - 1);
492  }
493 
499  [__DynamicallyInvokable]
500  public static string ToString(byte[] value)
501  {
502  if (value == null)
503  {
504  throw new ArgumentNullException("value");
505  }
506  return ToString(value, 0, value.Length);
507  }
508 
517  [__DynamicallyInvokable]
518  public static string ToString(byte[] value, int startIndex)
519  {
520  if (value == null)
521  {
522  throw new ArgumentNullException("value");
523  }
524  return ToString(value, startIndex, value.Length - startIndex);
525  }
526 
536  [__DynamicallyInvokable]
537  public static bool ToBoolean(byte[] value, int startIndex)
538  {
539  if (value == null)
540  {
541  throw new ArgumentNullException("value");
542  }
543  if (startIndex < 0)
544  {
545  throw new ArgumentOutOfRangeException("startIndex", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
546  }
547  if (startIndex > value.Length - 1)
548  {
549  throw new ArgumentOutOfRangeException("startIndex", Environment.GetResourceString("ArgumentOutOfRange_Index"));
550  }
551  if (value[startIndex] != 0)
552  {
553  return true;
554  }
555  return false;
556  }
557 
561  [SecuritySafeCritical]
562  [__DynamicallyInvokable]
563  public unsafe static long DoubleToInt64Bits(double value)
564  {
565  return *(long*)(&value);
566  }
567 
571  [SecuritySafeCritical]
572  [__DynamicallyInvokable]
573  public unsafe static double Int64BitsToDouble(long value)
574  {
575  return *(double*)(&value);
576  }
577  }
578 }
static ushort ToUInt16(byte[] value, int startIndex)
Returns a 16-bit unsigned integer converted from two bytes at a specified position in a byte array.
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
static char ToChar(byte[] value, int startIndex)
Returns a Unicode character converted from two bytes at a specified position in a byte array.
static unsafe long ToInt64(byte[] value, int startIndex)
Returns a 64-bit signed integer converted from eight bytes at a specified position in a byte array.
static byte [] GetBytes(char value)
Returns the specified Unicode character value as an array of bytes.
Definition: BitConverter.cs:31
static string ToString(byte[] value)
Converts the numeric value of each element of a specified array of bytes to its equivalent hexadecima...
Definition: __Canon.cs:3
static unsafe long DoubleToInt64Bits(double value)
Converts the specified double-precision floating point number to a 64-bit signed integer.
The exception that is thrown when the value of an argument is outside the allowable range of values a...
static ulong ToUInt64(byte[] value, int startIndex)
Returns a 64-bit unsigned integer converted from eight bytes at a specified position in a byte array.
static byte [] GetBytes(ulong value)
Returns the specified 64-bit unsigned integer value as an array of bytes.
static unsafe byte [] GetBytes(short value)
Returns the specified 16-bit signed integer value as an array of bytes.
Definition: BitConverter.cs:41
static string ToString(byte[] value, int startIndex)
Converts the numeric value of each element of a specified subarray of bytes to its equivalent hexadec...
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
static unsafe double ToDouble(byte[] value, int startIndex)
Returns a double-precision floating point number converted from eight bytes at a specified position i...
static unsafe int ToInt32(byte[] value, int startIndex)
Returns a 32-bit signed integer converted from four bytes at a specified position in a byte array.
static byte [] GetBytes(ushort value)
Returns the specified 16-bit unsigned integer value as an array of bytes.
Definition: BitConverter.cs:89
static byte [] GetBytes(uint value)
Returns the specified 32-bit unsigned integer value as an array of bytes.
Definition: BitConverter.cs:99
Converts base data types to an array of bytes, and an array of bytes to base data types.
Definition: BitConverter.cs:7
static unsafe byte [] GetBytes(float value)
Returns the specified single-precision floating point value as an array of bytes.
static unsafe short ToInt16(byte[] value, int startIndex)
Returns a 16-bit signed integer converted from two bytes at a specified position in a byte array.
static unsafe byte [] GetBytes(int value)
Returns the specified 32-bit signed integer value as an array of bytes.
Definition: BitConverter.cs:57
static unsafe byte [] GetBytes(double value)
Returns the specified double-precision floating point value as an array of bytes.
The exception that is thrown when one of the arguments provided to a method is not valid.
static readonly bool IsLittleEndian
Indicates the byte order ("endianness") in which data is stored in this computer architecture.
Definition: BitConverter.cs:13
static uint ToUInt32(byte[] value, int startIndex)
Returns a 32-bit unsigned integer converted from four bytes at a specified position in a byte array.
static unsafe byte [] GetBytes(long value)
Returns the specified 64-bit signed integer value as an array of bytes.
Definition: BitConverter.cs:73
static unsafe double Int64BitsToDouble(long value)
Converts the specified 64-bit signed integer to a double-precision floating point number.
static unsafe float ToSingle(byte[] value, int startIndex)
Returns a single-precision floating point number converted from four bytes at a specified position in...
static byte [] GetBytes(bool value)
Returns the specified Boolean value as a byte array.
Definition: BitConverter.cs:19
static bool ToBoolean(byte[] value, int startIndex)
Returns a Boolean value converted from the byte at a specified position in a byte array.
static string ToString(byte[] value, int startIndex, int length)
Converts the numeric value of each element of a specified subarray of bytes to its equivalent hexadec...