5 // Derek Holden (dholden@draper.com)
\r
6 // Duncan Mak (duncan@ximian.com)
\r
8 // (C) Ximian, Inc. http://www.ximian.com
\r
12 // System.Convert class. This was written word for word off the
\r
13 // Library specification for System.Convert in the ECMA TC39 TG2
\r
14 // and TG3 working documents. The first page of which has a table
\r
15 // for all legal conversion scenerios.
\r
17 // This header and the one above it can be formatted however, just trying
\r
18 // to keep it consistent w/ the existing mcs headers.
\r
20 // This Convert class could be written another way, with each type
\r
21 // implementing IConvertible and defining their own conversion functions,
\r
22 // and this class just calling the type's implementation. Or, they can
\r
23 // be defined here and the implementing type can use these functions when
\r
24 // defining their IConvertible interface. Byte's ToBoolean() calls
\r
25 // Convert.ToBoolean(byte), or Convert.ToBoolean(byte) calls
\r
26 // byte.ToBoolean(). The first case is what is done here.
\r
28 // See http://lists.ximian.com/archives/public/mono-list/2001-July/000525.html
\r
30 // There are also conversion functions that are not defined in
\r
31 // the ECMA draft, such as there is no bool ToBoolean(DateTime value),
\r
32 // and placing that somewhere won't compile w/ this Convert since the
\r
33 // function doesn't exist. However calling that when using Microsoft's
\r
34 // System.Convert doesn't produce any compiler errors, it just throws
\r
35 // an InvalidCastException at runtime.
\r
37 // Whenever a decimal, double, or single is converted to an integer
\r
38 // based type, it is even rounded. This uses Math.Round which only
\r
39 // has Round(decimal) and Round(double), so in the Convert from
\r
40 // single cases the value is passed to Math as a double. This
\r
41 // may not be completely necessary.
\r
43 // The .NET Framework SDK lists DBNull as a member of this class
\r
44 // as 'public static readonly object DBNull;'.
\r
46 // It should also be decided if all the cast return values should be
\r
47 // returned as unchecked or not.
\r
49 // All the XML function comments were auto generated which is why they
\r
50 // sound someone redundant.
\r
52 // TYPE | BOOL BYTE CHAR DT DEC DBL I16 I32 I64 SBYT SNGL STR UI16 UI32 UI64
\r
53 // -----+--------------------------------------------------------------------
\r
54 // BOOL | X X X X X X X X X X X X X
\r
55 // BYTE | X X X X X X X X X X X X X X
\r
56 // CHAR | X X X X X X X X X X
\r
58 // DEC | X X X X X X X X X X X X X
\r
59 // DBL | X X X X X X X X X X X X X
\r
60 // I16 | X X X X X X X X X X X X X X
\r
61 // I32 | X X X X X X X X X X X X X X
\r
62 // I64 | X X X X X X X X X X X X X X
\r
63 // SBYT | X X X X X X X X X X X X X X
\r
64 // SNGL | X X X X X X X X X X X X X
\r
65 // STR | X X X X X X X X X X X X X X X
\r
66 // UI16 | X X X X X X X X X X X X X X
\r
67 // UI32 | X X X X X X X X X X X X X X
\r
68 // UI64 | X X X X X X X X X X X X X X
\r
71 using System.Security.Cryptography;
\r
72 using System.Globalization;
\r
76 [CLSCompliant(false)]
\r
77 public sealed class Convert {
\r
80 public static readonly object DBNull;
\r
82 // ========== BASE 64 Conversions ========== //
\r
83 // the BASE64 convert methods are using the Base64 converting methods
\r
84 // from System.Security.Cryptography.ToBase64Transform and
\r
85 // System.Security.Cryptography.FromBase64Transform
\r
87 // should be changed to a stand-alone class Base64Encoder & Base64Decoder
\r
89 public static byte[] FromBase64CharArray(char[] inArray, int offset, int length)
\r
91 if (inArray == null)
\r
92 throw new ArgumentNullException();
\r
94 int len = inArray.Length;
\r
95 if (len < 4 || len % 4 != 0)
\r
96 throw new FormatException();
\r
98 byte[] inArr = new System.Text.UnicodeEncoding().GetBytes(inArray);
\r
99 FromBase64Transform t = new FromBase64Transform();
\r
101 return t.TransformFinalBlock(inArr, 0, inArr.Length);
\r
104 public static byte[] FromBase64String(string s)
\r
107 throw new ArgumentNullException();
\r
109 char[] inArr = s.ToCharArray();
\r
111 return FromBase64CharArray(inArr, 0, inArr.Length);
\r
114 public static TypeCode GetTypeCode (object value)
\r
117 return TypeCode.Empty;
\r
119 return Type.GetTypeCode (value.GetType ());
\r
122 public static bool IsDBNull (object value)
\r
124 TypeCode tc = Type.GetTypeCode (value.GetType ());
\r
126 if (tc == TypeCode.DBNull)
\r
132 public static int ToBase64CharArray(byte[] inArray, int offsetIn, int length,
\r
133 char[] outArray, int offsetOut)
\r
135 if (inArray == null || outArray == null)
\r
136 throw new ArgumentNullException();
\r
138 if (offsetIn < 0 || length < 0 || offsetOut < 0 || (offsetIn + length) > inArray.Length)
\r
139 throw new ArgumentOutOfRangeException();
\r
141 ToBase64Transform t = new ToBase64Transform();
\r
142 byte[] outArr = t.TransformFinalBlock(inArray, offsetIn, length);
\r
144 char[] cOutArr = new System.Text.ASCIIEncoding().GetChars(outArr);
\r
146 if ((offsetOut + cOutArr.Length) > outArray.Length)
\r
147 throw new ArgumentOutOfRangeException();
\r
149 Array.Copy(cOutArr, 0, outArray, offsetOut, cOutArr.Length);
\r
151 return cOutArr.Length;
\r
154 public static string ToBase64String(byte[] inArray)
\r
156 if (inArray == null)
\r
157 throw new ArgumentNullException();
\r
159 return ToBase64String(inArray, 0, inArray.Length);
\r
162 public static string ToBase64String(byte[] inArray, int offset, int length)
\r
164 if (inArray == null)
\r
165 throw new ArgumentNullException();
\r
167 if (offset < 0 || length < 0 || (offset + length) > inArray.Length)
\r
168 throw new ArgumentOutOfRangeException();
\r
170 // FIXME: change to stand alone Base64 Encoder class
\r
171 ToBase64Transform t = new ToBase64Transform();
\r
172 byte[] outArr = t.TransformFinalBlock(inArray, offset, length);
\r
174 return (new System.Text.ASCIIEncoding().GetString(outArr));
\r
177 // ========== Boolean Conversions ========== //
\r
179 public static bool ToBoolean (bool value)
\r
184 public static bool ToBoolean (byte value)
\r
186 return (value != 0);
\r
189 public static bool ToBoolean (char value)
\r
191 throw new InvalidCastException (Locale.GetText ("Can't convert char to bool"));
\r
194 public static bool ToBoolean (DateTime value)
\r
196 throw new InvalidCastException (Locale.GetText ("Can't convert date to bool"));
\r
199 public static bool ToBoolean (decimal value)
\r
201 return (value != 0M);
\r
204 public static bool ToBoolean (double value)
\r
206 return (value != 0);
\r
209 public static bool ToBoolean (float value)
\r
211 return (value != 0f);
\r
214 public static bool ToBoolean (int value)
\r
216 return (value != 0);
\r
219 public static bool ToBoolean (long value)
\r
221 return (value != 0);
\r
224 public static bool ToBoolean (sbyte value)
\r
226 return (value != 0);
\r
229 public static bool ToBoolean (short value)
\r
231 return (value != 0);
\r
234 public static bool ToBoolean (string value)
\r
236 return Boolean.Parse (value);
\r
239 public static bool ToBoolean (string value, IFormatProvider provider)
\r
241 return Boolean.Parse (value); // provider is ignored.
\r
244 public static bool ToBoolean (uint value)
\r
246 return (value != 0);
\r
249 public static bool ToBoolean (ulong value)
\r
251 return (value != 0);
\r
254 public static bool ToBoolean (ushort value)
\r
256 return (value != 0);
\r
259 public static bool ToBoolean (object value)
\r
261 return (bool) ChangeType (value, typeof (System.Boolean));
\r
264 public static bool ToBoolean (object value, IFormatProvider provider)
\r
266 return (bool) ChangeType (value, typeof (System.Boolean), provider);
\r
269 // ========== Byte Conversions ========== //
\r
271 public static byte ToByte (bool value)
\r
273 return (byte)(value ? 1 : 0);
\r
276 public static byte ToByte (byte value)
\r
281 public static byte ToByte (char value)
\r
283 if (value > Byte.MaxValue)
\r
284 throw new OverflowException (Locale.GetText (
\r
285 "Value is greater than Byte.MaxValue"));
\r
287 return (byte)value;
\r
290 public static byte ToByte (DateTime value)
\r
292 throw new InvalidCastException ("This conversion is not supported.");
\r
295 public static byte ToByte (decimal value)
\r
297 if (value > Byte.MaxValue || value < Byte.MinValue)
\r
298 throw new OverflowException (Locale.GetText (
\r
299 "Value is greater than Byte.MaxValue or less than Byte.MinValue"));
\r
301 // Returned Even-Rounded
\r
302 return (byte)(Math.Round (value));
\r
305 public static byte ToByte (double 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 // This and the float version of ToByte are the only ones
\r
312 // the spec listed as checking for .NaN and Infinity overflow
\r
313 if (Double.IsNaN(value) || Double.IsInfinity(value))
\r
314 throw new OverflowException (Locale.GetText (
\r
315 "Value is equal to Double.NaN, Double.PositiveInfinity, or Double.NegativeInfinity"));
\r
317 // Returned Even-Rounded
\r
318 return (byte)(Math.Round (value));
\r
321 public static byte ToByte (float value)
\r
323 if (value > Byte.MaxValue || value < Byte.MinValue)
\r
324 throw new OverflowException (Locale.GetText (
\r
325 "Value is greater than Byte.MaxValue or less than Byte.Minalue"));
\r
327 // This and the double version of ToByte are the only ones
\r
328 // the spec listed as checking for .NaN and Infinity overflow
\r
329 if (Single.IsNaN(value) || Single.IsInfinity(value))
\r
330 throw new OverflowException (Locale.GetText (
\r
331 "Value is equal to Single.NaN, Single.PositiveInfinity, or Single.NegativeInfinity"));
\r
333 // Returned Even-Rounded, pass it as a double, could have this
\r
334 // method just call Convert.ToByte ( (double)value)
\r
335 return (byte)(Math.Round ( (double)value));
\r
338 public static byte ToByte (int value)
\r
340 if (value > Byte.MaxValue || value < Byte.MinValue)
\r
341 throw new OverflowException (Locale.GetText (
\r
342 "Value is greater than Byte.MaxValue or less than Byte.MinValue"));
\r
344 return (byte)value;
\r
347 public static byte ToByte (long value)
\r
349 if (value > Byte.MaxValue || value < Byte.MinValue)
\r
350 throw new OverflowException (Locale.GetText (
\r
351 "Value is greater than Byte.MaxValue or less than Byte.MinValue"));
\r
353 return (byte)value;
\r
356 public static byte ToByte (sbyte value)
\r
358 if (value < Byte.MinValue)
\r
359 throw new OverflowException (Locale.GetText (
\r
360 "Value is less than Byte.MinValue"));
\r
362 return (byte)value;
\r
365 public static byte ToByte (short value)
\r
367 if (value > Byte.MaxValue || value < Byte.MinValue)
\r
368 throw new OverflowException (Locale.GetText (
\r
369 "Value is greater than Byte.MaxValue or less than Byte.MinValue"));
\r
371 return (byte)value;
\r
374 public static byte ToByte (string value)
\r
376 return Byte.Parse (value);
\r
379 public static byte ToByte (string value, IFormatProvider provider)
\r
381 return Byte.Parse (value, provider);
\r
384 public static byte ToByte (string value, int fromBase)
\r
386 return (byte) ConvertFromBase (value, fromBase);
\r
389 public static byte ToByte (uint value)
\r
391 if (value > Byte.MaxValue)
\r
392 throw new OverflowException (Locale.GetText (
\r
393 "Value is greater than Byte.MaxValue"));
\r
395 return (byte)value;
\r
398 public static byte ToByte (ulong value)
\r
400 if (value > Byte.MaxValue)
\r
401 throw new OverflowException (Locale.GetText (
\r
402 "Value is greater than Byte.MaxValue"));
\r
404 return (byte)value;
\r
407 public static byte ToByte (ushort value)
\r
409 if (value > Byte.MaxValue)
\r
410 throw new OverflowException (Locale.GetText (
\r
411 "Value is greater than Byte.MaxValue"));
\r
413 return (byte)value;
\r
416 public static byte ToByte (object value)
\r
418 return (byte) ChangeType (value, typeof (System.Byte));
\r
421 public static byte ToByte (object value, IFormatProvider provider)
\r
423 return (byte) ChangeType (value, typeof (System.Byte), provider);
\r
426 // ========== Char Conversions ========== //
\r
428 public static char ToChar (bool value)
\r
430 throw new InvalidCastException ("This conversion is not supported.");
\r
433 public static char ToChar (byte value)
\r
435 return (char)value;
\r
438 public static char ToChar (char value)
\r
443 public static char ToChar (DateTime value)
\r
445 throw new InvalidCastException ("This conversion is not supported.");
\r
448 public static char ToChar (decimal value)
\r
450 throw new InvalidCastException ("This conversion is not supported.");
\r
453 public static char ToChar (double value)
\r
455 throw new InvalidCastException ("This conversion is not supported.");
\r
458 public static char ToChar (int value)
\r
460 if (value > Char.MaxValue || value < Char.MinValue)
\r
461 throw new OverflowException (Locale.GetText (
\r
462 "Value is greater than Char.MaxValue or less than Char.MinValue"));
\r
464 return (char)value;
\r
467 public static char ToChar (long value)
\r
469 if (value > Char.MaxValue || value < Char.MinValue)
\r
470 throw new OverflowException (Locale.GetText (
\r
471 "Value is greater than Char.MaxValue or less than Char.MinValue"));
\r
473 return (char)value;
\r
476 public static char ToChar (float value)
\r
478 throw new InvalidCastException ("This conversion is not supported.");
\r
481 public static char ToChar (sbyte value)
\r
483 if (value < Char.MinValue)
\r
484 throw new OverflowException (Locale.GetText (
\r
485 "Value is less than Char.MinValue"));
\r
487 return (char)value;
\r
490 public static char ToChar (short value)
\r
492 if (value < Char.MinValue)
\r
493 throw new OverflowException (Locale.GetText (
\r
494 "Value is less than Char.MinValue"));
\r
496 return (char)value;
\r
499 public static char ToChar (string value)
\r
501 return Char.Parse (value);
\r
504 public static char ToChar (string value, IFormatProvider provider)
\r
506 return Char.Parse (value); // provider is ignored.
\r
509 public static char ToChar (uint value)
\r
511 if (value > Char.MaxValue)
\r
512 throw new OverflowException (Locale.GetText (
\r
513 "Value is greater than Char.MaxValue"));
\r
515 return (char)value;
\r
518 public static char ToChar (ulong value)
\r
520 if (value > Char.MaxValue)
\r
521 throw new OverflowException (Locale.GetText (
\r
522 "Value is greater than Char.MaxValue"));
\r
524 return (char)value;
\r
527 public static char ToChar (ushort value)
\r
529 if (value > Char.MaxValue)
\r
530 throw new OverflowException (Locale.GetText (
\r
531 "Value is greater than Char.MaxValue"));
\r
533 return (char)value;
\r
536 public static char ToChar (object value)
\r
538 return (char) ChangeType (value, typeof (System.Char));
\r
541 public static char ToChar (object value, IFormatProvider provider)
\r
544 return Char.MinValue;
\r
546 return (char) ChangeType (value, typeof (System.Char), provider);
\r
549 // ========== DateTime Conversions ========== //
\r
551 public static DateTime ToDateTime (string value)
\r
553 return DateTime.Parse (value);
\r
556 public static DateTime ToDateTime (string value, IFormatProvider provider)
\r
558 return DateTime.Parse (value, provider);
\r
561 public static DateTime ToDateTime (bool value)
\r
563 throw new InvalidCastException ("This conversion is not supported.");
\r
566 public static DateTime ToDateTime (byte value)
\r
568 throw new InvalidCastException ("This conversion is not supported.");
\r
571 public static DateTime ToDateTime (char value)
\r
573 throw new InvalidCastException ("This conversion is not supported.");
\r
576 public static DateTime ToDateTime (DateTime value)
\r
581 public static DateTime ToDateTime (decimal value)
\r
583 throw new InvalidCastException ("This conversion is not supported.");
\r
586 public static DateTime ToDateTime (double value)
\r
588 throw new InvalidCastException ("This conversion is not supported.");
\r
591 public static DateTime ToDateTime (short value)
\r
593 throw new InvalidCastException ("This conversion is not supported.");
\r
596 public static DateTime ToDateTime (int value)
\r
598 throw new InvalidCastException ("This conversion is not supported.");
\r
601 public static DateTime ToDateTime (long value)
\r
603 throw new InvalidCastException ("This conversion is not supported.");
\r
606 public static DateTime ToDateTime (float value)
\r
608 throw new InvalidCastException ("This conversion is not supported.");
\r
611 public static DateTime ToDateTime (object value)
\r
613 return (DateTime) ChangeType (value, typeof (System.DateTime));
\r
616 public static DateTime ToDateTime (object value, IFormatProvider provider)
\r
618 return (DateTime) ChangeType (value, typeof (System.DateTime), provider);
\r
621 [CLSCompliant (false)]
\r
622 public static DateTime ToDateTime (sbyte value)
\r
624 throw new InvalidCastException ("This conversion is not supported.");
\r
626 [CLSCompliant (false)]
\r
627 public static DateTime ToDateTime (ushort value)
\r
629 throw new InvalidCastException ("This conversion is not supported.");
\r
632 [CLSCompliant (false)]
\r
633 public static DateTime ToDateTime (uint value)
\r
635 throw new InvalidCastException ("This conversion is not supported.");
\r
638 [CLSCompliant (false)]
\r
639 public static DateTime ToDateTime (ulong value)
\r
641 throw new InvalidCastException ("This conversion is not supported.");
\r
644 // ========== Decimal Conversions ========== //
\r
646 public static decimal ToDecimal (bool value)
\r
648 return value ? 1 : 0;
\r
651 public static decimal ToDecimal (byte value)
\r
653 return (decimal)value;
\r
656 public static decimal ToDecimal (char value)
\r
658 throw new InvalidCastException ("This conversion is not supported.");
\r
661 public static decimal ToDecimal (DateTime value)
\r
663 throw new InvalidCastException ("This conversion is not supported.");
\r
666 public static decimal ToDecimal (decimal value)
\r
671 public static decimal ToDecimal (double value)
\r
673 if (value > (double)Decimal.MaxValue || value < (double)Decimal.MinValue)
\r
674 throw new OverflowException (Locale.GetText (
\r
675 "Value is greater than Decimal.MaxValue or less than Decimal.MinValue"));
\r
677 return (decimal)value;
\r
680 public static decimal ToDecimal (float value)
\r
682 return (decimal) Math.Round (value);
\r
685 public static decimal ToDecimal (int value)
\r
687 return (decimal)value;
\r
690 public static decimal ToDecimal (long value)
\r
692 return (decimal)value;
\r
695 [CLSCompliant (false)]
\r
696 public static decimal ToDecimal (sbyte value)
\r
698 return (decimal)value;
\r
701 public static decimal ToDecimal (short value)
\r
703 return (decimal)value;
\r
706 public static decimal ToDecimal (string value)
\r
708 return Decimal.Parse (value);
\r
711 public static decimal ToDecimal (string value, IFormatProvider provider)
\r
713 return Decimal.Parse (value, provider);
\r
716 [CLSCompliant (false)]
\r
717 public static decimal ToDecimal (uint value)
\r
719 return (decimal)value;
\r
722 [CLSCompliant (false)]
\r
723 public static decimal ToDecimal (ulong value)
\r
725 return (decimal)value;
\r
728 [CLSCompliant (false)]
\r
729 public static decimal ToDecimal (ushort value)
\r
731 return (decimal)value;
\r
734 public static decimal ToDecimal (object value)
\r
736 return (decimal) ChangeType (value, typeof (System.Decimal));
\r
739 public static decimal ToDecimal (object value, IFormatProvider provider)
\r
741 return (decimal) ChangeType (value, typeof (System.Decimal), provider);
\r
745 // ========== Double Conversions ========== //
\r
747 public static double ToDouble (bool value)
\r
749 return value ? 1 : 0;
\r
752 public static double ToDouble (byte value)
\r
754 throw new InvalidCastException ("This conversion is not supported.");
\r
757 public static double ToDouble (char value)
\r
759 throw new InvalidCastException ("This conversion is not supported.");
\r
762 public static double ToDouble (DateTime value)
\r
764 throw new InvalidCastException ("This conversion is not supported.");
\r
767 public static double ToDouble (decimal value)
\r
769 return (double)value;
\r
772 public static double ToDouble (double value)
\r
777 public static double ToDouble (float value)
\r
779 return (double) Math.Round (value);
\r
782 public static double ToDouble (int value)
\r
784 return (double)value;
\r
787 public static double ToDouble (long value)
\r
789 return (double)value;
\r
792 [CLSCompliant (false)]
\r
793 public static double ToDouble (sbyte value)
\r
795 return (double)value;
\r
798 public static double ToDouble (short value)
\r
800 return (double)value;
\r
803 public static double ToDouble (string value)
\r
805 return Double.Parse (value);
\r
808 public static double ToDouble (string value, IFormatProvider provider)
\r
810 return Double.Parse (value, provider);
\r
813 [CLSCompliant (false)]
\r
814 public static double ToDouble (uint value)
\r
816 return (double)value;
\r
819 [CLSCompliant (false)]
\r
820 public static double ToDouble (ulong value)
\r
822 return (double)value;
\r
825 [CLSCompliant (false)]
\r
826 public static double ToDouble (ushort value)
\r
828 return (double)value;
\r
831 public static double ToDouble (object value)
\r
833 return (double) ChangeType (value, typeof (System.Double));
\r
836 public static double ToDouble (object value, IFormatProvider provider)
\r
838 return (double) ChangeType (value, typeof (System.Double), provider);
\r
841 // ========== Int16 Conversions ========== //
\r
843 public static short ToInt16 (bool value)
\r
845 return (short)(value ? 1 : 0);
\r
848 public static short ToInt16 (byte value)
\r
850 return (short)value;
\r
853 public static short ToInt16 (char value)
\r
855 if (value > Int16.MaxValue)
\r
856 throw new OverflowException (Locale.GetText (
\r
857 "Value is greater than Int16.MaxValue"));
\r
859 return (short)value;
\r
862 public static short ToInt16 (DateTime value)
\r
864 throw new InvalidCastException ("This conversion is not supported.");
\r
867 public static short ToInt16 (decimal value)
\r
869 if (value > Int16.MaxValue || value < Int16.MinValue)
\r
870 throw new OverflowException (Locale.GetText (
\r
871 "Value is greater than Int16.MaxValue or less than Int16.MinValue"));
\r
873 // Returned Even-Rounded
\r
874 return (short)(Math.Round (value));
\r
877 public static short ToInt16 (double value)
\r
879 if (value > Int16.MaxValue || value < Int16.MinValue)
\r
880 throw new OverflowException (Locale.GetText (
\r
881 "Value is greater than Int16.MaxValue or less than Int16.MinValue"));
\r
883 // Returned Even-Rounded
\r
884 return (short)(Math.Round (value));
\r
887 public static short ToInt16 (float value)
\r
889 if (value > Int16.MaxValue || value < Int16.MinValue)
\r
890 throw new OverflowException (Locale.GetText (
\r
891 "Value is greater than Int16.MaxValue or less than Int16.MinValue"));
\r
893 // Returned Even-Rounded, use Math.Round pass as a double.
\r
894 return (short)Math.Round ( (double)value);
\r
897 public static short ToInt16 (int value)
\r
899 if (value > Int16.MaxValue || value < Int16.MinValue)
\r
900 throw new OverflowException (Locale.GetText (
\r
901 "Value is greater than Int16.MaxValue or less than Int16.MinValue"));
\r
903 return (short)value;
\r
906 public static short ToInt16 (long value)
\r
908 if (value > Int16.MaxValue || value < Int16.MinValue)
\r
909 throw new OverflowException (Locale.GetText (
\r
910 "Value is greater than Int16.MaxValue or less than Int16.MinValue"));
\r
912 return (short)value;
\r
915 [CLSCompliant (false)]
\r
916 public static short ToInt16 (sbyte value)
\r
918 return (short)value;
\r
921 public static short ToInt16 (short value)
\r
926 public static short ToInt16 (string value)
\r
928 return Int16.Parse (value);
\r
931 public static short ToInt16 (string value, IFormatProvider provider)
\r
933 return Int16.Parse (value, provider);
\r
937 public static short ToInt16 (string value, int fromBase)
\r
939 return (short) ConvertFromBase (value, fromBase);
\r
942 [CLSCompliant (false)]
\r
943 public static short ToInt16 (uint value)
\r
945 if (value > Int16.MaxValue)
\r
946 throw new OverflowException (Locale.GetText (
\r
947 "Value is greater than Int16.MaxValue"));
\r
949 return (short)value;
\r
952 [CLSCompliant (false)]
\r
953 public static short ToInt16 (ulong value)
\r
955 if (value > (ulong)Int16.MaxValue)
\r
956 throw new OverflowException (Locale.GetText (
\r
957 "Value is greater than Int16.MaxValue"));
\r
958 return (short)value;
\r
961 [CLSCompliant (false)]
\r
962 public static short ToInt16 (ushort value)
\r
964 if (value > Int16.MaxValue)
\r
965 throw new OverflowException (Locale.GetText (
\r
966 "Value is greater than Int16.MaxValue"));
\r
968 return (short)value;
\r
971 public static short ToInt16 (object value)
\r
973 return (short) ChangeType (value, typeof (System.Int16));
\r
976 public static short ToInt16 (object value, IFormatProvider provider)
\r
978 return (short) ChangeType (value, typeof (System.Int16), provider);
\r
981 // ========== Int32 Conversions ========== //
\r
983 public static int ToInt32 (bool value)
\r
985 return value ? 1 : 0;
\r
988 public static int ToInt32 (byte value)
\r
990 return (int)value;
\r
993 public static int ToInt32 (char value)
\r
995 return (int)value;
\r
998 public static int ToInt32 (DateTime value)
\r
1000 throw new InvalidCastException ("This conversion is not supported.");
\r
1003 public static int ToInt32 (decimal value)
\r
1005 if (value > Int32.MaxValue || value < Int32.MinValue)
\r
1006 throw new OverflowException (Locale.GetText (
\r
1007 "Value is greater than Int32.MaxValue or less than Int32.MinValue"));
\r
1009 // Returned Even-Rounded
\r
1010 return (int)(Math.Round (value));
\r
1013 public static int ToInt32 (double value)
\r
1015 if (value > Int32.MaxValue || value < Int32.MinValue)
\r
1016 throw new OverflowException (Locale.GetText (
\r
1017 "Value is greater than Int32.MaxValue or less than Int32.MinValue"));
\r
1019 // Returned Even-Rounded
\r
1020 return (int)(Math.Round (value));
\r
1023 public static int ToInt32 (float value)
\r
1025 if (value > Int32.MaxValue || value < Int32.MinValue)
\r
1026 throw new OverflowException (Locale.GetText (
\r
1027 "Value is greater than Int32.MaxValue or less than Int32.MinValue"));
\r
1029 // Returned Even-Rounded, pass as a double, could just call
\r
1030 // Convert.ToInt32 ( (double)value);
\r
1031 return (int)(Math.Round ( (double)value));
\r
1034 public static int ToInt32 (int value)
\r
1039 public static int ToInt32 (long value)
\r
1041 if (value > Int32.MaxValue || value < Int32.MinValue)
\r
1042 throw new OverflowException (Locale.GetText (
\r
1043 "Value is greater than Int32.MaxValue or less than Int32.MinValue"));
\r
1045 return (int)value;
\r
1048 [CLSCompliant (false)]
\r
1049 public static int ToInt32 (sbyte value)
\r
1051 return (int)value;
\r
1054 public static int ToInt32 (short value)
\r
1056 return (int)value;
\r
1059 public static int ToInt32 (string value)
\r
1061 return Int32.Parse (value);
\r
1064 public static int ToInt32 (string value, IFormatProvider provider)
\r
1066 return Int32.Parse (value, provider);
\r
1070 public static int ToInt32 (string value, int fromBase)
\r
1072 return ConvertFromBase (value, fromBase);
\r
1075 [CLSCompliant (false)]
\r
1076 public static int ToInt32 (uint value)
\r
1078 if (value > Int32.MaxValue)
\r
1079 throw new OverflowException (Locale.GetText (
\r
1080 "Value is greater than Int32.MaxValue"));
\r
1082 return (int)value;
\r
1085 [CLSCompliant (false)]
\r
1086 public static int ToInt32 (ulong value)
\r
1088 if (value > Int32.MaxValue)
\r
1089 throw new OverflowException (Locale.GetText (
\r
1090 "Value is greater than Int32.MaxValue"));
\r
1092 return (int)value;
\r
1095 [CLSCompliant (false)]
\r
1096 public static int ToInt32 (ushort value)
\r
1098 return (int)value;
\r
1101 public static int ToInt32 (object value)
\r
1103 return (int) ChangeType (value, typeof (System.Int32));
\r
1106 public static int ToInt32 (object value, IFormatProvider provider)
\r
1108 return (int) ChangeType (value, typeof (System.Int32), provider);
\r
1111 // ========== Int64 Conversions ========== //
\r
1113 public static long ToInt64 (bool value)
\r
1115 return value ? 1 : 0;
\r
1118 public static long ToInt64 (byte value)
\r
1120 return (long)value;
\r
1123 public static long ToInt64 (char value)
\r
1125 return (long)value;
\r
1128 public static long ToInt64 (DateTime value)
\r
1130 throw new InvalidCastException ("This conversion is not supported.");
\r
1133 public static long ToInt64 (decimal value)
\r
1135 if (value > Int64.MaxValue || value < Int64.MinValue)
\r
1136 throw new OverflowException (Locale.GetText (
\r
1137 "Value is greater than Int64.MaxValue or less than Int64.MinValue"));
\r
1139 // Returned Even-Rounded
\r
1140 return (long)(Math.Round (value));
\r
1143 public static long ToInt64 (double value)
\r
1145 if (value > Int64.MaxValue || value < Int64.MinValue)
\r
1146 throw new OverflowException (Locale.GetText (
\r
1147 "Value is greater than Int64.MaxValue or less than Int64.MinValue"));
\r
1149 // Returned Even-Rounded
\r
1150 return (long)(Math.Round (value));
\r
1153 public static long ToInt64 (float value)
\r
1155 if (value > Int64.MaxValue || value < Int64.MinValue)
\r
1156 throw new OverflowException (Locale.GetText (
\r
1157 "Value is greater than Int64.MaxValue or less than Int64.MinValue"));
\r
1159 // Returned Even-Rounded, pass to Math as a double, could
\r
1160 // just call Convert.ToInt64 ( (double)value);
\r
1161 return (long)(Math.Round ( (double)value));
\r
1164 public static long ToInt64 (int value)
\r
1166 return (long)value;
\r
1169 public static long ToInt64 (long value)
\r
1174 [CLSCompliant (false)]
\r
1175 public static long ToInt64 (sbyte value)
\r
1177 return (long)value;
\r
1180 public static long ToInt64 (short value)
\r
1182 return (long)value;
\r
1185 public static long ToInt64 (string value)
\r
1187 return Int64.Parse (value);
\r
1190 public static long ToInt64 (string value, IFormatProvider provider)
\r
1192 return Int64.Parse (value, provider);
\r
1195 public static long ToInt64 (string value, int fromBase)
\r
1197 if (NotValidBase (fromBase))
\r
1198 throw new ArgumentException ("fromBase is not valid.");
\r
1200 return (long) ConvertFromBase (value, fromBase);
\r
1203 [CLSCompliant (false)]
\r
1204 public static long ToInt64 (uint value)
\r
1206 return (long)value;
\r
1209 [CLSCompliant (false)]
\r
1210 public static long ToInt64 (ulong value)
\r
1212 if (value > Int64.MaxValue)
\r
1213 throw new OverflowException (Locale.GetText (
\r
1214 "Value is greater than Int64.MaxValue"));
\r
1216 return (long)value;
\r
1219 [CLSCompliant (false)]
\r
1220 public static long ToInt64 (ushort value)
\r
1222 return (long)value;
\r
1225 public static long ToInt64 (object value)
\r
1227 return (long) ChangeType (value, typeof (System.Int64));
\r
1230 public static long ToInt64 (object value, IFormatProvider provider)
\r
1232 return (long) ChangeType (value, typeof (System.Int64), provider);
\r
1235 // ========== SByte Conversions ========== //
\r
1237 [CLSCompliant (false)]
\r
1238 public static sbyte ToSByte (bool value)
\r
1240 return (sbyte)(value ? 1 : 0);
\r
1243 [CLSCompliant (false)]
\r
1244 public static sbyte ToSByte (byte value)
\r
1246 if (value > SByte.MaxValue)
\r
1247 throw new OverflowException (Locale.GetText (
\r
1248 "Value is greater than SByte.MaxValue"));
\r
1250 return (sbyte)value;
\r
1253 [CLSCompliant (false)]
\r
1254 public static sbyte ToSByte (char value)
\r
1256 if (value > SByte.MaxValue)
\r
1257 throw new OverflowException (Locale.GetText (
\r
1258 "Value is greater than SByte.MaxValue"));
\r
1260 return (sbyte)value;
\r
1263 [CLSCompliant (false)]
\r
1264 public static sbyte ToSByte (DateTime value)
\r
1266 throw new InvalidCastException ("This conversion is not supported.");
\r
1269 [CLSCompliant (false)]
\r
1270 public static sbyte ToSByte (decimal value)
\r
1272 if (value > SByte.MaxValue || value < SByte.MinValue)
\r
1273 throw new OverflowException (Locale.GetText (
\r
1274 "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
\r
1276 // Returned Even-Rounded
\r
1277 return (sbyte)(Math.Round (value));
\r
1280 [CLSCompliant (false)]
\r
1281 public static sbyte ToSByte (double value)
\r
1283 if (value > SByte.MaxValue || value < SByte.MinValue)
\r
1284 throw new OverflowException (Locale.GetText (
\r
1285 "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
\r
1287 // Returned Even-Rounded
\r
1288 return (sbyte)(Math.Round (value));
\r
1291 [CLSCompliant (false)]
\r
1292 public static sbyte ToSByte (float value)
\r
1294 if (value > SByte.MaxValue || value < SByte.MinValue)
\r
1295 throw new OverflowException (Locale.GetText (
\r
1296 "Value is greater than SByte.MaxValue or less than SByte.Minalue"));
\r
1298 // Returned Even-Rounded, pass as double to Math
\r
1299 return (sbyte)(Math.Round ( (double)value));
\r
1302 [CLSCompliant (false)]
\r
1303 public static sbyte ToSByte (int value)
\r
1305 if (value > SByte.MaxValue || value < SByte.MinValue)
\r
1306 throw new OverflowException (Locale.GetText (
\r
1307 "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
\r
1309 return (sbyte)value;
\r
1312 [CLSCompliant (false)]
\r
1313 public static sbyte ToSByte (long value)
\r
1315 if (value > SByte.MaxValue || value < SByte.MinValue)
\r
1316 throw new OverflowException (Locale.GetText (
\r
1317 "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
\r
1319 return (sbyte)value;
\r
1322 [CLSCompliant (false)]
\r
1323 public static sbyte ToSByte (sbyte value)
\r
1328 [CLSCompliant (false)]
\r
1329 public static sbyte ToSByte (short value)
\r
1331 if (value > SByte.MaxValue || value < SByte.MinValue)
\r
1332 throw new OverflowException (Locale.GetText (
\r
1333 "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
\r
1335 return (sbyte)value;
\r
1338 [CLSCompliant (false)]
\r
1339 public static sbyte ToSByte (string value)
\r
1341 return SByte.Parse (value);
\r
1344 [CLSCompliant (false)]
\r
1345 public static sbyte ToSByte (string value, IFormatProvider provider)
\r
1347 return SByte.Parse (value, provider);
\r
1350 [CLSCompliant (false)]
\r
1351 public static sbyte ToSByte (string value, int fromBase)
\r
1353 return (sbyte) ConvertFromBase (value, fromBase);
\r
1356 [CLSCompliant (false)]
\r
1357 public static sbyte ToSByte (uint value)
\r
1359 if (value > SByte.MaxValue)
\r
1360 throw new OverflowException (Locale.GetText (
\r
1361 "Value is greater than SByte.MaxValue"));
\r
1363 return (sbyte)value;
\r
1366 [CLSCompliant (false)]
\r
1367 public static sbyte ToSByte (ulong value)
\r
1369 if (value > (ulong)SByte.MaxValue)
\r
1370 throw new OverflowException (Locale.GetText (
\r
1371 "Value is greater than SByte.MaxValue"));
\r
1373 return (sbyte)value;
\r
1376 [CLSCompliant (false)]
\r
1377 public static sbyte ToSByte (ushort value)
\r
1379 if (value > SByte.MaxValue)
\r
1380 throw new OverflowException (Locale.GetText (
\r
1381 "Value is greater than SByte.MaxValue"));
\r
1383 return (sbyte)value;
\r
1386 [CLSCompliant (false)]
\r
1387 public static sbyte ToSByte (object value)
\r
1389 return (sbyte) ChangeType (value, typeof (System.SByte));
\r
1392 [CLSCompliant (false)]
\r
1393 public static sbyte ToSByte (object value, IFormatProvider provider)
\r
1395 return (sbyte) ChangeType (value, typeof (System.SByte), provider);
\r
1398 // ========== Single Conversions ========== //
\r
1400 public static float ToSingle (bool value)
\r
1402 return value ? 1 : 0;
\r
1405 public static float ToSingle (byte value)
\r
1407 return (float)value;
\r
1410 public static float ToSingle (Char value)
\r
1412 throw new InvalidCastException ("This conversion is not supported.");
\r
1415 public static float ToSingle (DateTime value)
\r
1417 throw new InvalidCastException ("This conversion is not supported.");
\r
1420 public static float ToSingle (decimal value)
\r
1422 return (float)value;
\r
1425 public static float ToSingle (double value)
\r
1427 if (value > Single.MaxValue || value < Single.MinValue)
\r
1428 throw new OverflowException (Locale.GetText (
\r
1429 "Value is greater than Single.MaxValue or less than Single.MinValue"));
\r
1431 return (float)value;
\r
1434 public static float ToSingle (float value)
\r
1439 public static float ToSingle (int value)
\r
1441 return (float)value;
\r
1444 public static float ToSingle (long value)
\r
1446 return (float)value;
\r
1449 [CLSCompliant (false)]
\r
1450 public static float ToSingle (sbyte value)
\r
1452 return (float)value;
\r
1455 public static float ToSingle (short value)
\r
1457 return (float)value;
\r
1460 public static float ToSingle (string value)
\r
1462 return Single.Parse (value);
\r
1465 public static float ToSingle (string value, IFormatProvider provider)
\r
1467 return Single.Parse (value, provider);
\r
1470 [CLSCompliant (false)]
\r
1471 public static float ToSingle (uint value)
\r
1473 return (float)value;
\r
1476 [CLSCompliant (false)]
\r
1477 public static float ToSingle (ulong value)
\r
1479 return (float)value;
\r
1482 [CLSCompliant (false)]
\r
1483 public static float ToSingle (ushort value)
\r
1485 return (float)value;
\r
1488 [CLSCompliant (false)]
\r
1489 public static float ToSingle (object value)
\r
1491 return (float) ChangeType (value, typeof (System.Single));
\r
1494 [CLSCompliant (false)]
\r
1495 public static float ToSingle (object value, IFormatProvider provider)
\r
1497 return (float) ChangeType (value, typeof (System.Single), provider);
\r
1500 // ========== String Conversions ========== //
\r
1502 public static string ToString (bool value)
\r
1504 return value.ToString ();
\r
1507 public static string ToString (bool value, IFormatProvider provider)
\r
1509 return value.ToString (); // the same as ToString (bool).
\r
1512 public static string ToString (byte value)
\r
1514 return value.ToString ();
\r
1517 public static string ToString (byte value, IFormatProvider provider)
\r
1519 return value.ToString (provider);
\r
1522 public static string ToString (byte value, int toBase)
\r
1524 if (NotValidBase (toBase))
\r
1525 throw new ArgumentException ("toBase is not valid.");
\r
1527 return ConvertToBase ((int) value, int toBase);
\r
1530 public static string ToString (char value)
\r
1532 return value.ToString ();
\r
1535 public static string ToString (char value, IFormatProvider provider)
\r
1537 return value.ToString (); // the same as ToString (char)
\r
1540 public static string ToString (DateTime value)
\r
1542 return value.ToString ();
\r
1545 public static string ToString (DateTime value, IFormatProvider provider)
\r
1547 return value.ToString (provider);
\r
1550 public static string ToString (decimal value)
\r
1552 return value.ToString ();
\r
1555 public static string ToString (decimal value, IFormatProvider provider)
\r
1557 return value.ToString (provider);
\r
1560 public static string ToString (double value)
\r
1562 return value.ToString ();
\r
1565 public static string ToString (double value, IFormatProvider provider)
\r
1567 return value.ToString (provider);
\r
1570 public static string ToString (float value)
\r
1572 return value.ToString ();
\r
1575 public static string ToString (float value, IFormatProvider provider)
\r
1577 return value.ToString (provider);
\r
1580 public static string ToString (int value)
\r
1582 return value.ToString ();
\r
1585 public static string ToString (int value, int fromBase)
\r
1587 int retVal = ConvertFromBase (value.ToString (), fromBase);
\r
1588 return retVal.ToString ();
\r
1591 public static string ToString (int value, IFormatProvider provider)
\r
1593 return value.ToString (provider);
\r
1596 public static string ToString (long value)
\r
1598 return value.ToString ();
\r
1601 public static string ToString (long value, int fromBase)
\r
1603 return (ConvertFromBase (value.ToString (), fromBase)).ToString ();
\r
1606 public static string ToString (long value, IFormatProvider provider)
\r
1608 return value.ToString (provider);
\r
1611 public static string ToString (object value)
\r
1613 return (string) ChangeType (value, typeof (System.String));
\r
1616 public static string ToString (object value, IFormatProvider provider)
\r
1618 return (string) ChangeType (value, typeof (System.String), provider);
\r
1621 public static string ToString (sbyte value)
\r
1623 return value.ToString ();
\r
1626 public static string ToString (sbyte value, IFormatProvider provider)
\r
1628 return value.ToString (provider);
\r
1631 public static string ToString (short value)
\r
1633 return value.ToString ();
\r
1636 public static string ToString (short value, int fromBase)
\r
1638 return (ConvertFromBase (value.ToString (), fromBase)).ToString ();
\r
1641 public static string ToString (short value, IFormatProvider provider)
\r
1643 return value.ToString (provider);
\r
1646 public static string ToString (string value)
\r
1651 public static string ToString (string value, IFormatProvider provider)
\r
1653 return value; // provider is ignored.
\r
1656 [CLSCompliant (false)]
\r
1657 public static string ToString (uint value)
\r
1659 return value.ToString ();
\r
1662 [CLSCompliant (false)]
\r
1663 public static string ToString (uint value, IFormatProvider provider)
\r
1665 return value.ToString (provider);
\r
1668 [CLSCompliant (false)]
\r
1669 public static string ToString (ulong value)
\r
1671 return value.ToString ();
\r
1674 [CLSCompliant (false)]
\r
1675 public static string ToString (ulong value, IFormatProvider provider)
\r
1677 return value.ToString (provider);
\r
1680 [CLSCompliant (false)]
\r
1681 public static string ToString (ushort value)
\r
1683 return value.ToString ();
\r
1686 [CLSCompliant (false)]
\r
1687 public static string ToString (ushort value, IFormatProvider provider)
\r
1689 return value.ToString (provider);
\r
1692 // ========== UInt16 Conversions ========== //
\r
1694 [CLSCompliant (false)]
\r
1695 public static ushort ToUInt16 (bool value)
\r
1697 return (ushort)(value ? 1 : 0);
\r
1700 [CLSCompliant (false)]
\r
1701 public static ushort ToUInt16 (byte value)
\r
1703 return (ushort)value;
\r
1706 [CLSCompliant (false)]
\r
1707 public static ushort ToUInt16 (char value)
\r
1709 return (ushort)value;
\r
1712 [CLSCompliant (false)]
\r
1713 public static ushort ToUInt16 (DateTime value)
\r
1715 throw new InvalidCastException ("This conversion is not supported.");
\r
1718 [CLSCompliant (false)]
\r
1719 public static ushort ToUInt16 (decimal value)
\r
1721 if (value > UInt16.MaxValue || value < UInt16.MinValue)
\r
1722 throw new OverflowException (Locale.GetText (
\r
1723 "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
\r
1725 // Returned Even-Rounded
\r
1726 return (ushort)(Math.Round (value));
\r
1729 [CLSCompliant (false)]
\r
1730 public static ushort ToUInt16 (double value)
\r
1732 if (value > UInt16.MaxValue || value < UInt16.MinValue)
\r
1733 throw new OverflowException (Locale.GetText (
\r
1734 "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
\r
1736 // Returned Even-Rounded
\r
1737 return (ushort)(Math.Round (value));
\r
1740 [CLSCompliant (false)]
\r
1741 public static ushort ToUInt16 (float value)
\r
1743 if (value > UInt16.MaxValue || value < UInt16.MinValue)
\r
1744 throw new OverflowException (Locale.GetText (
\r
1745 "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
\r
1747 // Returned Even-Rounded, pass as double to Math
\r
1748 return (ushort)(Math.Round ( (double)value));
\r
1751 [CLSCompliant (false)]
\r
1752 public static ushort ToUInt16 (int value)
\r
1754 if (value > UInt16.MaxValue || value < UInt16.MinValue)
\r
1755 throw new OverflowException (Locale.GetText (
\r
1756 "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
\r
1758 return (ushort)value;
\r
1761 [CLSCompliant (false)]
\r
1762 public static ushort ToUInt16 (long value)
\r
1764 if (value > UInt16.MaxValue || value < UInt16.MinValue)
\r
1765 throw new OverflowException (Locale.GetText (
\r
1766 "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
\r
1768 return (ushort)value;
\r
1771 [CLSCompliant (false)]
\r
1772 public static ushort ToUInt16 (sbyte value)
\r
1774 if (value < UInt16.MinValue)
\r
1775 throw new OverflowException (Locale.GetText (
\r
1776 "Value is less than UInt16.MinValue"));
\r
1778 return (ushort)value;
\r
1781 [CLSCompliant (false)]
\r
1782 public static ushort ToUInt16 (short value)
\r
1784 if (value < UInt16.MinValue)
\r
1785 throw new OverflowException (Locale.GetText (
\r
1786 "Value is less than UInt16.MinValue"));
\r
1788 return (ushort)value;
\r
1791 [CLSCompliant (false)]
\r
1792 public static ushort ToUInt16 (string value)
\r
1794 return UInt16.Parse (value);
\r
1797 [CLSCompliant (false)]
\r
1798 public static ushort ToUInt16 (string value, IFormatProvider provider)
\r
1800 return UInt16.Parse (value, provider);
\r
1803 [CLSCompliant (false)]
\r
1804 public static ushort ToUInt16 (string value, int fromBase)
\r
1806 return (ushort) ConvertFromBase (value, fromBase);
\r
1809 [CLSCompliant (false)]
\r
1810 public static ushort ToUInt16 (uint value)
\r
1812 if (value > UInt16.MaxValue)
\r
1813 throw new OverflowException (Locale.GetText (
\r
1814 "Value is greater than UInt16.MaxValue"));
\r
1816 return (ushort)value;
\r
1819 [CLSCompliant (false)]
\r
1820 public static ushort ToUInt16 (ulong value)
\r
1822 if (value > (ulong)UInt16.MaxValue)
\r
1823 throw new OverflowException (Locale.GetText (
\r
1824 "Value is greater than UInt16.MaxValue"));
\r
1826 return (ushort)value;
\r
1829 [CLSCompliant (false)]
\r
1830 public static ushort ToUInt16 (ushort value)
\r
1835 [CLSCompliant (false)]
\r
1836 public static ushort ToUInt16 (object value)
\r
1838 return (ushort) ChangeType (value, typeof (System.UInt16));
\r
1841 [CLSCompliant (false)]
\r
1842 public static ushort ToUInt16 (object value, IFormatProvider provider)
\r
1844 return (ushort) ChangeType (value, typeof (System.UInt16), provider);
\r
1847 // ========== UInt32 Conversions ========== //
\r
1849 [CLSCompliant (false)]
\r
1850 public static uint ToUInt32 (bool value)
\r
1852 return (uint)(value ? 1 : 0);
\r
1855 [CLSCompliant (false)]
\r
1856 public static uint ToUInt32 (byte value)
\r
1858 return (uint)value;
\r
1861 [CLSCompliant (false)]
\r
1862 public static uint ToUInt32 (char value)
\r
1864 return (uint)value;
\r
1867 [CLSCompliant (false)]
\r
1868 public static uint ToUInt32 (DateTime value)
\r
1870 throw new InvalidCastException ("This conversion is not supported.");
\r
1873 [CLSCompliant (false)]
\r
1874 public static uint ToUInt32 (decimal value)
\r
1876 if (value > UInt32.MaxValue || value < UInt32.MinValue)
\r
1877 throw new OverflowException (Locale.GetText (
\r
1878 "Value is greater than UInt32.MaxValue or less than UInt32.MinValue"));
\r
1880 // Returned Even-Rounded
\r
1881 return (uint)(Math.Round (value));
\r
1884 [CLSCompliant (false)]
\r
1885 public static uint ToUInt32 (double value)
\r
1887 if (value > UInt32.MaxValue || value < UInt32.MinValue)
\r
1888 throw new OverflowException (Locale.GetText (
\r
1889 "Value is greater than UInt32.MaxValue or less than UInt32.MinValue"));
\r
1891 // Returned Even-Rounded
\r
1892 return (uint)(Math.Round (value));
\r
1895 [CLSCompliant (false)]
\r
1896 public static uint ToUInt32 (float value)
\r
1898 if (value > UInt32.MaxValue || value < UInt32.MinValue)
\r
1899 throw new OverflowException (Locale.GetText (
\r
1900 "Value is greater than UInt32.MaxValue or less than UInt32.MinValue"));
\r
1902 // Returned Even-Rounded, pass as double to Math
\r
1903 return (uint)(Math.Round ( (double)value));
\r
1906 [CLSCompliant (false)]
\r
1907 public static uint ToUInt32 (int value)
\r
1909 if (value < UInt32.MinValue)
\r
1910 throw new OverflowException (Locale.GetText (
\r
1911 "Value is less than UInt32.MinValue"));
\r
1913 return (uint)value;
\r
1916 [CLSCompliant (false)]
\r
1917 public static uint ToUInt32 (long value)
\r
1919 if (value > UInt32.MaxValue || value < UInt32.MinValue)
\r
1920 throw new OverflowException (Locale.GetText (
\r
1921 "Value is greater than UInt32.MaxValue or less than UInt32.MinValue"));
\r
1923 return (uint)value;
\r
1926 [CLSCompliant (false)]
\r
1927 public static uint ToUInt32 (sbyte value)
\r
1929 if (value < UInt32.MinValue)
\r
1930 throw new OverflowException (Locale.GetText (
\r
1931 "Value is less than UInt32.MinValue"));
\r
1933 return (uint)value;
\r
1936 [CLSCompliant (false)]
\r
1937 public static uint ToUInt32 (short value)
\r
1939 if (value < UInt32.MinValue)
\r
1940 throw new OverflowException (Locale.GetText (
\r
1941 "Value is less than UInt32.MinValue"));
\r
1943 return (uint)value;
\r
1946 [CLSCompliant (false)]
\r
1947 public static uint ToUInt32 (string value)
\r
1949 return UInt32.Parse (value);
\r
1952 [CLSCompliant (false)]
\r
1953 public static uint ToUInt32 (string value, IFormatProvider provider)
\r
1955 return UInt32.Parse (value, provider);
\r
1958 [CLSCompliant (false)]
\r
1959 public static uint ToUInt32 (string value, int fromBase)
\r
1961 return (uint) ConvertFromBase (value, fromBase);
\r
1964 [CLSCompliant (false)]
\r
1965 public static uint ToUInt32 (uint value)
\r
1970 [CLSCompliant (false)]
\r
1971 public static uint ToUInt32 (ulong value)
\r
1973 if (value > UInt32.MaxValue)
\r
1974 throw new OverflowException (Locale.GetText (
\r
1975 "Value is greater than UInt32.MaxValue"));
\r
1977 return (uint)value;
\r
1980 [CLSCompliant (false)]
\r
1981 public static uint ToUInt32 (ushort value)
\r
1983 return (uint)value;
\r
1986 [CLSCompliant (false)]
\r
1987 public static uint ToUInt32 (object value)
\r
1989 return (uint) ChangeType (value, typeof (System.UInt32));
\r
1992 [CLSCompliant (false)]
\r
1993 public static uint ToUInt32 (object value, IFormatProvider provider)
\r
1995 return (uint) ChangeType (value, typeof (System.UInt32), provider);
\r
1999 // ========== UInt64 Conversions ========== //
\r
2001 [CLSCompliant (false)]
\r
2002 public static ulong ToUInt64 (bool value)
\r
2004 return (ulong)(value ? 1 : 0);
\r
2007 [CLSCompliant (false)]
\r
2008 public static ulong ToUInt64 (byte value)
\r
2010 return (ulong)value;
\r
2013 [CLSCompliant (false)]
\r
2014 public static ulong ToUInt64 (char value)
\r
2016 return (ulong)value;
\r
2019 [CLSCompliant (false)]
\r
2020 public static ulong ToUInt64 (DateTime value)
\r
2022 throw new InvalidCastException ("The conversion is not supported.");
\r
2025 [CLSCompliant (false)]
\r
2026 public static ulong ToUInt64 (decimal value)
\r
2028 if (value > UInt64.MaxValue || value < UInt64.MinValue)
\r
2029 throw new OverflowException (Locale.GetText (
\r
2030 "Value is greater than UInt64.MaxValue or less than UInt64.MinValue"));
\r
2032 // Returned Even-Rounded
\r
2033 return (ulong)(Math.Round (value));
\r
2036 [CLSCompliant (false)]
\r
2037 public static ulong ToUInt64 (double value)
\r
2039 if (value > UInt64.MaxValue || value < UInt64.MinValue)
\r
2040 throw new OverflowException (Locale.GetText (
\r
2041 "Value is greater than UInt64.MaxValue or less than UInt64.MinValue"));
\r
2043 // Returned Even-Rounded
\r
2044 return (ulong)(Math.Round (value));
\r
2047 [CLSCompliant (false)]
\r
2048 public static ulong ToUInt64 (float value)
\r
2050 if (value > UInt64.MaxValue || value < UInt64.MinValue)
\r
2051 throw new OverflowException (Locale.GetText (
\r
2052 "Value is greater than UInt64.MaxValue or less than UInt64.MinValue"));
\r
2054 // Returned Even-Rounded, pass as a double to Math
\r
2055 return (ulong)(Math.Round ( (double)value));
\r
2058 [CLSCompliant (false)]
\r
2059 public static ulong ToUInt64 (int value)
\r
2061 if (value < (int)UInt64.MinValue)
\r
2062 throw new OverflowException (Locale.GetText (
\r
2063 "Value is less than UInt64.MinValue"));
\r
2065 return (ulong)value;
\r
2068 [CLSCompliant (false)]
\r
2069 public static ulong ToUInt64 (long value)
\r
2071 if (value < (long)UInt64.MinValue)
\r
2072 throw new OverflowException (Locale.GetText (
\r
2073 "Value is less than UInt64.MinValue"));
\r
2075 return (ulong)value;
\r
2078 [CLSCompliant (false)]
\r
2079 public static ulong ToUInt64 (sbyte value)
\r
2081 if (value < (sbyte)UInt64.MinValue)
\r
2082 throw new OverflowException
\r
2083 ("Value is less than UInt64.MinValue");
\r
2085 return (ulong)value;
\r
2088 [CLSCompliant (false)]
\r
2089 public static ulong ToUInt64 (short value)
\r
2091 if (value < (short)UInt64.MinValue)
\r
2092 throw new OverflowException (Locale.GetText (
\r
2093 "Value is less than UInt64.MinValue"));
\r
2095 return (ulong)value;
\r
2098 [CLSCompliant (false)]
\r
2099 public static ulong ToUInt64 (string value)
\r
2101 return UInt64.Parse (value);
\r
2104 [CLSCompliant (false)]
\r
2105 public static ulong ToUInt64 (string value, IFormatProvider provider)
\r
2107 return UInt64.Parse (value, provider);
\r
2110 [CLSCompliant (false)]
\r
2111 public static ulong ToUInt64 (string value, int fromBase)
\r
2113 return (ulong) ConvertFromBase (value, fromBase);
\r
2116 [CLSCompliant (false)]
\r
2117 public static ulong ToUInt64 (uint value)
\r
2119 return (ulong)value;
\r
2122 [CLSCompliant (false)]
\r
2123 public static ulong ToUInt64 (ulong value)
\r
2128 [CLSCompliant (false)]
\r
2129 public static ulong ToUInt64 (ushort value)
\r
2131 return (ulong)value;
\r
2134 [CLSCompliant (false)]
\r
2135 public static ulong ToUInt64 (object value)
\r
2137 return (ulong) ChangeType (value, typeof (System.UInt64));
\r
2140 [CLSCompliant (false)]
\r
2141 public static ulong ToUInt64 (object value, IFormatProvider provider)
\r
2143 return (ulong) ChangeType (value, typeof (System.UInt64), provider);
\r
2147 // ========== Conversion / Helper Functions ========== //
\r
2149 public static object ChangeType (object value, Type conversionType)
\r
2151 CultureInfo ci = CultureInfo.CurrentCulture;
\r
2152 NumberFormatInfo number = ci.NumberFormat;
\r
2153 return ToType (value, conversionType, number);
\r
2156 public static object ChangeType (object value, TypeCode typeCode)
\r
2158 CultureInfo ci = CultureInfo.CurrentCulture;
\r
2159 Type conversionType = conversionTable [(int)typeCode];
\r
2160 NumberFormatInfo number = ci.NumberFormat;
\r
2161 return ToType (value, conversionType, number);
\r
2164 public static object ChangeType (object value, Type conversionType, IFormatProvider provider)
\r
2166 return ToType (value, conversionType, provider);
\r
2169 public static object ChangeType (object value, TypeCode typeCode, IFormatProvider provider)
\r
2171 Type conversionType = conversionTable [(int)typeCode];
\r
2172 return ToType (value, conversionType, provider);
\r
2175 private static bool NotValidBase (int value)
\r
2177 if ((value == 2) || (value == 8) ||
\r
2178 (value == 10) || (value == 16))
\r
2184 private static int ConvertFromBase (string value, int fromBase)
\r
2186 if (NotValidBase (fromBase))
\r
2187 throw new ArgumentException ("fromBase is not valid.");
\r
2191 foreach (char c in value) {
\r
2192 if (Char.IsLetter (c))
\r
2193 result = (fromBase) * result + c - 'a' + 10;
\r
2195 result = (fromBase) * result + c - '0';
\r
2200 private static string ConvertToBase (value, toBase)
\r
2202 StringBuilder sb = new StringBuilder ();
\r
2203 BuildConvertedString (sb, value, toBase);
\r
2204 return sb.ToString ();
\r
2207 internal static void BuildConvertedString (StringBuilder sb, int value, int toBase)
\r
2209 int divided = value / toBase;
\r
2210 int reminder = value % toBase;
\r
2213 SBFromBase (sb, divided, toBase);
\r
2215 if (reminder >= 10)
\r
2216 sb.Append ((char) (reminder + 'a' - 10));
\r
2218 sb.Append ((char) (reminder + '0'));
\r
2220 // Lookup table for the conversion ToType method. Order
\r
2221 // is important! Used by ToType for comparing the target
\r
2222 // type, and uses hardcoded array indexes.
\r
2223 private static Type[] conversionTable = {
\r
2224 // Valid ICovnertible Types
\r
2225 typeof (Boolean), // 0 TypeCode.Boolean
\r
2226 typeof (Byte), // 1 TypeCode.Byte
\r
2227 typeof (Char), // 2 TypeCode.Char
\r
2228 typeof (DateTime), // 3 TypeCode.DateTime
\r
2229 typeof (Decimal), // 4 TypeCode.Decimal
\r
2230 typeof (Double), // 5 TypeCode.Double
\r
2231 typeof (Int16), // 6 TypeCode.Int16
\r
2232 typeof (Int32), // 7 TypeCode.Int32
\r
2233 typeof (Int64), // 8 TypeCode.Int64
\r
2234 typeof (SByte), // 9 TypeCode.Sbyte
\r
2235 typeof (Single), // 10 TypeCode.Single
\r
2236 typeof (String), // 11 TypeCode.String
\r
2237 typeof (UInt16), // 12 TypeCode.UInt16
\r
2238 typeof (UInt32), // 13 TypeCode.UInt32
\r
2239 typeof (UInt64), // 14 TypeCode.UInt64
\r
2241 // Invalid IConvertible Interface Types
\r
2242 typeof (Object) // 15 TypeCode.Object
\r
2245 // Function to convert an object to another type and return
\r
2246 // it as an object. In place for the core data types to use
\r
2247 // when implementing IConvertible. Uses hardcoded indexes in
\r
2248 // the conversionTypes array, so if modify carefully.
\r
2249 internal static object ToType (object value, Type conversionType,
\r
2250 IFormatProvider provider)
\r
2252 if (value == null)
\r
2253 throw new ArgumentException (Locale.GetText (
\r
2254 "Invalid conversion from null value"));
\r
2256 if (value is IConvertible) {
\r
2257 IConvertible convertValue = (IConvertible)value;
\r
2259 if (conversionType == conversionTable[0]) {
\r
2260 // 0 TypeCode.Boolean
\r
2261 return (object)(convertValue.ToBoolean (provider));
\r
2263 } else if (conversionType == conversionTable[1]) {
\r
2264 // 1 TypeCode.Byte
\r
2265 return (object)(convertValue.ToByte (provider));
\r
2267 } else if (conversionType == conversionTable[2]) {
\r
2268 // 2 TypeCode.Char
\r
2269 return (object)(convertValue.ToChar (provider));
\r
2271 } else if (conversionType == conversionTable[3]) {
\r
2272 // 3 TypeCode.DateTime
\r
2273 return (object)(convertValue.ToDateTime (provider));
\r
2275 } else if (conversionType == conversionTable[4]) {
\r
2276 // 4 TypeCode.Decimal
\r
2277 return (object)(convertValue.ToDecimal (provider));
\r
2279 } else if (conversionType == conversionTable[5]) {
\r
2280 // 5 TypeCode.Double
\r
2281 return (object)(convertValue.ToDouble (provider));
\r
2283 } else if (conversionType == conversionTable[6]) {
\r
2284 // 6 TypeCode.Int16
\r
2285 return (object)(convertValue.ToInt16 (provider));
\r
2287 } else if (conversionType == conversionTable[7]) {
\r
2288 // 7 TypeCode.Int32
\r
2289 return (object)(convertValue.ToInt32 (provider));
\r
2291 } else if (conversionType == conversionTable[8]) {
\r
2292 // 8 TypeCode.Int64
\r
2293 return (object)(convertValue.ToInt64 (provider));
\r
2295 } else if (conversionType == conversionTable[9]) {
\r
2296 // 9 TypeCode.Sbyte
\r
2297 return (object)(convertValue.ToSByte (provider));
\r
2299 } else if (conversionType == conversionTable[10]) {
\r
2300 // 10 TypeCode.Single
\r
2301 return (object)(convertValue.ToSingle (provider));
\r
2303 } else if (conversionType == conversionTable[11]) {
\r
2304 // 11 TypeCode.String
\r
2305 return (object)(convertValue.ToString (provider));
\r
2307 } else if (conversionType == conversionTable[12]) {
\r
2308 // 12 TypeCode.UInt16
\r
2309 return (object)(convertValue.ToUInt16 (provider));
\r
2311 } else if (conversionType == conversionTable[13]) {
\r
2312 // 13 TypeCode.UInt32
\r
2313 return (object)(convertValue.ToUInt32 (provider));
\r
2315 } else if (conversionType == conversionTable[14]) {
\r
2316 // 14 TypeCode.UInt64
\r
2317 return (object)(convertValue.ToUInt64 (provider));
\r
2319 } else if (conversionType == conversionTable[15]) {
\r
2320 // 15 TypeCode.Object
\r
2321 return (object)(value);
\r
2324 // Not in the conversion table
\r
2325 throw new InvalidCastException (Locale.GetText (
\r
2326 "Unknown target conversion type"));
\r
2329 // Value is not IConvertible
\r
2330 throw new ArgumentException (Locale.GetText (
\r
2331 "Value is not a convertible object: "+ value.GetType().ToString()+" to "+conversionType.ToString()));
\r