5 // Derek Holden (dholden@draper.com)
\r
7 // (C) Ximian, Inc. http://www.ximian.com
\r
11 // System.Convert class. This was written word for word off the
\r
12 // Library specification for System.Convert in the ECMA TC39 TG2
\r
13 // and TG3 working documents. The first page of which has a table
\r
14 // for all legal conversion scenerios.
\r
16 // This header and the one above it can be formatted however, just trying
\r
17 // to keep it consistent w/ the existing mcs headers.
\r
19 // This Convert class could be written another way, with each type
\r
20 // implementing IConvertible and defining their own conversion functions,
\r
21 // and this class just calling the type's implementation. Or, they can
\r
22 // be defined here and the implementing type can use these functions when
\r
23 // defining their IConvertible interface. Byte's ToBoolean() calls
\r
24 // Convert.ToBoolean(byte), or Convert.ToBoolean(byte) calls
\r
25 // byte.ToBoolean(). The first case is what is done here.
\r
27 // See http://lists.ximian.com/archives/public/mono-list/2001-July/000525.html
\r
29 // There are also conversion functions that are not defined in
\r
30 // the ECMA draft, such as there is no bool ToBoolean(DateTime value),
\r
31 // and placing that somewhere won't compile w/ this Convert since the
\r
32 // function doesn't exist. However calling that when using Microsoft's
\r
33 // System.Convert doesn't produce any compiler errors, it just throws
\r
34 // an InvalidCastException at runtime.
\r
36 // Whenever a decimal, double, or single is converted to an integer
\r
37 // based type, it is even rounded. This uses Math.Round which only
\r
38 // has Round(decimal) and Round(double), so in the Convert from
\r
39 // single cases the value is passed to Math as a double. This
\r
40 // may not be completely necessary.
\r
42 // The .NET Framework SDK lists DBNull as a member of this class
\r
43 // as 'public static readonly object DBNull;'.
\r
45 // It should also be decided if all the cast return values should be
\r
46 // returned as unchecked or not.
\r
48 // All the XML function comments were auto generated which is why they
\r
49 // sound someone redundant.
\r
51 // TYPE | BOOL BYTE CHAR DT DEC DBL I16 I32 I64 SBYT SNGL STR UI16 UI32 UI64
\r
52 // -----+--------------------------------------------------------------------
\r
53 // BOOL | X X X X X X X X X X X X X
\r
54 // BYTE | X X X X X X X X X X X X X X
\r
55 // CHAR | X X X X X X X X X X
\r
57 // DEC | X X X X X X X X X X X X X
\r
58 // DBL | X X X X X X X X X X X X X
\r
59 // I16 | X X X X X X X X X X X X X X
\r
60 // I32 | X X X X X X X X X X X X X X
\r
61 // I64 | X X X X X X X X X X X X X X
\r
62 // SBYT | X X X X X X X X X X X X X X
\r
63 // SNGL | X X X X X X X X X X X X X
\r
64 // STR | X X X X X X X X X X X X X X X
\r
65 // UI16 | X X X X X X X X X X X X X X
\r
66 // UI32 | X X X X X X X X X X X X X X
\r
67 // UI64 | X X X X X X X X X X X X X X
\r
70 using System.Security.Cryptography;
\r
74 public sealed class Convert {
\r
76 // ========== BASE 64 Conversions ========== //
\r
77 // the BASE64 convert methods are using the Base64 converting methods
\r
78 // from System.Security.Cryptography.ToBase64Transform and
\r
79 // System.Security.Cryptography.FromBase64Transform
\r
81 // should be changed to a stand-alone class Base64Encoder & Base64Decoder
\r
83 public static byte[] FromBase64CharArray(char[] inArray, int offset, int length)
\r
85 if (inArray == null)
\r
86 throw new ArgumentNullException();
\r
88 int len = inArray.Length;
\r
89 if (len < 4 || len % 4 != 0)
\r
90 throw new FormatException();
\r
92 byte[] inArr = new System.Text.UnicodeEncoding().GetBytes(inArray);
\r
93 FromBase64Transform t = new FromBase64Transform();
\r
95 return t.TransformFinalBlock(inArr, 0, inArr.Length);
\r
98 public static byte[] FromBase64String(string s)
\r
101 throw new ArgumentNullException();
\r
103 char[] inArr = s.ToCharArray();
\r
105 return FromBase64CharArray(inArr, 0, inArr.Length);
\r
108 public static int ToBase64CharArray(byte[] inArray, int offsetIn, int length,
\r
109 char[] outArray, int offsetOut)
\r
111 if (inArray == null || outArray == null)
\r
112 throw new ArgumentNullException();
\r
114 if (offsetIn < 0 || length < 0 || offsetOut < 0 || (offsetIn + length) > inArray.Length)
\r
115 throw new ArgumentOutOfRangeException();
\r
117 ToBase64Transform t = new ToBase64Transform();
\r
118 byte[] outArr = t.TransformFinalBlock(inArray, offsetIn, length);
\r
120 char[] cOutArr = new System.Text.ASCIIEncoding().GetChars(outArr);
\r
122 if ((offsetOut + cOutArr.Length) > outArray.Length)
\r
123 throw new ArgumentOutOfRangeException();
\r
125 Array.Copy(cOutArr, 0, outArray, offsetOut, cOutArr.Length);
\r
127 return cOutArr.Length;
\r
130 public static string ToBase64String(byte[] inArray)
\r
132 if (inArray == null)
\r
133 throw new ArgumentNullException();
\r
135 return ToBase64String(inArray, 0, inArray.Length);
\r
138 public static string ToBase64String(byte[] inArray, int offset, int length)
\r
140 if (inArray == null)
\r
141 throw new ArgumentNullException();
\r
143 if (offset < 0 || length < 0 || (offset + length) > inArray.Length)
\r
144 throw new ArgumentOutOfRangeException();
\r
146 // FIXME: change to stand alone Base64 Encoder class
\r
147 ToBase64Transform t = new ToBase64Transform();
\r
148 byte[] outArr = t.TransformFinalBlock(inArray, offset, length);
\r
150 return (new System.Text.ASCIIEncoding().GetString(outArr));
\r
153 // ========== Boolean Conversions ========== //
\r
155 public static bool ToBoolean (bool value)
\r
160 public static bool ToBoolean (byte value)
\r
162 return (value != 0);
\r
165 public static bool ToBoolean (decimal value)
\r
167 return (value != 0M);
\r
170 public static bool ToBoolean (double value)
\r
172 return (value != 0);
\r
175 public static bool ToBoolean (float value)
\r
177 return (value != 0f);
\r
180 public static bool ToBoolean (int value)
\r
182 return (value != 0);
\r
185 public static bool ToBoolean (long value)
\r
187 return (value != 0);
\r
190 public static bool ToBoolean (sbyte value)
\r
192 return (value != 0);
\r
195 public static bool ToBoolean (short value)
\r
197 return (value != 0);
\r
200 public static bool ToBoolean (string value)
\r
202 return Boolean.Parse (value);
\r
205 public static bool ToBoolean (uint value)
\r
207 return (value != 0);
\r
210 public static bool ToBoolean (ulong value)
\r
212 return (value != 0);
\r
215 public static bool ToBoolean (ushort value)
\r
217 return (value != 0);
\r
220 // ========== Byte Conversions ========== //
\r
222 public static byte ToByte (bool value)
\r
224 return (byte)(value ? 1 : 0);
\r
227 public static byte ToByte (byte value)
\r
232 public static byte ToByte (char value)
\r
234 if (value > Byte.MaxValue)
\r
235 throw new OverflowException
\r
236 ("Value is greater than Byte.MaxValue");
\r
238 return (byte)value;
\r
241 public static byte ToByte (decimal value)
\r
243 if (value > Byte.MaxValue || value < Byte.MinValue)
\r
244 throw new OverflowException
\r
245 ("Value is greater than Byte.MaxValue or less than Byte.MinValue");
\r
247 // Returned Even-Rounded
\r
248 return (byte)(Math.Round (value));
\r
251 public static byte ToByte (double value)
\r
253 if (value > Byte.MaxValue || value < Byte.MinValue)
\r
254 throw new OverflowException
\r
255 ("Value is greater than Byte.MaxValue or less than Byte.MinValue");
\r
257 // This and the float version of ToByte are the only ones
\r
258 // the spec listed as checking for .NaN and Infinity overflow
\r
259 if (Double.IsNaN(value) || Double.IsInfinity(value))
\r
260 throw new OverflowException
\r
261 ("Value is equal to Double.NaN, Double.PositiveInfinity, or Double.NegativeInfinity");
\r
263 // Returned Even-Rounded
\r
264 return (byte)(Math.Round (value));
\r
267 public static byte ToByte (float value)
\r
269 if (value > Byte.MaxValue || value < Byte.MinValue)
\r
270 throw new OverflowException
\r
271 ("Value is greater than Byte.MaxValue or less than Byte.Minalue");
\r
273 // This and the double version of ToByte are the only ones
\r
274 // the spec listed as checking for .NaN and Infinity overflow
\r
275 if (Single.IsNaN(value) || Single.IsInfinity(value))
\r
276 throw new OverflowException
\r
277 ("Value is equal to Single.NaN, Single.PositiveInfinity, or Single.NegativeInfinity");
\r
279 // Returned Even-Rounded, pass it as a double, could have this
\r
280 // method just call Convert.ToByte ( (double)value)
\r
281 return (byte)(Math.Round ( (double)value));
\r
284 public static byte ToByte (int value)
\r
286 if (value > Byte.MaxValue || value < Byte.MinValue)
\r
287 throw new OverflowException
\r
288 ("Value is greater than Byte.MaxValue or less than Byte.MinValue");
\r
290 return (byte)value;
\r
293 public static byte ToByte (long value)
\r
295 if (value > Byte.MaxValue || value < Byte.MinValue)
\r
296 throw new OverflowException
\r
297 ("Value is greater than Byte.MaxValue or less than Byte.MinValue");
\r
299 return (byte)value;
\r
302 public static byte ToByte (sbyte value)
\r
304 if (value < Byte.MinValue)
\r
305 throw new OverflowException
\r
306 ("Value is less than Byte.MinValue");
\r
308 return (byte)value;
\r
311 public static byte ToByte (short value)
\r
313 if (value > Byte.MaxValue || value < Byte.MinValue)
\r
314 throw new OverflowException
\r
315 ("Value is greater than Byte.MaxValue or less than Byte.MinValue");
\r
317 return (byte)value;
\r
320 public static byte ToByte (string value)
\r
322 return Byte.Parse (value);
\r
325 public static byte ToByte (string value, IFormatProvider provider)
\r
327 return Byte.Parse (value, provider);
\r
330 public static byte ToByte (uint value)
\r
332 if (value > Byte.MaxValue)
\r
333 throw new OverflowException
\r
334 ("Value is greater than Byte.MaxValue");
\r
336 return (byte)value;
\r
339 public static byte ToByte (ulong value)
\r
341 if (value > Byte.MaxValue)
\r
342 throw new OverflowException
\r
343 ("Value is greater than Byte.MaxValue");
\r
345 return (byte)value;
\r
348 public static byte ToByte (ushort value)
\r
350 if (value > Byte.MaxValue)
\r
351 throw new OverflowException
\r
352 ("Value is greater than Byte.MaxValue");
\r
354 return (byte)value;
\r
357 // ========== Char Conversions ========== //
\r
359 public static char ToChar (byte value)
\r
361 return (char)value;
\r
364 public static char ToChar (char value)
\r
369 public static char ToChar (int value)
\r
371 if (value > Char.MaxValue || value < Char.MinValue)
\r
372 throw new OverflowException
\r
373 ("Value is greater than Char.MaxValue or less than Char.MinValue");
\r
375 return (char)value;
\r
378 public static char ToChar (long value)
\r
380 if (value > Char.MaxValue || value < Char.MinValue)
\r
381 throw new OverflowException
\r
382 ("Value is greater than Char.MaxValue or less than Char.MinValue");
\r
384 return (char)value;
\r
387 public static char ToChar (sbyte value)
\r
389 if (value < Char.MinValue)
\r
390 throw new OverflowException
\r
391 ("Value is less than Char.MinValue");
\r
393 return (char)value;
\r
396 public static char ToChar (short value)
\r
398 if (value < Char.MinValue)
\r
399 throw new OverflowException
\r
400 ("Value is less than Char.MinValue");
\r
402 return (char)value;
\r
405 public static char ToChar (string value)
\r
407 return Char.Parse (value);
\r
410 public static char ToChar (uint value)
\r
412 if (value > Char.MaxValue)
\r
413 throw new OverflowException
\r
414 ("Value is greater than Char.MaxValue");
\r
416 return (char)value;
\r
419 public static char ToChar (ulong value)
\r
421 if (value > Char.MaxValue)
\r
422 throw new OverflowException
\r
423 ("Value is greater than Char.MaxValue");
\r
425 return (char)value;
\r
428 public static char ToChar (ushort value)
\r
430 if (value > Char.MaxValue)
\r
431 throw new OverflowException
\r
432 ("Value is greater than Char.MaxValue");
\r
434 return (char)value;
\r
437 // ========== DateTime Conversions ========== //
\r
439 public static DateTime ToDateTime (string value)
\r
441 return DateTime.Parse (value);
\r
444 public static DateTime ToDateTime (string value, IFormatProvider provider)
\r
446 return DateTime.Parse (value, provider);
\r
449 // ========== Decimal Conversions ========== //
\r
451 public static decimal ToDecimal (bool value)
\r
453 return value ? 1 : 0;
\r
456 public static decimal ToDecimal (byte value)
\r
458 return (decimal)value;
\r
461 public static decimal ToDecimal (decimal value)
\r
466 public static decimal ToDecimal (double value)
\r
468 if (value > (double)Decimal.MaxValue || value < (double)Decimal.MinValue)
\r
469 throw new OverflowException
\r
470 ("Value is greater than Decimal.MaxValue or less than Decimal.MinValue");
\r
472 return (decimal)value;
\r
475 public static decimal ToDecimal (float value)
\r
477 if (value > (double)Decimal.MaxValue || value < (double)Decimal.MinValue)
\r
478 throw new OverflowException
\r
479 ("Value is greater than Decimal.MaxValue or less than Decimal.MinValue");
\r
481 return (decimal)value;
\r
484 public static decimal ToDecimal (int value)
\r
486 return (decimal)value;
\r
489 public static decimal ToDecimal (long value)
\r
491 return (decimal)value;
\r
494 public static decimal ToDecimal (sbyte value)
\r
496 return (decimal)value;
\r
499 public static decimal ToDecimal (short value)
\r
501 return (decimal)value;
\r
504 public static decimal ToDecimal (string value)
\r
506 return Decimal.Parse (value);
\r
509 public static decimal ToDecimal (string value, IFormatProvider provider)
\r
511 return Decimal.Parse (value, provider);
\r
514 public static decimal ToDecimal (uint value)
\r
516 return (decimal)value;
\r
519 public static decimal ToDecimal (ulong value)
\r
521 return (decimal)value;
\r
524 public static decimal ToDecimal (ushort value)
\r
526 return (decimal)value;
\r
529 // ========== Double Conversions ========== //
\r
531 public static double ToDouble (bool value)
\r
533 return value ? 1 : 0;
\r
536 public static double ToDouble (byte value)
\r
538 return (double)value;
\r
541 public static double ToDouble (decimal value)
\r
543 return (double)value;
\r
546 public static double ToDouble (double value)
\r
551 public static double ToDouble (float value)
\r
553 return (double)value;
\r
556 public static double ToDouble (int value)
\r
558 return (double)value;
\r
561 public static double ToDouble (long value)
\r
563 return (double)value;
\r
566 public static double ToDouble (sbyte value)
\r
568 return (double)value;
\r
571 public static double ToDouble (short value)
\r
573 return (double)value;
\r
576 public static double ToDouble (string value)
\r
578 return Double.Parse (value);
\r
581 public static double ToDouble (string value, IFormatProvider provider)
\r
583 return Double.Parse (value, provider);
\r
586 public static double ToDouble (uint value)
\r
588 return (double)value;
\r
591 public static double ToDouble (ulong value)
\r
593 return (double)value;
\r
596 public static double ToDouble (ushort value)
\r
598 return (double)value;
\r
601 // ========== Int16 Conversions ========== //
\r
603 public static short ToInt16 (bool value)
\r
605 return (short)(value ? 1 : 0);
\r
608 public static short ToInt16 (byte value)
\r
610 return (short)value;
\r
613 public static short ToInt16 (char value)
\r
615 if (value > Int16.MaxValue)
\r
616 throw new OverflowException
\r
617 ("Value is greater than Int16.MaxValue");
\r
619 return (short)value;
\r
622 public static short ToInt16 (decimal value)
\r
624 if (value > Int16.MaxValue || value < Int16.MinValue)
\r
625 throw new OverflowException
\r
626 ("Value is greater than Int16.MaxValue or less than Int16.MinValue");
\r
628 // Returned Even-Rounded
\r
629 return (short)(Math.Round (value));
\r
632 public static short ToInt16 (double value)
\r
634 if (value > Int16.MaxValue || value < Int16.MinValue)
\r
635 throw new OverflowException
\r
636 ("Value is greater than Int16.MaxValue or less than Int16.MinValue");
\r
638 // Returned Even-Rounded
\r
639 return (short)(Math.Round (value));
\r
642 public static short ToInt16 (float value)
\r
644 if (value > Int16.MaxValue || value < Int16.MinValue)
\r
645 throw new OverflowException
\r
646 ("Value is greater than Int16.MaxValue or less than Int16.MinValue");
\r
648 // Returned Even-Rounded, use Math.Round pass as a double.
\r
649 return (short)Math.Round ( (double)value);
\r
652 public static short ToInt16 (int value)
\r
654 if (value > Int16.MaxValue || value < Int16.MinValue)
\r
655 throw new OverflowException
\r
656 ("Value is greater than Int16.MaxValue or less than Int16.MinValue");
\r
658 return (short)value;
\r
661 public static short ToInt16 (long value)
\r
663 if (value > Int16.MaxValue || value < Int16.MinValue)
\r
664 throw new OverflowException
\r
665 ("Value is greater than Int16.MaxValue or less than Int16.MinValue");
\r
667 return (short)value;
\r
670 public static short ToInt16 (sbyte value)
\r
672 return (short)value;
\r
675 public static short ToInt16 (short value)
\r
680 public static short ToInt16 (string value)
\r
682 return Int16.Parse (value);
\r
685 public static short ToInt16 (string value, IFormatProvider provider)
\r
687 return Int16.Parse (value, provider);
\r
690 public static short ToInt16 (uint value)
\r
692 if (value > Int16.MaxValue)
\r
693 throw new OverflowException
\r
694 ("Value is greater than Int16.MaxValue");
\r
696 return (short)value;
\r
699 public static short ToInt16 (ulong value)
\r
701 if (value > (ulong)Int16.MaxValue)
\r
702 throw new OverflowException
\r
703 ("Value is greater than Int16.MaxValue");
\r
705 return (short)value;
\r
708 public static short ToInt16 (ushort value)
\r
710 if (value > Int16.MaxValue)
\r
711 throw new OverflowException
\r
712 ("Value is greater than Int16.MaxValue");
\r
714 return (short)value;
\r
717 // ========== Int32 Conversions ========== //
\r
719 public static int ToInt32 (bool value)
\r
721 return value ? 1 : 0;
\r
724 public static int ToInt32 (byte value)
\r
726 return (int)value;
\r
729 public static int ToInt32 (char value)
\r
731 return (int)value;
\r
734 public static int ToInt32 (decimal value)
\r
736 if (value > Int32.MaxValue || value < Int32.MinValue)
\r
737 throw new OverflowException
\r
738 ("Value is greater than Int32.MaxValue or less than Int32.MinValue");
\r
740 // Returned Even-Rounded
\r
741 return (int)(Math.Round (value));
\r
744 public static int ToInt32 (double value)
\r
746 if (value > Int32.MaxValue || value < Int32.MinValue)
\r
747 throw new OverflowException
\r
748 ("Value is greater than Int32.MaxValue or less than Int32.MinValue");
\r
750 // Returned Even-Rounded
\r
751 return (int)(Math.Round (value));
\r
754 public static int ToInt32 (float value)
\r
756 if (value > Int32.MaxValue || value < Int32.MinValue)
\r
757 throw new OverflowException
\r
758 ("Value is greater than Int32.MaxValue or less than Int32.MinValue");
\r
760 // Returned Even-Rounded, pass as a double, could just call
\r
761 // Convert.ToInt32 ( (double)value);
\r
762 return (int)(Math.Round ( (double)value));
\r
765 public static int ToInt32 (int value)
\r
770 public static int ToInt32 (long value)
\r
772 if (value > Int32.MaxValue || value < Int32.MinValue)
\r
773 throw new OverflowException
\r
774 ("Value is greater than Int32.MaxValue or less than Int32.MinValue");
\r
776 return (int)value;
\r
779 public static int ToInt32 (sbyte value)
\r
781 return (int)value;
\r
784 public static int ToInt32 (short value)
\r
786 return (int)value;
\r
789 public static int ToInt32 (string value)
\r
791 return Int32.Parse (value);
\r
794 public static int ToInt32 (string value, IFormatProvider provider)
\r
796 return Int32.Parse (value, provider);
\r
799 public static int ToInt32 (uint value)
\r
801 if (value > Int32.MaxValue)
\r
802 throw new OverflowException
\r
803 ("Value is greater than Int32.MaxValue");
\r
805 return (int)value;
\r
808 public static int ToInt32 (ulong value)
\r
810 if (value > Int32.MaxValue)
\r
811 throw new OverflowException
\r
812 ("Value is greater than Int32.MaxValue");
\r
814 return (int)value;
\r
817 public static int ToInt32 (ushort value)
\r
819 return (int)value;
\r
822 // ========== Int64 Conversions ========== //
\r
824 public static long ToInt64 (bool value)
\r
826 return value ? 1 : 0;
\r
829 public static long ToInt64 (byte value)
\r
831 return (long)value;
\r
834 public static long ToInt64 (char value)
\r
836 return (long)value;
\r
839 public static long ToInt64 (decimal value)
\r
841 if (value > Int64.MaxValue || value < Int64.MinValue)
\r
842 throw new OverflowException
\r
843 ("Value is greater than Int64.MaxValue or less than Int64.MinValue");
\r
845 // Returned Even-Rounded
\r
846 return (long)(Math.Round (value));
\r
849 public static long ToInt64 (double value)
\r
851 if (value > Int64.MaxValue || value < Int64.MinValue)
\r
852 throw new OverflowException
\r
853 ("Value is greater than Int64.MaxValue or less than Int64.MinValue");
\r
855 // Returned Even-Rounded
\r
856 return (long)(Math.Round (value));
\r
859 public static long ToInt64 (float value)
\r
861 if (value > Int64.MaxValue || value < Int64.MinValue)
\r
862 throw new OverflowException
\r
863 ("Value is greater than Int64.MaxValue or less than Int64.MinValue");
\r
865 // Returned Even-Rounded, pass to Math as a double, could
\r
866 // just call Convert.ToInt64 ( (double)value);
\r
867 return (long)(Math.Round ( (double)value));
\r
870 public static long ToInt64 (int value)
\r
872 return (long)value;
\r
875 public static long ToInt64 (long value)
\r
880 public static long ToInt64 (sbyte value)
\r
882 return (long)value;
\r
885 public static long ToInt64 (short value)
\r
887 return (long)value;
\r
890 public static long ToInt64 (string value)
\r
892 return Int64.Parse (value);
\r
895 public static long ToInt64 (string value, IFormatProvider provider)
\r
897 return Int64.Parse (value, provider);
\r
900 public static long ToInt64 (uint value)
\r
902 return (long)value;
\r
905 public static long ToInt64 (ulong value)
\r
907 if (value > Int64.MaxValue)
\r
908 throw new OverflowException
\r
909 ("Value is greater than Int64.MaxValue");
\r
911 return (long)value;
\r
914 public static long ToInt64 (ushort value)
\r
916 return (long)value;
\r
919 // ========== SByte Conversions ========== //
\r
921 public static sbyte ToSByte (bool value)
\r
923 return (sbyte)(value ? 1 : 0);
\r
926 public static sbyte ToSByte (byte value)
\r
928 if (value > SByte.MaxValue)
\r
929 throw new OverflowException
\r
930 ("Value is greater than SByte.MaxValue");
\r
932 return (sbyte)value;
\r
935 public static sbyte ToSByte (char value)
\r
937 if (value > SByte.MaxValue)
\r
938 throw new OverflowException
\r
939 ("Value is greater than SByte.MaxValue");
\r
941 return (sbyte)value;
\r
944 public static sbyte ToSByte (decimal value)
\r
946 if (value > SByte.MaxValue || value < SByte.MinValue)
\r
947 throw new OverflowException
\r
948 ("Value is greater than SByte.MaxValue or less than SByte.MinValue");
\r
950 // Returned Even-Rounded
\r
951 return (sbyte)(Math.Round (value));
\r
954 public static sbyte ToSByte (double value)
\r
956 if (value > SByte.MaxValue || value < SByte.MinValue)
\r
957 throw new OverflowException
\r
958 ("Value is greater than SByte.MaxValue or less than SByte.MinValue");
\r
960 // Returned Even-Rounded
\r
961 return (sbyte)(Math.Round (value));
\r
964 public static sbyte ToSByte (float value)
\r
966 if (value > SByte.MaxValue || value < SByte.MinValue)
\r
967 throw new OverflowException
\r
968 ("Value is greater than SByte.MaxValue or less than SByte.Minalue");
\r
970 // Returned Even-Rounded, pass as double to Math
\r
971 return (sbyte)(Math.Round ( (double)value));
\r
974 public static sbyte ToSByte (int value)
\r
976 if (value > SByte.MaxValue || value < SByte.MinValue)
\r
977 throw new OverflowException
\r
978 ("Value is greater than SByte.MaxValue or less than SByte.MinValue");
\r
980 return (sbyte)value;
\r
983 public static sbyte ToSByte (long value)
\r
985 if (value > SByte.MaxValue || value < SByte.MinValue)
\r
986 throw new OverflowException
\r
987 ("Value is greater than SByte.MaxValue or less than SByte.MinValue");
\r
989 return (sbyte)value;
\r
992 public static sbyte ToSByte (sbyte value)
\r
997 public static sbyte ToSByte (short value)
\r
999 if (value > SByte.MaxValue || value < SByte.MinValue)
\r
1000 throw new OverflowException
\r
1001 ("Value is greater than SByte.MaxValue or less than SByte.MinValue");
\r
1003 return (sbyte)value;
\r
1006 public static sbyte ToSByte (string value)
\r
1008 return SByte.Parse (value);
\r
1011 public static sbyte ToSByte (string value, IFormatProvider provider)
\r
1013 return SByte.Parse (value, provider);
\r
1016 public static sbyte ToSByte (uint value)
\r
1018 if (value > SByte.MaxValue)
\r
1019 throw new OverflowException
\r
1020 ("Value is greater than SByte.MaxValue");
\r
1022 return (sbyte)value;
\r
1025 public static sbyte ToSByte (ulong value)
\r
1027 if (value > (ulong)SByte.MaxValue)
\r
1028 throw new OverflowException
\r
1029 ("Value is greater than SByte.MaxValue");
\r
1031 return (sbyte)value;
\r
1034 public static sbyte ToSByte (ushort value)
\r
1036 if (value > SByte.MaxValue)
\r
1037 throw new OverflowException
\r
1038 ("Value is greater than SByte.MaxValue");
\r
1040 return (sbyte)value;
\r
1043 // ========== Single Conversions ========== //
\r
1045 public static float ToSingle (bool value)
\r
1047 return value ? 1 : 0;
\r
1050 public static float ToSingle (byte value)
\r
1052 return (float)value;
\r
1055 public static float ToSingle (decimal value)
\r
1057 return (float)value;
\r
1060 public static float ToSingle (double value)
\r
1062 if (value > Single.MaxValue || value < Single.MinValue)
\r
1063 throw new OverflowException
\r
1064 ("Value is greater than Single.MaxValue or less than Single.MinValue");
\r
1066 return (float)value;
\r
1069 public static float ToSingle (float value)
\r
1074 public static float ToSingle (int value)
\r
1076 return (float)value;
\r
1079 public static float ToSingle (long value)
\r
1081 return (float)value;
\r
1084 public static float ToSingle (sbyte value)
\r
1086 return (float)value;
\r
1089 public static float ToSingle (short value)
\r
1091 return (float)value;
\r
1094 public static float ToSingle (string value)
\r
1096 return Single.Parse (value);
\r
1099 public static float ToSingle (string value, IFormatProvider provider)
\r
1101 return Single.Parse (value, provider);
\r
1104 public static float ToSingle (uint value)
\r
1106 return (float)value;
\r
1109 public static float ToSingle (ulong value)
\r
1111 return (float)value;
\r
1114 public static float ToSingle (ushort value)
\r
1116 return (float)value;
\r
1119 // ========== String Conversions ========== //
\r
1121 public static string ToString (bool value)
\r
1123 return value.ToString ();
\r
1126 public static string ToString (byte value)
\r
1128 return value.ToString ();
\r
1131 public static string ToString (byte value, IFormatProvider provider)
\r
1133 return value.ToString (provider);
\r
1136 public static string ToString (char value)
\r
1138 return value.ToString ();
\r
1141 public static string ToString (DateTime value)
\r
1143 return value.ToString ();
\r
1146 public static string ToString (DateTime value, IFormatProvider provider)
\r
1148 return value.ToString (provider);
\r
1151 public static string ToString (decimal value)
\r
1153 return value.ToString ();
\r
1156 public static string ToString (decimal value, IFormatProvider provider)
\r
1158 return value.ToString (provider);
\r
1161 public static string ToString (double value)
\r
1163 return value.ToString ();
\r
1166 public static string ToString (double value, IFormatProvider provider)
\r
1168 return value.ToString (provider);
\r
1171 public static string ToString (float value)
\r
1173 return value.ToString ();
\r
1176 public static string ToString (float value, IFormatProvider provider)
\r
1178 return value.ToString (provider);
\r
1181 public static string ToString (int value)
\r
1183 return value.ToString ();
\r
1186 public static string ToString (int value, IFormatProvider provider)
\r
1188 return value.ToString (provider);
\r
1191 public static string ToString (long value)
\r
1193 return value.ToString ();
\r
1196 public static string ToString (long value, IFormatProvider provider)
\r
1198 return value.ToString (provider);
\r
1201 public static string ToString (sbyte value)
\r
1203 return value.ToString ();
\r
1206 public static string ToString (sbyte value, IFormatProvider provider)
\r
1208 return value.ToString (provider);
\r
1211 public static string ToString (short value)
\r
1213 return value.ToString ();
\r
1216 public static string ToString (short value, IFormatProvider provider)
\r
1218 return value.ToString (provider);
\r
1221 public static string ToString (string value)
\r
1226 public static string ToString (uint value)
\r
1228 return value.ToString ();
\r
1231 public static string ToString (uint value, IFormatProvider provider)
\r
1233 return value.ToString (provider);
\r
1236 public static string ToString (ulong value)
\r
1238 return value.ToString ();
\r
1241 public static string ToString (ulong value, IFormatProvider provider)
\r
1243 return value.ToString (provider);
\r
1246 public static string ToString (ushort value)
\r
1248 return value.ToString ();
\r
1251 public static string ToString (ushort value, IFormatProvider provider)
\r
1253 return value.ToString (provider);
\r
1256 // ========== UInt16 Conversions ========== //
\r
1258 public static ushort ToUInt16 (bool value)
\r
1260 return (ushort)(value ? 1 : 0);
\r
1263 public static ushort ToUInt16 (byte value)
\r
1265 return (ushort)value;
\r
1268 public static ushort ToUInt16 (char value)
\r
1270 return (ushort)value;
\r
1273 public static ushort ToUInt16 (decimal value)
\r
1275 if (value > UInt16.MaxValue || value < UInt16.MinValue)
\r
1276 throw new OverflowException
\r
1277 ("Value is greater than UInt16.MaxValue or less than UInt16.MinValue");
\r
1279 // Returned Even-Rounded
\r
1280 return (ushort)(Math.Round (value));
\r
1283 public static ushort ToUInt16 (double value)
\r
1285 if (value > UInt16.MaxValue || value < UInt16.MinValue)
\r
1286 throw new OverflowException
\r
1287 ("Value is greater than UInt16.MaxValue or less than UInt16.MinValue");
\r
1289 // Returned Even-Rounded
\r
1290 return (ushort)(Math.Round (value));
\r
1293 public static ushort ToUInt16 (float value)
\r
1295 if (value > UInt16.MaxValue || value < UInt16.MinValue)
\r
1296 throw new OverflowException
\r
1297 ("Value is greater than UInt16.MaxValue or less than UInt16.MinValue");
\r
1299 // Returned Even-Rounded, pass as double to Math
\r
1300 return (ushort)(Math.Round ( (double)value));
\r
1303 public static ushort ToUInt16 (int value)
\r
1305 if (value > UInt16.MaxValue || value < UInt16.MinValue)
\r
1306 throw new OverflowException
\r
1307 ("Value is greater than UInt16.MaxValue or less than UInt16.MinValue");
\r
1309 return (ushort)value;
\r
1312 public static ushort ToUInt16 (long value)
\r
1314 if (value > UInt16.MaxValue || value < UInt16.MinValue)
\r
1315 throw new OverflowException
\r
1316 ("Value is greater than UInt16.MaxValue or less than UInt16.MinValue");
\r
1318 return (ushort)value;
\r
1321 public static ushort ToUInt16 (sbyte value)
\r
1323 if (value < UInt16.MinValue)
\r
1324 throw new OverflowException
\r
1325 ("Value is less than UInt16.MinValue");
\r
1327 return (ushort)value;
\r
1330 public static ushort ToUInt16 (short value)
\r
1332 if (value < UInt16.MinValue)
\r
1333 throw new OverflowException
\r
1334 ("Value is less than UInt16.MinValue");
\r
1336 return (ushort)value;
\r
1339 public static ushort ToUInt16 (string value)
\r
1341 return UInt16.Parse (value);
\r
1344 public static ushort ToUInt16 (string value, IFormatProvider provider)
\r
1346 return UInt16.Parse (value, provider);
\r
1349 public static ushort ToUInt16 (uint value)
\r
1351 if (value > UInt16.MaxValue)
\r
1352 throw new OverflowException
\r
1353 ("Value is greater than UInt16.MaxValue");
\r
1355 return (ushort)value;
\r
1358 public static ushort ToUInt16 (ulong value)
\r
1360 if (value > (ulong)UInt16.MaxValue)
\r
1361 throw new OverflowException
\r
1362 ("Value is greater than UInt16.MaxValue");
\r
1364 return (ushort)value;
\r
1367 public static ushort ToUInt16 (ushort value)
\r
1372 // ========== UInt32 Conversions ========== //
\r
1374 public static uint ToUInt32 (bool value)
\r
1376 return (uint)(value ? 1 : 0);
\r
1379 public static uint ToUInt32 (byte value)
\r
1381 return (uint)value;
\r
1384 public static uint ToUInt32 (char value)
\r
1386 return (uint)value;
\r
1389 public static uint ToUInt32 (decimal value)
\r
1391 if (value > UInt32.MaxValue || value < UInt32.MinValue)
\r
1392 throw new OverflowException
\r
1393 ("Value is greater than UInt32.MaxValue or less than UInt32.MinValue");
\r
1395 // Returned Even-Rounded
\r
1396 return (uint)(Math.Round (value));
\r
1399 public static uint ToUInt32 (double value)
\r
1401 if (value > UInt32.MaxValue || value < UInt32.MinValue)
\r
1402 throw new OverflowException
\r
1403 ("Value is greater than UInt32.MaxValue or less than UInt32.MinValue");
\r
1405 // Returned Even-Rounded
\r
1406 return (uint)(Math.Round (value));
\r
1409 public static uint ToUInt32 (float value)
\r
1411 if (value > UInt32.MaxValue || value < UInt32.MinValue)
\r
1412 throw new OverflowException
\r
1413 ("Value is greater than UInt32.MaxValue or less than UInt32.MinValue");
\r
1415 // Returned Even-Rounded, pass as double to Math
\r
1416 return (uint)(Math.Round ( (double)value));
\r
1419 public static uint ToUInt32 (int value)
\r
1421 if (value < UInt32.MinValue)
\r
1422 throw new OverflowException
\r
1423 ("Value is less than UInt32.MinValue");
\r
1425 return (uint)value;
\r
1428 public static uint ToUInt32 (long value)
\r
1430 if (value > UInt32.MaxValue || value < UInt32.MinValue)
\r
1431 throw new OverflowException
\r
1432 ("Value is greater than UInt32.MaxValue or less than UInt32.MinValue");
\r
1434 return (uint)value;
\r
1437 public static uint ToUInt32 (sbyte value)
\r
1439 if (value < UInt32.MinValue)
\r
1440 throw new OverflowException
\r
1441 ("Value is less than UInt32.MinValue");
\r
1443 return (uint)value;
\r
1446 public static uint ToUInt32 (short value)
\r
1448 if (value < UInt32.MinValue)
\r
1449 throw new OverflowException
\r
1450 ("Value is less than UInt32.MinValue");
\r
1452 return (uint)value;
\r
1455 public static uint ToUInt32 (string value)
\r
1457 return UInt32.Parse (value);
\r
1460 public static uint ToUInt32 (string value, IFormatProvider provider)
\r
1462 return UInt32.Parse (value, provider);
\r
1465 public static uint ToUInt32 (uint value)
\r
1470 public static uint ToUInt32 (ulong value)
\r
1472 if (value > UInt32.MaxValue)
\r
1473 throw new OverflowException
\r
1474 ("Value is greater than UInt32.MaxValue");
\r
1476 return (uint)value;
\r
1479 public static uint ToUInt32 (ushort value)
\r
1481 return (uint)value;
\r
1484 // ========== UInt64 Conversions ========== //
\r
1486 public static ulong ToUInt64 (bool value)
\r
1488 return (ulong)(value ? 1 : 0);
\r
1491 public static ulong ToUInt64 (byte value)
\r
1493 return (ulong)value;
\r
1496 public static ulong ToUInt64 (char value)
\r
1498 return (ulong)value;
\r
1501 public static ulong ToUInt64 (decimal value)
\r
1503 if (value > UInt64.MaxValue || value < UInt64.MinValue)
\r
1504 throw new OverflowException
\r
1505 ("Value is greater than UInt64.MaxValue or less than UInt64.MinValue");
\r
1507 // Returned Even-Rounded
\r
1508 return (ulong)(Math.Round (value));
\r
1511 public static ulong ToUInt64 (double value)
\r
1513 if (value > UInt64.MaxValue || value < UInt64.MinValue)
\r
1514 throw new OverflowException
\r
1515 ("Value is greater than UInt64.MaxValue or less than UInt64.MinValue");
\r
1517 // Returned Even-Rounded
\r
1518 return (ulong)(Math.Round (value));
\r
1521 public static ulong ToUInt64 (float value)
\r
1523 if (value > UInt64.MaxValue || value < UInt64.MinValue)
\r
1524 throw new OverflowException
\r
1525 ("Value is greater than UInt64.MaxValue or less than UInt64.MinValue");
\r
1527 // Returned Even-Rounded, pass as a double to Math
\r
1528 return (ulong)(Math.Round ( (double)value));
\r
1531 public static ulong ToUInt64 (int value)
\r
1533 if (value < (int)UInt64.MinValue)
\r
1534 throw new OverflowException
\r
1535 ("Value is less than UInt64.MinValue");
\r
1537 return (ulong)value;
\r
1540 public static ulong ToUInt64 (long value)
\r
1542 if (value < (long)UInt64.MinValue)
\r
1543 throw new OverflowException
\r
1544 ("Value is less than UInt64.MinValue");
\r
1546 return (ulong)value;
\r
1549 public static ulong ToUInt64 (sbyte value)
\r
1551 if (value < (sbyte)UInt64.MinValue)
\r
1552 throw new OverflowException
\r
1553 ("Value is less than UInt64.MinValue");
\r
1555 return (ulong)value;
\r
1558 public static ulong ToUInt64 (short value)
\r
1560 if (value < (short)UInt64.MinValue)
\r
1561 throw new OverflowException
\r
1562 ("Value is less than UInt64.MinValue");
\r
1564 return (ulong)value;
\r
1567 public static ulong ToUInt64 (string value)
\r
1569 return UInt64.Parse (value);
\r
1572 public static ulong ToUInt64 (string value, IFormatProvider provider)
\r
1574 return UInt64.Parse (value, provider);
\r
1577 public static ulong ToUInt64 (uint value)
\r
1579 return (ulong)value;
\r
1582 public static ulong ToUInt64 (ulong value)
\r
1587 public static ulong ToUInt64 (ushort value)
\r
1589 return (ulong)value;
\r
1592 // ========== Conversion / Helper Fucntions ========== //
\r
1594 // Lookup table for the conversion ToType method. Order
\r
1595 // is important! Used by ToType for comparing the target
\r
1596 // type, and uses hardcoded array indexes.
\r
1597 private static Type[] conversionTable = {
\r
1598 // Valid ICovnertible Types
\r
1599 typeof (Boolean), // 0 TypeCode.Boolean
\r
1600 typeof (Byte), // 1 TypeCode.Byte
\r
1601 typeof (Char), // 2 TypeCode.Char
\r
1602 typeof (DateTime), // 3 TypeCode.DateTime
\r
1603 typeof (Decimal), // 4 TypeCode.Decimal
\r
1604 typeof (Double), // 5 TypeCode.Double
\r
1605 typeof (Int16), // 6 TypeCode.Int16
\r
1606 typeof (Int32), // 7 TypeCode.Int32
\r
1607 typeof (Int64), // 8 TypeCode.Int64
\r
1608 typeof (SByte), // 9 TypeCode.Sbyte
\r
1609 typeof (Single), // 10 TypeCode.Single
\r
1610 typeof (String), // 11 TypeCode.String
\r
1611 typeof (UInt16), // 12 TypeCode.UInt16
\r
1612 typeof (UInt32), // 13 TypeCode.UInt32
\r
1613 typeof (UInt64), // 14 TypeCode.UInt64
\r
1615 // Invalid IConvertible Interface Types
\r
1616 typeof (Object) // 15 TypeCode.Object
\r
1619 // Function to convert an object to another type and return
\r
1620 // it as an object. In place for the core data types to use
\r
1621 // when implementing IConvertible. Uses hardcoded indexes in
\r
1622 // the conversionTypes array, so if modify carefully.
\r
1623 internal static object ToType (object value, Type conversionType,
\r
1624 IFormatProvider provider)
\r
1626 if (value == null)
\r
1627 throw new ArgumentException
\r
1628 ("Invalid conversion from null value");
\r
1630 if (value is IConvertible) {
\r
1631 IConvertible convertValue = (IConvertible)value;
\r
1633 if (conversionType == conversionTable[0]) {
\r
1634 // 0 TypeCode.Boolean
\r
1635 return (object)(convertValue.ToBoolean (provider));
\r
1637 } else if (conversionType == conversionTable[1]) {
\r
1638 // 1 TypeCode.Byte
\r
1639 return (object)(convertValue.ToByte (provider));
\r
1641 } else if (conversionType == conversionTable[2]) {
\r
1642 // 2 TypeCode.Char
\r
1643 return (object)(convertValue.ToChar (provider));
\r
1645 } else if (conversionType == conversionTable[3]) {
\r
1646 // 3 TypeCode.DateTime
\r
1647 return (object)(convertValue.ToDateTime (provider));
\r
1649 } else if (conversionType == conversionTable[4]) {
\r
1650 // 4 TypeCode.Decimal
\r
1651 return (object)(convertValue.ToDecimal (provider));
\r
1653 } else if (conversionType == conversionTable[5]) {
\r
1654 // 5 TypeCode.Double
\r
1655 return (object)(convertValue.ToDouble (provider));
\r
1657 } else if (conversionType == conversionTable[6]) {
\r
1658 // 6 TypeCode.Int16
\r
1659 return (object)(convertValue.ToInt16 (provider));
\r
1661 } else if (conversionType == conversionTable[7]) {
\r
1662 // 7 TypeCode.Int32
\r
1663 return (object)(convertValue.ToInt32 (provider));
\r
1665 } else if (conversionType == conversionTable[8]) {
\r
1666 // 8 TypeCode.Int64
\r
1667 return (object)(convertValue.ToInt64 (provider));
\r
1669 } else if (conversionType == conversionTable[9]) {
\r
1670 // 9 TypeCode.Sbyte
\r
1671 return (object)(convertValue.ToSByte (provider));
\r
1673 } else if (conversionType == conversionTable[10]) {
\r
1674 // 10 TypeCode.Single
\r
1675 return (object)(convertValue.ToSingle (provider));
\r
1677 } else if (conversionType == conversionTable[11]) {
\r
1678 // 11 TypeCode.String
\r
1679 return (object)(convertValue.ToString (provider));
\r
1681 } else if (conversionType == conversionTable[12]) {
\r
1682 // 12 TypeCode.UInt16
\r
1683 return (object)(convertValue.ToUInt16 (provider));
\r
1685 } else if (conversionType == conversionTable[13]) {
\r
1686 // 13 TypeCode.UInt32
\r
1687 return (object)(convertValue.ToUInt32 (provider));
\r
1689 } else if (conversionType == conversionTable[14]) {
\r
1690 // 14 TypeCode.UInt64
\r
1691 return (object)(convertValue.ToUInt64 (provider));
\r
1693 } else if (conversionType == conversionTable[15]) {
\r
1694 // 15 TypeCode.Object
\r
1695 return (object)(value);
\r
1698 // Not in the conversion table
\r
1699 throw new InvalidCastException
\r
1700 ("Unknown target conversion type");
\r
1703 // Value is not IConvertible
\r
1704 throw new ArgumentException
\r
1705 ("Value is not a convertible object");
\r