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
71 using System.Globalization;
\r
75 [CLSCompliant(false)]
\r
76 public sealed class Convert {
\r
78 // ========== BASE 64 Conversions ========== //
\r
79 // the BASE64 convert methods are using the Base64 converting methods
\r
80 // from System.Security.Cryptography.ToBase64Transform and
\r
81 // System.Security.Cryptography.FromBase64Transform
\r
83 // should be changed to a stand-alone class Base64Encoder & Base64Decoder
\r
85 public static byte[] FromBase64CharArray(char[] inArray, int offset, int length)
\r
87 if (inArray == null)
\r
88 throw new ArgumentNullException();
\r
90 int len = inArray.Length;
\r
91 if (len < 4 || len % 4 != 0)
\r
92 throw new FormatException();
\r
94 byte[] inArr = new System.Text.UnicodeEncoding().GetBytes(inArray);
\r
95 FromBase64Transform t = new FromBase64Transform();
\r
97 return t.TransformFinalBlock(inArr, 0, inArr.Length);
\r
100 public static byte[] FromBase64String(string s)
\r
103 throw new ArgumentNullException();
\r
105 char[] inArr = s.ToCharArray();
\r
107 return FromBase64CharArray(inArr, 0, inArr.Length);
\r
110 public static int ToBase64CharArray(byte[] inArray, int offsetIn, int length,
\r
111 char[] outArray, int offsetOut)
\r
113 if (inArray == null || outArray == null)
\r
114 throw new ArgumentNullException();
\r
116 if (offsetIn < 0 || length < 0 || offsetOut < 0 || (offsetIn + length) > inArray.Length)
\r
117 throw new ArgumentOutOfRangeException();
\r
119 ToBase64Transform t = new ToBase64Transform();
\r
120 byte[] outArr = t.TransformFinalBlock(inArray, offsetIn, length);
\r
122 char[] cOutArr = new System.Text.ASCIIEncoding().GetChars(outArr);
\r
124 if ((offsetOut + cOutArr.Length) > outArray.Length)
\r
125 throw new ArgumentOutOfRangeException();
\r
127 Array.Copy(cOutArr, 0, outArray, offsetOut, cOutArr.Length);
\r
129 return cOutArr.Length;
\r
132 public static string ToBase64String(byte[] inArray)
\r
134 if (inArray == null)
\r
135 throw new ArgumentNullException();
\r
137 return ToBase64String(inArray, 0, inArray.Length);
\r
140 public static string ToBase64String(byte[] inArray, int offset, int length)
\r
142 if (inArray == null)
\r
143 throw new ArgumentNullException();
\r
145 if (offset < 0 || length < 0 || (offset + length) > inArray.Length)
\r
146 throw new ArgumentOutOfRangeException();
\r
148 // FIXME: change to stand alone Base64 Encoder class
\r
149 ToBase64Transform t = new ToBase64Transform();
\r
150 byte[] outArr = t.TransformFinalBlock(inArray, offset, length);
\r
152 return (new System.Text.ASCIIEncoding().GetString(outArr));
\r
155 // ========== Boolean Conversions ========== //
\r
157 public static bool ToBoolean (bool value)
\r
162 public static bool ToBoolean (byte value)
\r
164 return (value != 0);
\r
167 public static bool ToBoolean (char value)
\r
169 throw new InvalidCastException (Locale.GetText ("Can't convert char to bool"));
\r
172 public static bool ToBoolean (DateTime value)
\r
174 throw new InvalidCastException (Locale.GetText ("Can't convert date to bool"));
\r
177 public static bool ToBoolean (decimal value)
\r
179 return (value != 0M);
\r
182 public static bool ToBoolean (double value)
\r
184 return (value != 0);
\r
187 public static bool ToBoolean (float value)
\r
189 return (value != 0f);
\r
192 public static bool ToBoolean (int value)
\r
194 return (value != 0);
\r
197 public static bool ToBoolean (long value)
\r
199 return (value != 0);
\r
202 public static bool ToBoolean (sbyte value)
\r
204 return (value != 0);
\r
207 public static bool ToBoolean (short value)
\r
209 return (value != 0);
\r
212 public static bool ToBoolean (string value)
\r
214 return Boolean.Parse (value);
\r
217 public static bool ToBoolean (uint value)
\r
219 return (value != 0);
\r
222 public static bool ToBoolean (ulong value)
\r
224 return (value != 0);
\r
227 public static bool ToBoolean (ushort value)
\r
229 return (value != 0);
\r
232 // ========== Byte Conversions ========== //
\r
234 public static byte ToByte (bool value)
\r
236 return (byte)(value ? 1 : 0);
\r
239 public static byte ToByte (byte value)
\r
244 public static byte ToByte (char value)
\r
246 if (value > Byte.MaxValue)
\r
247 throw new OverflowException (Locale.GetText (
\r
248 "Value is greater than Byte.MaxValue"));
\r
250 return (byte)value;
\r
253 public static byte ToByte (decimal value)
\r
255 if (value > Byte.MaxValue || value < Byte.MinValue)
\r
256 throw new OverflowException (Locale.GetText (
\r
257 "Value is greater than Byte.MaxValue or less than Byte.MinValue"));
\r
259 // Returned Even-Rounded
\r
260 return (byte)(Math.Round (value));
\r
263 public static byte ToByte (double value)
\r
265 if (value > Byte.MaxValue || value < Byte.MinValue)
\r
266 throw new OverflowException (Locale.GetText (
\r
267 "Value is greater than Byte.MaxValue or less than Byte.MinValue"));
\r
269 // This and the float version of ToByte are the only ones
\r
270 // the spec listed as checking for .NaN and Infinity overflow
\r
271 if (Double.IsNaN(value) || Double.IsInfinity(value))
\r
272 throw new OverflowException (Locale.GetText (
\r
273 "Value is equal to Double.NaN, Double.PositiveInfinity, or Double.NegativeInfinity"));
\r
275 // Returned Even-Rounded
\r
276 return (byte)(Math.Round (value));
\r
279 public static byte ToByte (float value)
\r
281 if (value > Byte.MaxValue || value < Byte.MinValue)
\r
282 throw new OverflowException (Locale.GetText (
\r
283 "Value is greater than Byte.MaxValue or less than Byte.Minalue"));
\r
285 // This and the double version of ToByte are the only ones
\r
286 // the spec listed as checking for .NaN and Infinity overflow
\r
287 if (Single.IsNaN(value) || Single.IsInfinity(value))
\r
288 throw new OverflowException (Locale.GetText (
\r
289 "Value is equal to Single.NaN, Single.PositiveInfinity, or Single.NegativeInfinity"));
\r
291 // Returned Even-Rounded, pass it as a double, could have this
\r
292 // method just call Convert.ToByte ( (double)value)
\r
293 return (byte)(Math.Round ( (double)value));
\r
296 public static byte ToByte (int value)
\r
298 if (value > Byte.MaxValue || value < Byte.MinValue)
\r
299 throw new OverflowException (Locale.GetText (
\r
300 "Value is greater than Byte.MaxValue or less than Byte.MinValue"));
\r
302 return (byte)value;
\r
305 public static byte ToByte (long value)
\r
307 if (value > Byte.MaxValue || value < Byte.MinValue)
\r
308 throw new OverflowException (Locale.GetText (
\r
309 "Value is greater than Byte.MaxValue or less than Byte.MinValue"));
\r
311 return (byte)value;
\r
314 public static byte ToByte (sbyte value)
\r
316 if (value < Byte.MinValue)
\r
317 throw new OverflowException (Locale.GetText (
\r
318 "Value is less than Byte.MinValue"));
\r
320 return (byte)value;
\r
323 public static byte ToByte (short value)
\r
325 if (value > Byte.MaxValue || value < Byte.MinValue)
\r
326 throw new OverflowException (Locale.GetText (
\r
327 "Value is greater than Byte.MaxValue or less than Byte.MinValue"));
\r
329 return (byte)value;
\r
332 public static byte ToByte (string value)
\r
334 return Byte.Parse (value);
\r
337 public static byte ToByte (string value, IFormatProvider provider)
\r
339 return Byte.Parse (value, provider);
\r
342 public static byte ToByte (uint value)
\r
344 if (value > Byte.MaxValue)
\r
345 throw new OverflowException (Locale.GetText (
\r
346 "Value is greater than Byte.MaxValue"));
\r
348 return (byte)value;
\r
351 public static byte ToByte (ulong value)
\r
353 if (value > Byte.MaxValue)
\r
354 throw new OverflowException (Locale.GetText (
\r
355 "Value is greater than Byte.MaxValue"));
\r
357 return (byte)value;
\r
360 public static byte ToByte (ushort value)
\r
362 if (value > Byte.MaxValue)
\r
363 throw new OverflowException (Locale.GetText (
\r
364 "Value is greater than Byte.MaxValue"));
\r
366 return (byte)value;
\r
369 // ========== Char Conversions ========== //
\r
371 public static char ToChar (byte value)
\r
373 return (char)value;
\r
376 public static char ToChar (char value)
\r
381 public static char ToChar (int value)
\r
383 if (value > Char.MaxValue || value < Char.MinValue)
\r
384 throw new OverflowException (Locale.GetText (
\r
385 "Value is greater than Char.MaxValue or less than Char.MinValue"));
\r
387 return (char)value;
\r
390 public static char ToChar (long value)
\r
392 if (value > Char.MaxValue || value < Char.MinValue)
\r
393 throw new OverflowException (Locale.GetText (
\r
394 "Value is greater than Char.MaxValue or less than Char.MinValue"));
\r
396 return (char)value;
\r
399 public static char ToChar (sbyte value)
\r
401 if (value < Char.MinValue)
\r
402 throw new OverflowException (Locale.GetText (
\r
403 "Value is less than Char.MinValue"));
\r
405 return (char)value;
\r
408 public static char ToChar (short value)
\r
410 if (value < Char.MinValue)
\r
411 throw new OverflowException (Locale.GetText (
\r
412 "Value is less than Char.MinValue"));
\r
414 return (char)value;
\r
417 public static char ToChar (string value)
\r
419 return Char.Parse (value);
\r
422 public static char ToChar (uint value)
\r
424 if (value > Char.MaxValue)
\r
425 throw new OverflowException (Locale.GetText (
\r
426 "Value is greater than Char.MaxValue"));
\r
428 return (char)value;
\r
431 public static char ToChar (ulong value)
\r
433 if (value > Char.MaxValue)
\r
434 throw new OverflowException (Locale.GetText (
\r
435 "Value is greater than Char.MaxValue"));
\r
437 return (char)value;
\r
440 public static char ToChar (ushort value)
\r
442 if (value > Char.MaxValue)
\r
443 throw new OverflowException (Locale.GetText (
\r
444 "Value is greater than Char.MaxValue"));
\r
446 return (char)value;
\r
449 // ========== DateTime Conversions ========== //
\r
451 public static DateTime ToDateTime (string value)
\r
453 return DateTime.Parse (value);
\r
456 public static DateTime ToDateTime (string value, IFormatProvider provider)
\r
458 return DateTime.Parse (value, provider);
\r
461 // ========== Decimal Conversions ========== //
\r
463 public static decimal ToDecimal (bool value)
\r
465 return value ? 1 : 0;
\r
468 public static decimal ToDecimal (byte value)
\r
470 return (decimal)value;
\r
473 public static decimal ToDecimal (decimal value)
\r
478 public static decimal ToDecimal (double value)
\r
480 if (value > (double)Decimal.MaxValue || value < (double)Decimal.MinValue)
\r
481 throw new OverflowException (Locale.GetText (
\r
482 "Value is greater than Decimal.MaxValue or less than Decimal.MinValue"));
\r
484 return (decimal)value;
\r
487 public static decimal ToDecimal (float value)
\r
489 if (value > (double)Decimal.MaxValue || value < (double)Decimal.MinValue)
\r
490 throw new OverflowException (Locale.GetText (
\r
491 "Value is greater than Decimal.MaxValue or less than Decimal.MinValue"));
\r
493 return (decimal)value;
\r
496 public static decimal ToDecimal (int value)
\r
498 return (decimal)value;
\r
501 public static decimal ToDecimal (long value)
\r
503 return (decimal)value;
\r
506 public static decimal ToDecimal (sbyte value)
\r
508 return (decimal)value;
\r
511 public static decimal ToDecimal (short value)
\r
513 return (decimal)value;
\r
516 public static decimal ToDecimal (string value)
\r
518 return Decimal.Parse (value);
\r
521 public static decimal ToDecimal (string value, IFormatProvider provider)
\r
523 return Decimal.Parse (value, provider);
\r
526 public static decimal ToDecimal (uint value)
\r
528 return (decimal)value;
\r
531 public static decimal ToDecimal (ulong value)
\r
533 return (decimal)value;
\r
536 public static decimal ToDecimal (ushort value)
\r
538 return (decimal)value;
\r
541 // ========== Double Conversions ========== //
\r
543 public static double ToDouble (bool value)
\r
545 return value ? 1 : 0;
\r
548 public static double ToDouble (byte value)
\r
550 return (double)value;
\r
553 public static double ToDouble (decimal value)
\r
555 return (double)value;
\r
558 public static double ToDouble (double value)
\r
563 public static double ToDouble (float value)
\r
565 return (double)value;
\r
568 public static double ToDouble (int value)
\r
570 return (double)value;
\r
573 public static double ToDouble (long value)
\r
575 return (double)value;
\r
578 public static double ToDouble (sbyte value)
\r
580 return (double)value;
\r
583 public static double ToDouble (short value)
\r
585 return (double)value;
\r
588 public static double ToDouble (string value)
\r
590 return Double.Parse (value);
\r
593 public static double ToDouble (string value, IFormatProvider provider)
\r
595 return Double.Parse (value, provider);
\r
598 public static double ToDouble (uint value)
\r
600 return (double)value;
\r
603 public static double ToDouble (ulong value)
\r
605 return (double)value;
\r
608 public static double ToDouble (ushort value)
\r
610 return (double)value;
\r
613 // ========== Int16 Conversions ========== //
\r
615 public static short ToInt16 (bool value)
\r
617 return (short)(value ? 1 : 0);
\r
620 public static short ToInt16 (byte value)
\r
622 return (short)value;
\r
625 public static short ToInt16 (char value)
\r
627 if (value > Int16.MaxValue)
\r
628 throw new OverflowException (Locale.GetText (
\r
629 "Value is greater than Int16.MaxValue"));
\r
631 return (short)value;
\r
634 public static short ToInt16 (decimal value)
\r
636 if (value > Int16.MaxValue || value < Int16.MinValue)
\r
637 throw new OverflowException (Locale.GetText (
\r
638 "Value is greater than Int16.MaxValue or less than Int16.MinValue"));
\r
640 // Returned Even-Rounded
\r
641 return (short)(Math.Round (value));
\r
644 public static short ToInt16 (double value)
\r
646 if (value > Int16.MaxValue || value < Int16.MinValue)
\r
647 throw new OverflowException (Locale.GetText (
\r
648 "Value is greater than Int16.MaxValue or less than Int16.MinValue"));
\r
650 // Returned Even-Rounded
\r
651 return (short)(Math.Round (value));
\r
654 public static short ToInt16 (float value)
\r
656 if (value > Int16.MaxValue || value < Int16.MinValue)
\r
657 throw new OverflowException (Locale.GetText (
\r
658 "Value is greater than Int16.MaxValue or less than Int16.MinValue"));
\r
660 // Returned Even-Rounded, use Math.Round pass as a double.
\r
661 return (short)Math.Round ( (double)value);
\r
664 public static short ToInt16 (int value)
\r
666 if (value > Int16.MaxValue || value < Int16.MinValue)
\r
667 throw new OverflowException (Locale.GetText (
\r
668 "Value is greater than Int16.MaxValue or less than Int16.MinValue"));
\r
670 return (short)value;
\r
673 public static short ToInt16 (long value)
\r
675 if (value > Int16.MaxValue || value < Int16.MinValue)
\r
676 throw new OverflowException (Locale.GetText (
\r
677 "Value is greater than Int16.MaxValue or less than Int16.MinValue"));
\r
679 return (short)value;
\r
682 public static short ToInt16 (sbyte value)
\r
684 return (short)value;
\r
687 public static short ToInt16 (short value)
\r
692 public static short ToInt16 (string value)
\r
694 return Int16.Parse (value);
\r
697 public static short ToInt16 (string value, IFormatProvider provider)
\r
699 return Int16.Parse (value, provider);
\r
702 public static short ToInt16 (uint value)
\r
704 if (value > Int16.MaxValue)
\r
705 throw new OverflowException (Locale.GetText (
\r
706 "Value is greater than Int16.MaxValue"));
\r
708 return (short)value;
\r
711 public static short ToInt16 (ulong value)
\r
713 if (value > (ulong)Int16.MaxValue)
\r
714 throw new OverflowException (Locale.GetText (
\r
715 "Value is greater than Int16.MaxValue"));
\r
717 return (short)value;
\r
720 public static short ToInt16 (ushort value)
\r
722 if (value > Int16.MaxValue)
\r
723 throw new OverflowException (Locale.GetText (
\r
724 "Value is greater than Int16.MaxValue"));
\r
726 return (short)value;
\r
729 // ========== Int32 Conversions ========== //
\r
731 public static int ToInt32 (bool value)
\r
733 return value ? 1 : 0;
\r
736 public static int ToInt32 (byte value)
\r
738 return (int)value;
\r
741 public static int ToInt32 (char value)
\r
743 return (int)value;
\r
746 public static int ToInt32 (decimal value)
\r
748 if (value > Int32.MaxValue || value < Int32.MinValue)
\r
749 throw new OverflowException (Locale.GetText (
\r
750 "Value is greater than Int32.MaxValue or less than Int32.MinValue"));
\r
752 // Returned Even-Rounded
\r
753 return (int)(Math.Round (value));
\r
756 public static int ToInt32 (double value)
\r
758 if (value > Int32.MaxValue || value < Int32.MinValue)
\r
759 throw new OverflowException (Locale.GetText (
\r
760 "Value is greater than Int32.MaxValue or less than Int32.MinValue"));
\r
762 // Returned Even-Rounded
\r
763 return (int)(Math.Round (value));
\r
766 public static int ToInt32 (float value)
\r
768 if (value > Int32.MaxValue || value < Int32.MinValue)
\r
769 throw new OverflowException (Locale.GetText (
\r
770 "Value is greater than Int32.MaxValue or less than Int32.MinValue"));
\r
772 // Returned Even-Rounded, pass as a double, could just call
\r
773 // Convert.ToInt32 ( (double)value);
\r
774 return (int)(Math.Round ( (double)value));
\r
777 public static int ToInt32 (int value)
\r
782 public static int ToInt32 (long value)
\r
784 if (value > Int32.MaxValue || value < Int32.MinValue)
\r
785 throw new OverflowException (Locale.GetText (
\r
786 "Value is greater than Int32.MaxValue or less than Int32.MinValue"));
\r
788 return (int)value;
\r
791 public static int ToInt32 (sbyte value)
\r
793 return (int)value;
\r
796 public static int ToInt32 (short value)
\r
798 return (int)value;
\r
801 public static int ToInt32 (string value)
\r
803 return Int32.Parse (value);
\r
806 public static int ToInt32 (string value, IFormatProvider provider)
\r
808 return Int32.Parse (value, provider);
\r
811 public static int ToInt32 (uint value)
\r
813 if (value > Int32.MaxValue)
\r
814 throw new OverflowException (Locale.GetText (
\r
815 "Value is greater than Int32.MaxValue"));
\r
817 return (int)value;
\r
820 public static int ToInt32 (ulong value)
\r
822 if (value > Int32.MaxValue)
\r
823 throw new OverflowException (Locale.GetText (
\r
824 "Value is greater than Int32.MaxValue"));
\r
826 return (int)value;
\r
829 public static int ToInt32 (ushort value)
\r
831 return (int)value;
\r
834 // ========== Int64 Conversions ========== //
\r
836 public static long ToInt64 (bool value)
\r
838 return value ? 1 : 0;
\r
841 public static long ToInt64 (byte value)
\r
843 return (long)value;
\r
846 public static long ToInt64 (char value)
\r
848 return (long)value;
\r
851 public static long ToInt64 (decimal value)
\r
853 if (value > Int64.MaxValue || value < Int64.MinValue)
\r
854 throw new OverflowException (Locale.GetText (
\r
855 "Value is greater than Int64.MaxValue or less than Int64.MinValue"));
\r
857 // Returned Even-Rounded
\r
858 return (long)(Math.Round (value));
\r
861 public static long ToInt64 (double value)
\r
863 if (value > Int64.MaxValue || value < Int64.MinValue)
\r
864 throw new OverflowException (Locale.GetText (
\r
865 "Value is greater than Int64.MaxValue or less than Int64.MinValue"));
\r
867 // Returned Even-Rounded
\r
868 return (long)(Math.Round (value));
\r
871 public static long ToInt64 (float value)
\r
873 if (value > Int64.MaxValue || value < Int64.MinValue)
\r
874 throw new OverflowException (Locale.GetText (
\r
875 "Value is greater than Int64.MaxValue or less than Int64.MinValue"));
\r
877 // Returned Even-Rounded, pass to Math as a double, could
\r
878 // just call Convert.ToInt64 ( (double)value);
\r
879 return (long)(Math.Round ( (double)value));
\r
882 public static long ToInt64 (int value)
\r
884 return (long)value;
\r
887 public static long ToInt64 (long value)
\r
892 public static long ToInt64 (sbyte value)
\r
894 return (long)value;
\r
897 public static long ToInt64 (short value)
\r
899 return (long)value;
\r
902 public static long ToInt64 (string value)
\r
904 return Int64.Parse (value);
\r
907 public static long ToInt64 (string value, IFormatProvider provider)
\r
909 return Int64.Parse (value, provider);
\r
912 public static long ToInt64 (uint value)
\r
914 return (long)value;
\r
917 public static long ToInt64 (ulong value)
\r
919 if (value > Int64.MaxValue)
\r
920 throw new OverflowException (Locale.GetText (
\r
921 "Value is greater than Int64.MaxValue"));
\r
923 return (long)value;
\r
926 public static long ToInt64 (ushort value)
\r
928 return (long)value;
\r
931 // ========== SByte Conversions ========== //
\r
933 public static sbyte ToSByte (bool value)
\r
935 return (sbyte)(value ? 1 : 0);
\r
938 public static sbyte ToSByte (byte value)
\r
940 if (value > SByte.MaxValue)
\r
941 throw new OverflowException (Locale.GetText (
\r
942 "Value is greater than SByte.MaxValue"));
\r
944 return (sbyte)value;
\r
947 public static sbyte ToSByte (char value)
\r
949 if (value > SByte.MaxValue)
\r
950 throw new OverflowException (Locale.GetText (
\r
951 "Value is greater than SByte.MaxValue"));
\r
953 return (sbyte)value;
\r
956 public static sbyte ToSByte (decimal value)
\r
958 if (value > SByte.MaxValue || value < SByte.MinValue)
\r
959 throw new OverflowException (Locale.GetText (
\r
960 "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
\r
962 // Returned Even-Rounded
\r
963 return (sbyte)(Math.Round (value));
\r
966 public static sbyte ToSByte (double value)
\r
968 if (value > SByte.MaxValue || value < SByte.MinValue)
\r
969 throw new OverflowException (Locale.GetText (
\r
970 "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
\r
972 // Returned Even-Rounded
\r
973 return (sbyte)(Math.Round (value));
\r
976 public static sbyte ToSByte (float value)
\r
978 if (value > SByte.MaxValue || value < SByte.MinValue)
\r
979 throw new OverflowException (Locale.GetText (
\r
980 "Value is greater than SByte.MaxValue or less than SByte.Minalue"));
\r
982 // Returned Even-Rounded, pass as double to Math
\r
983 return (sbyte)(Math.Round ( (double)value));
\r
986 public static sbyte ToSByte (int value)
\r
988 if (value > SByte.MaxValue || value < SByte.MinValue)
\r
989 throw new OverflowException (Locale.GetText (
\r
990 "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
\r
992 return (sbyte)value;
\r
995 public static sbyte ToSByte (long value)
\r
997 if (value > SByte.MaxValue || value < SByte.MinValue)
\r
998 throw new OverflowException (Locale.GetText (
\r
999 "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
\r
1001 return (sbyte)value;
\r
1004 public static sbyte ToSByte (sbyte value)
\r
1009 public static sbyte ToSByte (short value)
\r
1011 if (value > SByte.MaxValue || value < SByte.MinValue)
\r
1012 throw new OverflowException (Locale.GetText (
\r
1013 "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
\r
1015 return (sbyte)value;
\r
1018 public static sbyte ToSByte (string value)
\r
1020 return SByte.Parse (value);
\r
1023 public static sbyte ToSByte (string value, IFormatProvider provider)
\r
1025 return SByte.Parse (value, provider);
\r
1028 public static sbyte ToSByte (uint value)
\r
1030 if (value > SByte.MaxValue)
\r
1031 throw new OverflowException (Locale.GetText (
\r
1032 "Value is greater than SByte.MaxValue"));
\r
1034 return (sbyte)value;
\r
1037 public static sbyte ToSByte (ulong value)
\r
1039 if (value > (ulong)SByte.MaxValue)
\r
1040 throw new OverflowException (Locale.GetText (
\r
1041 "Value is greater than SByte.MaxValue"));
\r
1043 return (sbyte)value;
\r
1046 public static sbyte ToSByte (ushort value)
\r
1048 if (value > SByte.MaxValue)
\r
1049 throw new OverflowException (Locale.GetText (
\r
1050 "Value is greater than SByte.MaxValue"));
\r
1052 return (sbyte)value;
\r
1055 // ========== Single Conversions ========== //
\r
1057 public static float ToSingle (bool value)
\r
1059 return value ? 1 : 0;
\r
1062 public static float ToSingle (byte value)
\r
1064 return (float)value;
\r
1067 public static float ToSingle (decimal value)
\r
1069 return (float)value;
\r
1072 public static float ToSingle (double value)
\r
1074 if (value > Single.MaxValue || value < Single.MinValue)
\r
1075 throw new OverflowException (Locale.GetText (
\r
1076 "Value is greater than Single.MaxValue or less than Single.MinValue"));
\r
1078 return (float)value;
\r
1081 public static float ToSingle (float value)
\r
1086 public static float ToSingle (int value)
\r
1088 return (float)value;
\r
1091 public static float ToSingle (long value)
\r
1093 return (float)value;
\r
1096 public static float ToSingle (sbyte value)
\r
1098 return (float)value;
\r
1101 public static float ToSingle (short value)
\r
1103 return (float)value;
\r
1106 public static float ToSingle (string value)
\r
1108 return Single.Parse (value);
\r
1111 public static float ToSingle (string value, IFormatProvider provider)
\r
1113 return Single.Parse (value, provider);
\r
1116 public static float ToSingle (uint value)
\r
1118 return (float)value;
\r
1121 public static float ToSingle (ulong value)
\r
1123 return (float)value;
\r
1126 public static float ToSingle (ushort value)
\r
1128 return (float)value;
\r
1131 // ========== String Conversions ========== //
\r
1133 public static string ToString (bool value)
\r
1135 return value.ToString ();
\r
1138 public static string ToString (byte value)
\r
1140 return value.ToString ();
\r
1143 public static string ToString (byte value, IFormatProvider provider)
\r
1145 return value.ToString (provider);
\r
1148 public static string ToString (char value)
\r
1150 return value.ToString ();
\r
1153 public static string ToString (DateTime value)
\r
1155 return value.ToString ();
\r
1158 public static string ToString (DateTime value, IFormatProvider provider)
\r
1160 return value.ToString (provider);
\r
1163 public static string ToString (decimal value)
\r
1165 return value.ToString ();
\r
1168 public static string ToString (decimal value, IFormatProvider provider)
\r
1170 return value.ToString (provider);
\r
1173 public static string ToString (double value)
\r
1175 return value.ToString ();
\r
1178 public static string ToString (double value, IFormatProvider provider)
\r
1180 return value.ToString (provider);
\r
1183 public static string ToString (float value)
\r
1185 return value.ToString ();
\r
1188 public static string ToString (float value, IFormatProvider provider)
\r
1190 return value.ToString (provider);
\r
1193 public static string ToString (int value)
\r
1195 return value.ToString ();
\r
1198 public static string ToString (int value, IFormatProvider provider)
\r
1200 return value.ToString (provider);
\r
1203 public static string ToString (long value)
\r
1205 return value.ToString ();
\r
1208 public static string ToString (long value, IFormatProvider provider)
\r
1210 return value.ToString (provider);
\r
1213 public static string ToString (sbyte value)
\r
1215 return value.ToString ();
\r
1218 public static string ToString (sbyte value, IFormatProvider provider)
\r
1220 return value.ToString (provider);
\r
1223 public static string ToString (short value)
\r
1225 return value.ToString ();
\r
1228 public static string ToString (short value, IFormatProvider provider)
\r
1230 return value.ToString (provider);
\r
1233 public static string ToString (string value)
\r
1238 public static string ToString (uint value)
\r
1240 return value.ToString ();
\r
1243 public static string ToString (uint value, IFormatProvider provider)
\r
1245 return value.ToString (provider);
\r
1248 public static string ToString (ulong value)
\r
1250 return value.ToString ();
\r
1253 public static string ToString (ulong value, IFormatProvider provider)
\r
1255 return value.ToString (provider);
\r
1258 public static string ToString (ushort value)
\r
1260 return value.ToString ();
\r
1263 public static string ToString (ushort value, IFormatProvider provider)
\r
1265 return value.ToString (provider);
\r
1268 // ========== UInt16 Conversions ========== //
\r
1270 public static ushort ToUInt16 (bool value)
\r
1272 return (ushort)(value ? 1 : 0);
\r
1275 public static ushort ToUInt16 (byte value)
\r
1277 return (ushort)value;
\r
1280 public static ushort ToUInt16 (char value)
\r
1282 return (ushort)value;
\r
1285 public static ushort ToUInt16 (decimal value)
\r
1287 if (value > UInt16.MaxValue || value < UInt16.MinValue)
\r
1288 throw new OverflowException (Locale.GetText (
\r
1289 "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
\r
1291 // Returned Even-Rounded
\r
1292 return (ushort)(Math.Round (value));
\r
1295 public static ushort ToUInt16 (double value)
\r
1297 if (value > UInt16.MaxValue || value < UInt16.MinValue)
\r
1298 throw new OverflowException (Locale.GetText (
\r
1299 "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
\r
1301 // Returned Even-Rounded
\r
1302 return (ushort)(Math.Round (value));
\r
1305 public static ushort ToUInt16 (float value)
\r
1307 if (value > UInt16.MaxValue || value < UInt16.MinValue)
\r
1308 throw new OverflowException (Locale.GetText (
\r
1309 "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
\r
1311 // Returned Even-Rounded, pass as double to Math
\r
1312 return (ushort)(Math.Round ( (double)value));
\r
1315 public static ushort ToUInt16 (int value)
\r
1317 if (value > UInt16.MaxValue || value < UInt16.MinValue)
\r
1318 throw new OverflowException (Locale.GetText (
\r
1319 "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
\r
1321 return (ushort)value;
\r
1324 public static ushort ToUInt16 (long value)
\r
1326 if (value > UInt16.MaxValue || value < UInt16.MinValue)
\r
1327 throw new OverflowException (Locale.GetText (
\r
1328 "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
\r
1330 return (ushort)value;
\r
1333 public static ushort ToUInt16 (sbyte value)
\r
1335 if (value < UInt16.MinValue)
\r
1336 throw new OverflowException (Locale.GetText (
\r
1337 "Value is less than UInt16.MinValue"));
\r
1339 return (ushort)value;
\r
1342 public static ushort ToUInt16 (short value)
\r
1344 if (value < UInt16.MinValue)
\r
1345 throw new OverflowException (Locale.GetText (
\r
1346 "Value is less than UInt16.MinValue"));
\r
1348 return (ushort)value;
\r
1351 public static ushort ToUInt16 (string value)
\r
1353 return UInt16.Parse (value);
\r
1356 public static ushort ToUInt16 (string value, IFormatProvider provider)
\r
1358 return UInt16.Parse (value, provider);
\r
1361 public static ushort ToUInt16 (uint value)
\r
1363 if (value > UInt16.MaxValue)
\r
1364 throw new OverflowException (Locale.GetText (
\r
1365 "Value is greater than UInt16.MaxValue"));
\r
1367 return (ushort)value;
\r
1370 public static ushort ToUInt16 (ulong value)
\r
1372 if (value > (ulong)UInt16.MaxValue)
\r
1373 throw new OverflowException (Locale.GetText (
\r
1374 "Value is greater than UInt16.MaxValue"));
\r
1376 return (ushort)value;
\r
1379 public static ushort ToUInt16 (ushort value)
\r
1384 // ========== UInt32 Conversions ========== //
\r
1386 public static uint ToUInt32 (bool value)
\r
1388 return (uint)(value ? 1 : 0);
\r
1391 public static uint ToUInt32 (byte value)
\r
1393 return (uint)value;
\r
1396 public static uint ToUInt32 (char value)
\r
1398 return (uint)value;
\r
1401 public static uint ToUInt32 (decimal value)
\r
1403 if (value > UInt32.MaxValue || value < UInt32.MinValue)
\r
1404 throw new OverflowException (Locale.GetText (
\r
1405 "Value is greater than UInt32.MaxValue or less than UInt32.MinValue"));
\r
1407 // Returned Even-Rounded
\r
1408 return (uint)(Math.Round (value));
\r
1411 public static uint ToUInt32 (double value)
\r
1413 if (value > UInt32.MaxValue || value < UInt32.MinValue)
\r
1414 throw new OverflowException (Locale.GetText (
\r
1415 "Value is greater than UInt32.MaxValue or less than UInt32.MinValue"));
\r
1417 // Returned Even-Rounded
\r
1418 return (uint)(Math.Round (value));
\r
1421 public static uint ToUInt32 (float value)
\r
1423 if (value > UInt32.MaxValue || value < UInt32.MinValue)
\r
1424 throw new OverflowException (Locale.GetText (
\r
1425 "Value is greater than UInt32.MaxValue or less than UInt32.MinValue"));
\r
1427 // Returned Even-Rounded, pass as double to Math
\r
1428 return (uint)(Math.Round ( (double)value));
\r
1431 public static uint ToUInt32 (int value)
\r
1433 if (value < UInt32.MinValue)
\r
1434 throw new OverflowException (Locale.GetText (
\r
1435 "Value is less than UInt32.MinValue"));
\r
1437 return (uint)value;
\r
1440 public static uint ToUInt32 (long value)
\r
1442 if (value > UInt32.MaxValue || value < UInt32.MinValue)
\r
1443 throw new OverflowException (Locale.GetText (
\r
1444 "Value is greater than UInt32.MaxValue or less than UInt32.MinValue"));
\r
1446 return (uint)value;
\r
1449 public static uint ToUInt32 (sbyte value)
\r
1451 if (value < UInt32.MinValue)
\r
1452 throw new OverflowException (Locale.GetText (
\r
1453 "Value is less than UInt32.MinValue"));
\r
1455 return (uint)value;
\r
1458 public static uint ToUInt32 (short value)
\r
1460 if (value < UInt32.MinValue)
\r
1461 throw new OverflowException (Locale.GetText (
\r
1462 "Value is less than UInt32.MinValue"));
\r
1464 return (uint)value;
\r
1467 public static uint ToUInt32 (string value)
\r
1469 return UInt32.Parse (value);
\r
1472 public static uint ToUInt32 (string value, IFormatProvider provider)
\r
1474 return UInt32.Parse (value, provider);
\r
1477 public static uint ToUInt32 (uint value)
\r
1482 public static uint ToUInt32 (ulong value)
\r
1484 if (value > UInt32.MaxValue)
\r
1485 throw new OverflowException (Locale.GetText (
\r
1486 "Value is greater than UInt32.MaxValue"));
\r
1488 return (uint)value;
\r
1491 public static uint ToUInt32 (ushort value)
\r
1493 return (uint)value;
\r
1496 // ========== UInt64 Conversions ========== //
\r
1498 public static ulong ToUInt64 (bool value)
\r
1500 return (ulong)(value ? 1 : 0);
\r
1503 public static ulong ToUInt64 (byte value)
\r
1505 return (ulong)value;
\r
1508 public static ulong ToUInt64 (char value)
\r
1510 return (ulong)value;
\r
1513 public static ulong ToUInt64 (decimal value)
\r
1515 if (value > UInt64.MaxValue || value < UInt64.MinValue)
\r
1516 throw new OverflowException (Locale.GetText (
\r
1517 "Value is greater than UInt64.MaxValue or less than UInt64.MinValue"));
\r
1519 // Returned Even-Rounded
\r
1520 return (ulong)(Math.Round (value));
\r
1523 public static ulong ToUInt64 (double value)
\r
1525 if (value > UInt64.MaxValue || value < UInt64.MinValue)
\r
1526 throw new OverflowException (Locale.GetText (
\r
1527 "Value is greater than UInt64.MaxValue or less than UInt64.MinValue"));
\r
1529 // Returned Even-Rounded
\r
1530 return (ulong)(Math.Round (value));
\r
1533 public static ulong ToUInt64 (float value)
\r
1535 if (value > UInt64.MaxValue || value < UInt64.MinValue)
\r
1536 throw new OverflowException (Locale.GetText (
\r
1537 "Value is greater than UInt64.MaxValue or less than UInt64.MinValue"));
\r
1539 // Returned Even-Rounded, pass as a double to Math
\r
1540 return (ulong)(Math.Round ( (double)value));
\r
1543 public static ulong ToUInt64 (int value)
\r
1545 if (value < (int)UInt64.MinValue)
\r
1546 throw new OverflowException (Locale.GetText (
\r
1547 "Value is less than UInt64.MinValue"));
\r
1549 return (ulong)value;
\r
1552 public static ulong ToUInt64 (long value)
\r
1554 if (value < (long)UInt64.MinValue)
\r
1555 throw new OverflowException (Locale.GetText (
\r
1556 "Value is less than UInt64.MinValue"));
\r
1558 return (ulong)value;
\r
1561 public static ulong ToUInt64 (sbyte value)
\r
1563 if (value < (sbyte)UInt64.MinValue)
\r
1564 throw new OverflowException
\r
1565 ("Value is less than UInt64.MinValue");
\r
1567 return (ulong)value;
\r
1570 public static ulong ToUInt64 (short value)
\r
1572 if (value < (short)UInt64.MinValue)
\r
1573 throw new OverflowException (Locale.GetText (
\r
1574 "Value is less than UInt64.MinValue"));
\r
1576 return (ulong)value;
\r
1579 public static ulong ToUInt64 (string value)
\r
1581 return UInt64.Parse (value);
\r
1584 public static ulong ToUInt64 (string value, IFormatProvider provider)
\r
1586 return UInt64.Parse (value, provider);
\r
1589 public static ulong ToUInt64 (uint value)
\r
1591 return (ulong)value;
\r
1594 public static ulong ToUInt64 (ulong value)
\r
1599 public static ulong ToUInt64 (ushort value)
\r
1601 return (ulong)value;
\r
1604 // ========== Conversion / Helper Fucntions ========== //
\r
1607 public static object ChangeType( object value, Type conversionType) {
\r
1611 public static object ChangeType( object value, TypeCode typeCode) {
\r
1615 public static object ChangeType( object value, Type conversionType, IFormatProvider provider) {
\r
1619 public static object ChangeType( object value, TypeCode typeCode, IFormatProvider provider) {
\r
1623 // Lookup table for the conversion ToType method. Order
\r
1624 // is important! Used by ToType for comparing the target
\r
1625 // type, and uses hardcoded array indexes.
\r
1626 private static Type[] conversionTable = {
\r
1627 // Valid ICovnertible Types
\r
1628 typeof (Boolean), // 0 TypeCode.Boolean
\r
1629 typeof (Byte), // 1 TypeCode.Byte
\r
1630 typeof (Char), // 2 TypeCode.Char
\r
1631 typeof (DateTime), // 3 TypeCode.DateTime
\r
1632 typeof (Decimal), // 4 TypeCode.Decimal
\r
1633 typeof (Double), // 5 TypeCode.Double
\r
1634 typeof (Int16), // 6 TypeCode.Int16
\r
1635 typeof (Int32), // 7 TypeCode.Int32
\r
1636 typeof (Int64), // 8 TypeCode.Int64
\r
1637 typeof (SByte), // 9 TypeCode.Sbyte
\r
1638 typeof (Single), // 10 TypeCode.Single
\r
1639 typeof (String), // 11 TypeCode.String
\r
1640 typeof (UInt16), // 12 TypeCode.UInt16
\r
1641 typeof (UInt32), // 13 TypeCode.UInt32
\r
1642 typeof (UInt64), // 14 TypeCode.UInt64
\r
1644 // Invalid IConvertible Interface Types
\r
1645 typeof (Object) // 15 TypeCode.Object
\r
1648 // Function to convert an object to another type and return
\r
1649 // it as an object. In place for the core data types to use
\r
1650 // when implementing IConvertible. Uses hardcoded indexes in
\r
1651 // the conversionTypes array, so if modify carefully.
\r
1652 internal static object ToType (object value, Type conversionType,
\r
1653 IFormatProvider provider)
\r
1655 if (value == null)
\r
1656 throw new ArgumentException (Locale.GetText (
\r
1657 "Invalid conversion from null value"));
\r
1659 if (value is IConvertible) {
\r
1660 IConvertible convertValue = (IConvertible)value;
\r
1662 if (conversionType == conversionTable[0]) {
\r
1663 // 0 TypeCode.Boolean
\r
1664 return (object)(convertValue.ToBoolean (provider));
\r
1666 } else if (conversionType == conversionTable[1]) {
\r
1667 // 1 TypeCode.Byte
\r
1668 return (object)(convertValue.ToByte (provider));
\r
1670 } else if (conversionType == conversionTable[2]) {
\r
1671 // 2 TypeCode.Char
\r
1672 return (object)(convertValue.ToChar (provider));
\r
1674 } else if (conversionType == conversionTable[3]) {
\r
1675 // 3 TypeCode.DateTime
\r
1676 return (object)(convertValue.ToDateTime (provider));
\r
1678 } else if (conversionType == conversionTable[4]) {
\r
1679 // 4 TypeCode.Decimal
\r
1680 return (object)(convertValue.ToDecimal (provider));
\r
1682 } else if (conversionType == conversionTable[5]) {
\r
1683 // 5 TypeCode.Double
\r
1684 return (object)(convertValue.ToDouble (provider));
\r
1686 } else if (conversionType == conversionTable[6]) {
\r
1687 // 6 TypeCode.Int16
\r
1688 return (object)(convertValue.ToInt16 (provider));
\r
1690 } else if (conversionType == conversionTable[7]) {
\r
1691 // 7 TypeCode.Int32
\r
1692 return (object)(convertValue.ToInt32 (provider));
\r
1694 } else if (conversionType == conversionTable[8]) {
\r
1695 // 8 TypeCode.Int64
\r
1696 return (object)(convertValue.ToInt64 (provider));
\r
1698 } else if (conversionType == conversionTable[9]) {
\r
1699 // 9 TypeCode.Sbyte
\r
1700 return (object)(convertValue.ToSByte (provider));
\r
1702 } else if (conversionType == conversionTable[10]) {
\r
1703 // 10 TypeCode.Single
\r
1704 return (object)(convertValue.ToSingle (provider));
\r
1706 } else if (conversionType == conversionTable[11]) {
\r
1707 // 11 TypeCode.String
\r
1708 return (object)(convertValue.ToString (provider));
\r
1710 } else if (conversionType == conversionTable[12]) {
\r
1711 // 12 TypeCode.UInt16
\r
1712 return (object)(convertValue.ToUInt16 (provider));
\r
1714 } else if (conversionType == conversionTable[13]) {
\r
1715 // 13 TypeCode.UInt32
\r
1716 return (object)(convertValue.ToUInt32 (provider));
\r
1718 } else if (conversionType == conversionTable[14]) {
\r
1719 // 14 TypeCode.UInt64
\r
1720 return (object)(convertValue.ToUInt64 (provider));
\r
1722 } else if (conversionType == conversionTable[15]) {
\r
1723 // 15 TypeCode.Object
\r
1724 return (object)(value);
\r
1727 // Not in the conversion table
\r
1728 throw new InvalidCastException (Locale.GetText (
\r
1729 "Unknown target conversion type"));
\r
1732 // Value is not IConvertible
\r
1733 throw new ArgumentException (Locale.GetText (
\r
1734 "Value is not a convertible object"));
\r