5 // Derek Holden (dholden@draper.com)
6 // Duncan Mak (duncan@ximian.com)
8 // (C) Ximian, Inc. http://www.ximian.com
11 // System.Convert class. This was written word for word off the
12 // Library specification for System.Convert in the ECMA TC39 TG2
13 // and TG3 working documents. The first page of which has a table
14 // for all legal conversion scenerios.
16 // This header and the one above it can be formatted however, just trying
17 // to keep it consistent w/ the existing mcs headers.
19 // This Convert class could be written another way, with each type
20 // implementing IConvertible and defining their own conversion functions,
21 // and this class just calling the type's implementation. Or, they can
22 // be defined here and the implementing type can use these functions when
23 // defining their IConvertible interface. Byte's ToBoolean() calls
24 // Convert.ToBoolean(byte), or Convert.ToBoolean(byte) calls
25 // byte.ToBoolean(). The first case is what is done here.
27 // See http://lists.ximian.com/archives/public/mono-list/2001-July/000525.html
29 // There are also conversion functions that are not defined in
30 // the ECMA draft, such as there is no bool ToBoolean(DateTime value),
31 // and placing that somewhere won't compile w/ this Convert since the
32 // function doesn't exist. However calling that when using Microsoft's
33 // System.Convert doesn't produce any compiler errors, it just throws
34 // an InvalidCastException at runtime.
36 // Whenever a decimal, double, or single is converted to an integer
37 // based type, it is even rounded. This uses Math.Round which only
38 // has Round(decimal) and Round(double), so in the Convert from
39 // single cases the value is passed to Math as a double. This
40 // may not be completely necessary.
42 // The .NET Framework SDK lists DBNull as a member of this class
43 // as 'public static readonly object DBNull;'.
45 // It should also be decided if all the cast return values should be
46 // returned as unchecked or not.
48 // All the XML function comments were auto generated which is why they
49 // sound someone redundant.
51 // TYPE | BOOL BYTE CHAR DT DEC DBL I16 I32 I64 SBYT SNGL STR UI16 UI32 UI64
52 // -----+--------------------------------------------------------------------
53 // BOOL | X X X X X X X X X X X X X
54 // BYTE | X X X X X X X X X X X X X X
55 // CHAR | X X X X X X X X X X
57 // DEC | X X X X X X X X X X X X X
58 // DBL | X X X X X X X X X X X X X
59 // I16 | X X X X X X X X X X X X X X
60 // I32 | X X X X X X X X X X X X X X
61 // I64 | X X X X X X X X X X X X X X
62 // SBYT | X X X X X X X X X X X X X X
63 // SNGL | X X X X X X X X X X X X X
64 // STR | X X X X X X X X X X X X X X X
65 // UI16 | X X X X X X X X X X X X X X
66 // UI32 | X X X X X X X X X X X X X X
67 // UI64 | X X X X X X X X X X X X X X
71 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
73 // Permission is hereby granted, free of charge, to any person obtaining
74 // a copy of this software and associated documentation files (the
75 // "Software"), to deal in the Software without restriction, including
76 // without limitation the rights to use, copy, modify, merge, publish,
77 // distribute, sublicense, and/or sell copies of the Software, and to
78 // permit persons to whom the Software is furnished to do so, subject to
79 // the following conditions:
81 // The above copyright notice and this permission notice shall be
82 // included in all copies or substantial portions of the Software.
84 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
85 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
86 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
87 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
88 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
89 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
90 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
93 using System.Globalization;
95 using System.Security.Cryptography;
97 using System.Runtime.CompilerServices;
98 using System.Runtime.InteropServices;
102 // [CLSCompliant(false)]
103 public static class Convert {
106 public static readonly object DBNull = System.DBNull.Value;
108 [MethodImplAttribute (MethodImplOptions.InternalCall)]
109 extern static byte [] InternalFromBase64String (string str, bool allowWhitespaceOnly);
111 [MethodImplAttribute (MethodImplOptions.InternalCall)]
112 extern static byte [] InternalFromBase64CharArray (char [] arr, int offset, int length);
114 public static byte[] FromBase64CharArray (char[] inArray, int offset, int length)
117 throw new ArgumentNullException ("inArray");
119 throw new ArgumentOutOfRangeException ("offset < 0");
121 throw new ArgumentOutOfRangeException ("length < 0");
122 // avoid integer overflow
123 if (offset > inArray.Length - length)
124 throw new ArgumentOutOfRangeException ("offset + length > array.Length");
126 return InternalFromBase64CharArray (inArray, offset, length);
129 public static byte[] FromBase64String (string s)
132 throw new ArgumentNullException ("s");
138 return InternalFromBase64String (s, true);
141 public static TypeCode GetTypeCode (object value)
144 return TypeCode.Empty;
146 return Type.GetTypeCode (value.GetType ());
149 public static bool IsDBNull (object value)
157 public static int ToBase64CharArray (byte[] inArray, int offsetIn, int length,
158 char[] outArray, int offsetOut)
161 throw new ArgumentNullException ("inArray");
162 if (outArray == null)
163 throw new ArgumentNullException ("outArray");
164 if (offsetIn < 0 || length < 0 || offsetOut < 0)
165 throw new ArgumentOutOfRangeException ("offsetIn, length, offsetOut < 0");
166 // avoid integer overflow
167 if (offsetIn > inArray.Length - length)
168 throw new ArgumentOutOfRangeException ("offsetIn + length > array.Length");
170 // note: normally ToBase64Transform doesn't support multiple block processing
171 byte[] outArr = ToBase64Transform.InternalTransformFinalBlock (inArray, offsetIn, length);
173 char[] cOutArr = new ASCIIEncoding ().GetChars (outArr);
175 // avoid integer overflow
176 if (offsetOut > outArray.Length - cOutArr.Length)
177 throw new ArgumentOutOfRangeException ("offsetOut + cOutArr.Length > outArray.Length");
179 Array.Copy (cOutArr, 0, outArray, offsetOut, cOutArr.Length);
181 return cOutArr.Length;
184 public static string ToBase64String (byte[] inArray)
187 throw new ArgumentNullException ("inArray");
189 return ToBase64String (inArray, 0, inArray.Length);
192 public static string ToBase64String (byte[] inArray, int offset, int length)
195 throw new ArgumentNullException ("inArray");
196 if (offset < 0 || length < 0)
197 throw new ArgumentOutOfRangeException ("offset < 0 || length < 0");
198 // avoid integer overflow
199 if (offset > inArray.Length - length)
200 throw new ArgumentOutOfRangeException ("offset + length > array.Length");
202 // note: normally ToBase64Transform doesn't support multiple block processing
203 byte[] outArr = ToBase64Transform.InternalTransformFinalBlock (inArray, offset, length);
205 return (new ASCIIEncoding ().GetString (outArr));
209 public static string ToBase64String (byte[] inArray, Base64FormattingOptions options)
212 throw new ArgumentNullException ("inArray");
213 return ToBase64String (inArray, 0, inArray.Length, options);
217 public static string ToBase64String (byte[] inArray, int offset, int length, Base64FormattingOptions options)
220 throw new ArgumentNullException ("inArray");
221 if (offset < 0 || length < 0)
222 throw new ArgumentOutOfRangeException ("offset < 0 || length < 0");
223 // avoid integer overflow
224 if (offset > inArray.Length - length)
225 throw new ArgumentOutOfRangeException ("offset + length > array.Length");
230 if (options == Base64FormattingOptions.InsertLineBreaks)
231 return ToBase64StringBuilderWithLine (inArray, offset, length).ToString ();
233 return Encoding.ASCII.GetString (ToBase64Transform.InternalTransformFinalBlock (inArray, offset, length));
237 public static int ToBase64CharArray (byte[] inArray, int offsetIn, int length,
238 char[] outArray, int offsetOut, Base64FormattingOptions options)
241 throw new ArgumentNullException ("inArray");
242 if (outArray == null)
243 throw new ArgumentNullException ("outArray");
244 if (offsetIn < 0 || length < 0 || offsetOut < 0)
245 throw new ArgumentOutOfRangeException ("offsetIn, length, offsetOut < 0");
246 // avoid integer overflow
247 if (offsetIn > inArray.Length - length)
248 throw new ArgumentOutOfRangeException ("offsetIn + length > array.Length");
253 // note: normally ToBase64Transform doesn't support multiple block processing
254 if (options == Base64FormattingOptions.InsertLineBreaks) {
255 StringBuilder sb = ToBase64StringBuilderWithLine (inArray, offsetIn, length);
256 sb.CopyTo (0, outArray, offsetOut, sb.Length);
259 byte[] outArr = ToBase64Transform.InternalTransformFinalBlock (inArray, offsetIn, length);
261 char[] cOutArr = Encoding.ASCII.GetChars (outArr);
263 // avoid integer overflow
264 if (offsetOut > outArray.Length - cOutArr.Length)
265 throw new ArgumentOutOfRangeException ("offsetOut + cOutArr.Length > outArray.Length");
267 Array.Copy (cOutArr, 0, outArray, offsetOut, cOutArr.Length);
268 return cOutArr.Length;
272 private const int MaxBytesPerLine = 57;
274 static StringBuilder ToBase64StringBuilderWithLine (byte [] inArray, int offset, int length)
276 StringBuilder sb = new StringBuilder ();
279 int full = Math.DivRem (length, MaxBytesPerLine, out remainder);
280 for (int i = 0; i < full; i ++) {
281 byte[] data = ToBase64Transform.InternalTransformFinalBlock (inArray, offset, MaxBytesPerLine);
282 sb.AppendLine (Encoding.ASCII.GetString (data));
283 offset += MaxBytesPerLine;
285 // we never complete (i.e. the last line) with a new line
286 if (remainder == 0) {
287 int nll = Environment.NewLine.Length;
288 sb.Remove (sb.Length - nll, nll);
290 byte[] data = ToBase64Transform.InternalTransformFinalBlock (inArray, offset, remainder);
291 sb.Append (Encoding.ASCII.GetString (data));
296 // ========== Boolean Conversions ========== //
298 public static bool ToBoolean (bool value)
303 public static bool ToBoolean (byte value)
308 public static bool ToBoolean (char value)
310 throw new InvalidCastException (Locale.GetText ("Can't convert char to bool"));
313 public static bool ToBoolean (DateTime value)
315 throw new InvalidCastException (Locale.GetText ("Can't convert date to bool"));
318 public static bool ToBoolean (decimal value)
320 return (value != 0M);
323 public static bool ToBoolean (double value)
328 public static bool ToBoolean (float value)
330 return (value != 0f);
333 public static bool ToBoolean (int value)
338 public static bool ToBoolean (long value)
343 [CLSCompliant (false)]
344 public static bool ToBoolean (sbyte value)
349 public static bool ToBoolean (short value)
354 public static bool ToBoolean (string value)
357 return false; // LAMESPEC: Spec says throw ArgumentNullException
358 return Boolean.Parse (value);
361 public static bool ToBoolean (string value, IFormatProvider provider)
364 return false; // LAMESPEC: Spec says throw ArgumentNullException
365 return Boolean.Parse (value); // provider is ignored.
368 [CLSCompliant (false)]
369 public static bool ToBoolean (uint value)
374 [CLSCompliant (false)]
375 public static bool ToBoolean (ulong value)
380 [CLSCompliant (false)]
381 public static bool ToBoolean (ushort value)
388 public static bool ToBoolean (object value)
392 return ToBoolean (value, null);
395 public static bool ToBoolean (object value, IFormatProvider provider)
399 return ((IConvertible) value).ToBoolean (provider);
402 // ========== Byte Conversions ========== //
404 public static byte ToByte (bool value)
406 return (byte)(value ? 1 : 0);
409 public static byte ToByte (byte value)
414 public static byte ToByte (char value)
416 if (value > Byte.MaxValue)
417 throw new OverflowException (Locale.GetText (
418 "Value is greater than Byte.MaxValue"));
423 public static byte ToByte (DateTime value)
425 throw new InvalidCastException ("This conversion is not supported.");
428 public static byte ToByte (decimal value)
430 if (value > Byte.MaxValue || value < Byte.MinValue)
431 throw new OverflowException (Locale.GetText (
432 "Value is greater than Byte.MaxValue or less than Byte.MinValue"));
434 // Returned Even-Rounded
435 return (byte)(Math.Round (value));
438 public static byte ToByte (double value)
440 if (value > Byte.MaxValue || value < Byte.MinValue)
441 throw new OverflowException (Locale.GetText (
442 "Value is greater than Byte.MaxValue or less than Byte.MinValue"));
444 // This and the float version of ToByte are the only ones
445 // the spec listed as checking for .NaN and Infinity overflow
446 if (Double.IsNaN(value) || Double.IsInfinity(value))
447 throw new OverflowException (Locale.GetText (
448 "Value is equal to Double.NaN, Double.PositiveInfinity, or Double.NegativeInfinity"));
450 // Returned Even-Rounded
451 return (byte)(Math.Round (value));
454 public static byte ToByte (float value)
456 if (value > Byte.MaxValue || value < Byte.MinValue)
457 throw new OverflowException (Locale.GetText (
458 "Value is greater than Byte.MaxValue or less than Byte.Minalue"));
460 // This and the double version of ToByte are the only ones
461 // the spec listed as checking for .NaN and Infinity overflow
462 if (Single.IsNaN(value) || Single.IsInfinity(value))
463 throw new OverflowException (Locale.GetText (
464 "Value is equal to Single.NaN, Single.PositiveInfinity, or Single.NegativeInfinity"));
466 // Returned Even-Rounded, pass it as a double, could have this
467 // method just call Convert.ToByte ( (double)value)
468 return (byte)(Math.Round ( (double)value));
471 public static byte ToByte (int value)
473 if (value > Byte.MaxValue || value < Byte.MinValue)
474 throw new OverflowException (Locale.GetText (
475 "Value is greater than Byte.MaxValue or less than Byte.MinValue"));
480 public static byte ToByte (long value)
482 if (value > Byte.MaxValue || value < Byte.MinValue)
483 throw new OverflowException (Locale.GetText (
484 "Value is greater than Byte.MaxValue or less than Byte.MinValue"));
489 [CLSCompliant (false)]
490 public static byte ToByte (sbyte value)
492 if (value < Byte.MinValue)
493 throw new OverflowException (Locale.GetText (
494 "Value is less than Byte.MinValue"));
499 public static byte ToByte (short value)
501 if (value > Byte.MaxValue || value < Byte.MinValue)
502 throw new OverflowException (Locale.GetText (
503 "Value is greater than Byte.MaxValue or less than Byte.MinValue"));
508 public static byte ToByte (string value)
511 return 0; // LAMESPEC: Spec says throw ArgumentNullException
512 return Byte.Parse (value);
515 public static byte ToByte (string value, IFormatProvider provider)
518 return 0; // LAMESPEC: Spec says throw ArgumentNullException
519 return Byte.Parse (value, provider);
522 public static byte ToByte (string value, int fromBase)
524 int retVal = ConvertFromBase (value, fromBase, true);
526 if (retVal < (int) Byte.MinValue || retVal > (int) Byte.MaxValue)
527 throw new OverflowException ();
529 return (byte) retVal;
532 [CLSCompliant (false)]
533 public static byte ToByte (uint value)
535 if (value > Byte.MaxValue)
536 throw new OverflowException (Locale.GetText (
537 "Value is greater than Byte.MaxValue"));
542 [CLSCompliant (false)]
543 public static byte ToByte (ulong value)
545 if (value > Byte.MaxValue)
546 throw new OverflowException (Locale.GetText (
547 "Value is greater than Byte.MaxValue"));
552 [CLSCompliant (false)]
553 public static byte ToByte (ushort value)
555 if (value > Byte.MaxValue)
556 throw new OverflowException (Locale.GetText (
557 "Value is greater than Byte.MaxValue"));
562 public static byte ToByte (object value)
566 return ToByte (value, null);
569 public static byte ToByte (object value, IFormatProvider provider)
573 return ((IConvertible) value).ToByte (provider);
576 // ========== Char Conversions ========== //
578 public static char ToChar (bool value)
580 throw new InvalidCastException ("This conversion is not supported.");
583 public static char ToChar (byte value)
588 public static char ToChar (char value)
593 public static char ToChar (DateTime value)
595 throw new InvalidCastException ("This conversion is not supported.");
598 public static char ToChar (decimal value)
600 throw new InvalidCastException ("This conversion is not supported.");
603 public static char ToChar (double value)
605 throw new InvalidCastException ("This conversion is not supported.");
608 public static char ToChar (int value)
610 if (value > Char.MaxValue || value < Char.MinValue)
611 throw new OverflowException (Locale.GetText (
612 "Value is greater than Char.MaxValue or less than Char.MinValue"));
617 public static char ToChar (long value)
619 if (value > Char.MaxValue || value < Char.MinValue)
620 throw new OverflowException (Locale.GetText (
621 "Value is greater than Char.MaxValue or less than Char.MinValue"));
626 public static char ToChar (float value)
628 throw new InvalidCastException ("This conversion is not supported.");
631 [CLSCompliant (false)]
632 public static char ToChar (sbyte value)
634 if (value < Char.MinValue)
635 throw new OverflowException (Locale.GetText (
636 "Value is less than Char.MinValue"));
641 public static char ToChar (short value)
643 if (value < Char.MinValue)
644 throw new OverflowException (Locale.GetText (
645 "Value is less than Char.MinValue"));
650 public static char ToChar (string value)
652 return Char.Parse (value);
655 public static char ToChar (string value, IFormatProvider provider)
657 return Char.Parse (value); // provider is ignored.
660 [CLSCompliant (false)]
661 public static char ToChar (uint value)
663 if (value > Char.MaxValue)
664 throw new OverflowException (Locale.GetText (
665 "Value is greater than Char.MaxValue"));
670 [CLSCompliant (false)]
671 public static char ToChar (ulong value)
673 if (value > Char.MaxValue)
674 throw new OverflowException (Locale.GetText (
675 "Value is greater than Char.MaxValue"));
680 [CLSCompliant (false)]
681 public static char ToChar (ushort value)
686 public static char ToChar (object value)
690 return ToChar (value, null);
693 public static char ToChar (object value, IFormatProvider provider)
697 return ((IConvertible) value).ToChar (provider);
700 // ========== DateTime Conversions ========== //
702 public static DateTime ToDateTime (string value)
705 return DateTime.MinValue; // LAMESPEC: Spec says throw ArgumentNullException
706 return DateTime.Parse (value);
709 public static DateTime ToDateTime (string value, IFormatProvider provider)
712 return DateTime.MinValue; // LAMESPEC: Spec says throw ArgumentNullException
713 return DateTime.Parse (value, provider);
716 public static DateTime ToDateTime (bool value)
718 throw new InvalidCastException ("This conversion is not supported.");
721 public static DateTime ToDateTime (byte value)
723 throw new InvalidCastException ("This conversion is not supported.");
726 public static DateTime ToDateTime (char value)
728 throw new InvalidCastException ("This conversion is not supported.");
731 public static DateTime ToDateTime (DateTime value)
736 public static DateTime ToDateTime (decimal value)
738 throw new InvalidCastException ("This conversion is not supported.");
741 public static DateTime ToDateTime (double value)
743 throw new InvalidCastException ("This conversion is not supported.");
746 public static DateTime ToDateTime (short value)
748 throw new InvalidCastException ("This conversion is not supported.");
751 public static DateTime ToDateTime (int value)
753 throw new InvalidCastException ("This conversion is not supported.");
756 public static DateTime ToDateTime (long value)
758 throw new InvalidCastException ("This conversion is not supported.");
761 public static DateTime ToDateTime (float value)
763 throw new InvalidCastException ("This conversion is not supported.");
766 public static DateTime ToDateTime (object value)
769 return DateTime.MinValue;
770 return ToDateTime (value, null);
773 public static DateTime ToDateTime (object value, IFormatProvider provider)
776 return DateTime.MinValue;
777 return ((IConvertible) value).ToDateTime (provider);
780 [CLSCompliant (false)]
781 public static DateTime ToDateTime (sbyte value)
783 throw new InvalidCastException ("This conversion is not supported.");
785 [CLSCompliant (false)]
786 public static DateTime ToDateTime (ushort value)
788 throw new InvalidCastException ("This conversion is not supported.");
791 [CLSCompliant (false)]
792 public static DateTime ToDateTime (uint value)
794 throw new InvalidCastException ("This conversion is not supported.");
797 [CLSCompliant (false)]
798 public static DateTime ToDateTime (ulong value)
800 throw new InvalidCastException ("This conversion is not supported.");
803 // ========== Decimal Conversions ========== //
805 public static decimal ToDecimal (bool value)
807 return value ? 1 : 0;
810 public static decimal ToDecimal (byte value)
812 return (decimal)value;
815 public static decimal ToDecimal (char value)
817 throw new InvalidCastException ("This conversion is not supported.");
820 public static decimal ToDecimal (DateTime value)
822 throw new InvalidCastException ("This conversion is not supported.");
825 public static decimal ToDecimal (decimal value)
830 public static decimal ToDecimal (double value)
832 return (decimal) value;
835 public static decimal ToDecimal (float value)
837 return (decimal) value;
840 public static decimal ToDecimal (int value)
842 return (decimal)value;
845 public static decimal ToDecimal (long value)
847 return (decimal)value;
850 [CLSCompliant (false)]
851 public static decimal ToDecimal (sbyte value)
853 return (decimal)value;
856 public static decimal ToDecimal (short value)
858 return (decimal)value;
861 public static decimal ToDecimal (string value)
864 return new Decimal (0); // LAMESPEC: Spec says throw ArgumentNullException
865 return Decimal.Parse (value);
868 public static decimal ToDecimal (string value, IFormatProvider provider)
871 return new Decimal (0); // LAMESPEC: Spec says throw ArgumentNullException
872 return Decimal.Parse (value, provider);
875 [CLSCompliant (false)]
876 public static decimal ToDecimal (uint value)
878 return (decimal)value;
881 [CLSCompliant (false)]
882 public static decimal ToDecimal (ulong value)
884 return (decimal)value;
887 [CLSCompliant (false)]
888 public static decimal ToDecimal (ushort value)
890 return (decimal)value;
893 public static decimal ToDecimal (object value)
896 return new Decimal (0);
897 return ToDecimal (value, null);
900 public static decimal ToDecimal (object value, IFormatProvider provider)
903 return new Decimal (0);
904 return ((IConvertible) value).ToDecimal (provider);
908 // ========== Double Conversions ========== //
910 public static double ToDouble (bool value)
912 return value ? 1 : 0;
915 public static double ToDouble (byte value)
917 return (double) value;
920 public static double ToDouble (char value)
922 throw new InvalidCastException ("This conversion is not supported.");
925 public static double ToDouble (DateTime value)
927 throw new InvalidCastException ("This conversion is not supported.");
930 public static double ToDouble (decimal value)
932 return (double)value;
935 public static double ToDouble (double value)
940 public static double ToDouble (float value)
942 return (double) value;
945 public static double ToDouble (int value)
947 return (double)value;
950 public static double ToDouble (long value)
952 return (double)value;
955 [CLSCompliant (false)]
956 public static double ToDouble (sbyte value)
958 return (double)value;
961 public static double ToDouble (short value)
963 return (double)value;
966 public static double ToDouble (string value)
969 return 0.0; // LAMESPEC: Spec says throw ArgumentNullException
970 return Double.Parse (value);
973 public static double ToDouble (string value, IFormatProvider provider)
976 return 0.0; // LAMESPEC: Spec says throw ArgumentNullException
977 return Double.Parse (value, provider);
980 [CLSCompliant (false)]
981 public static double ToDouble (uint value)
983 return (double)value;
986 [CLSCompliant (false)]
987 public static double ToDouble (ulong value)
989 return (double)value;
992 [CLSCompliant (false)]
993 public static double ToDouble (ushort value)
995 return (double)value;
998 public static double ToDouble (object value)
1002 return ToDouble (value, null);
1005 public static double ToDouble (object value, IFormatProvider provider)
1009 return ((IConvertible) value).ToDouble (provider);
1012 // ========== Int16 Conversions ========== //
1014 public static short ToInt16 (bool value)
1016 return (short)(value ? 1 : 0);
1019 public static short ToInt16 (byte value)
1021 return (short)value;
1024 public static short ToInt16 (char value)
1026 if (value > Int16.MaxValue)
1027 throw new OverflowException (Locale.GetText (
1028 "Value is greater than Int16.MaxValue"));
1030 return (short)value;
1033 public static short ToInt16 (DateTime value)
1035 throw new InvalidCastException ("This conversion is not supported.");
1038 public static short ToInt16 (decimal value)
1040 if (value > Int16.MaxValue || value < Int16.MinValue)
1041 throw new OverflowException (Locale.GetText (
1042 "Value is greater than Int16.MaxValue or less than Int16.MinValue"));
1044 // Returned Even-Rounded
1045 return (short)(Math.Round (value));
1048 public static short ToInt16 (double value)
1050 if (value > Int16.MaxValue || value < Int16.MinValue)
1051 throw new OverflowException (Locale.GetText (
1052 "Value is greater than Int16.MaxValue or less than Int16.MinValue"));
1054 // Returned Even-Rounded
1055 return (short)(Math.Round (value));
1058 public static short ToInt16 (float value)
1060 if (value > Int16.MaxValue || value < Int16.MinValue)
1061 throw new OverflowException (Locale.GetText (
1062 "Value is greater than Int16.MaxValue or less than Int16.MinValue"));
1064 // Returned Even-Rounded, use Math.Round pass as a double.
1065 return (short)Math.Round ( (double)value);
1068 public static short ToInt16 (int value)
1070 if (value > Int16.MaxValue || value < Int16.MinValue)
1071 throw new OverflowException (Locale.GetText (
1072 "Value is greater than Int16.MaxValue or less than Int16.MinValue"));
1074 return (short)value;
1077 public static short ToInt16 (long value)
1079 if (value > Int16.MaxValue || value < Int16.MinValue)
1080 throw new OverflowException (Locale.GetText (
1081 "Value is greater than Int16.MaxValue or less than Int16.MinValue"));
1083 return (short)value;
1086 [CLSCompliant (false)]
1087 public static short ToInt16 (sbyte value)
1089 return (short)value;
1092 public static short ToInt16 (short value)
1097 public static short ToInt16 (string value)
1100 return 0; // LAMESPEC: Spec says throw ArgumentNullException
1101 return Int16.Parse (value);
1104 public static short ToInt16 (string value, IFormatProvider provider)
1107 return 0; // LAMESPEC: Spec says throw ArgumentNullException
1108 return Int16.Parse (value, provider);
1111 public static short ToInt16 (string value, int fromBase)
1113 int result = ConvertFromBase (value, fromBase, false);
1114 if (fromBase != 10) {
1115 if (result > ushort.MaxValue) {
1116 throw new OverflowException ("Value was either too large or too small for an Int16.");
1119 // note: no sign are available to detect negatives
1120 if (result > Int16.MaxValue) {
1121 // return negative 2's complement
1122 return Convert.ToInt16 (-(65536 - result));
1125 return Convert.ToInt16 (result);
1128 [CLSCompliant (false)]
1129 public static short ToInt16 (uint value)
1131 if (value > Int16.MaxValue)
1132 throw new OverflowException (Locale.GetText (
1133 "Value is greater than Int16.MaxValue"));
1135 return (short)value;
1138 [CLSCompliant (false)]
1139 public static short ToInt16 (ulong value)
1141 if (value > (ulong)Int16.MaxValue)
1142 throw new OverflowException (Locale.GetText (
1143 "Value is greater than Int16.MaxValue"));
1144 return (short)value;
1147 [CLSCompliant (false)]
1148 public static short ToInt16 (ushort value)
1150 if (value > Int16.MaxValue)
1151 throw new OverflowException (Locale.GetText (
1152 "Value is greater than Int16.MaxValue"));
1154 return (short)value;
1157 public static short ToInt16 (object value)
1161 return ToInt16 (value, null);
1164 public static short ToInt16 (object value, IFormatProvider provider)
1168 return ((IConvertible) value).ToInt16 (provider);
1171 // ========== Int32 Conversions ========== //
1173 public static int ToInt32 (bool value)
1175 return value ? 1 : 0;
1178 public static int ToInt32 (byte value)
1183 public static int ToInt32 (char value)
1188 public static int ToInt32 (DateTime value)
1190 throw new InvalidCastException ("This conversion is not supported.");
1193 public static int ToInt32 (decimal value)
1195 if (value > Int32.MaxValue || value < Int32.MinValue)
1196 throw new OverflowException (Locale.GetText (
1197 "Value is greater than Int32.MaxValue or less than Int32.MinValue"));
1199 // Returned Even-Rounded
1200 return (int)(Math.Round (value));
1203 public static int ToInt32 (double value)
1205 if (value > Int32.MaxValue || value < Int32.MinValue)
1206 throw new OverflowException (Locale.GetText (
1207 "Value is greater than Int32.MaxValue or less than Int32.MinValue"));
1209 // Returned Even-Rounded
1210 return (int)(Math.Round (value));
1213 public static int ToInt32 (float value)
1215 if (value > Int32.MaxValue || value < Int32.MinValue)
1216 throw new OverflowException (Locale.GetText (
1217 "Value is greater than Int32.MaxValue or less than Int32.MinValue"));
1219 // Returned Even-Rounded, pass as a double, could just call
1220 // Convert.ToInt32 ( (double)value);
1221 return (int)(Math.Round ( (double)value));
1224 public static int ToInt32 (int value)
1229 public static int ToInt32 (long value)
1231 if (value > Int32.MaxValue || value < Int32.MinValue)
1232 throw new OverflowException (Locale.GetText (
1233 "Value is greater than Int32.MaxValue or less than Int32.MinValue"));
1238 [CLSCompliant (false)]
1239 public static int ToInt32 (sbyte value)
1244 public static int ToInt32 (short value)
1249 public static int ToInt32 (string value)
1252 return 0; // LAMESPEC: Spec says throw ArgumentNullException
1253 return Int32.Parse (value);
1256 public static int ToInt32 (string value, IFormatProvider provider)
1259 return 0; // LAMESPEC: Spec says throw ArgumentNullException
1260 return Int32.Parse (value, provider);
1264 public static int ToInt32 (string value, int fromBase)
1266 return ConvertFromBase (value, fromBase, false);
1269 [CLSCompliant (false)]
1270 public static int ToInt32 (uint value)
1272 if (value > Int32.MaxValue)
1273 throw new OverflowException (Locale.GetText (
1274 "Value is greater than Int32.MaxValue"));
1279 [CLSCompliant (false)]
1280 public static int ToInt32 (ulong value)
1282 if (value > Int32.MaxValue)
1283 throw new OverflowException (Locale.GetText (
1284 "Value is greater than Int32.MaxValue"));
1289 [CLSCompliant (false)]
1290 public static int ToInt32 (ushort value)
1295 public static int ToInt32 (object value)
1299 return ToInt32 (value, null);
1302 public static int ToInt32 (object value, IFormatProvider provider)
1306 return ((IConvertible) value).ToInt32 (provider);
1309 // ========== Int64 Conversions ========== //
1311 public static long ToInt64 (bool value)
1313 return value ? 1 : 0;
1316 public static long ToInt64 (byte value)
1318 return (long)(ulong)value;
1321 public static long ToInt64 (char value)
1326 public static long ToInt64 (DateTime value)
1328 throw new InvalidCastException ("This conversion is not supported.");
1331 public static long ToInt64 (decimal value)
1333 if (value > Int64.MaxValue || value < Int64.MinValue)
1334 throw new OverflowException (Locale.GetText (
1335 "Value is greater than Int64.MaxValue or less than Int64.MinValue"));
1337 // Returned Even-Rounded
1338 return (long)(Math.Round (value));
1341 public static long ToInt64 (double value)
1343 if (value > Int64.MaxValue || value < Int64.MinValue)
1344 throw new OverflowException (Locale.GetText (
1345 "Value is greater than Int64.MaxValue or less than Int64.MinValue"));
1347 // Returned Even-Rounded
1348 return (long)(Math.Round (value));
1351 public static long ToInt64 (float value)
1353 if (value > Int64.MaxValue || value < Int64.MinValue)
1354 throw new OverflowException (Locale.GetText (
1355 "Value is greater than Int64.MaxValue or less than Int64.MinValue"));
1357 // Returned Even-Rounded, pass to Math as a double, could
1358 // just call Convert.ToInt64 ( (double)value);
1359 return (long)(Math.Round ( (double)value));
1362 public static long ToInt64 (int value)
1367 public static long ToInt64 (long value)
1372 [CLSCompliant (false)]
1373 public static long ToInt64 (sbyte value)
1378 public static long ToInt64 (short value)
1383 public static long ToInt64 (string value)
1386 return 0; // LAMESPEC: Spec says throw ArgumentNullException
1387 return Int64.Parse (value);
1390 public static long ToInt64 (string value, IFormatProvider provider)
1393 return 0; // LAMESPEC: Spec says throw ArgumentNullException
1394 return Int64.Parse (value, provider);
1397 public static long ToInt64 (string value, int fromBase)
1399 return ConvertFromBase64 (value, fromBase, false);
1402 [CLSCompliant (false)]
1403 public static long ToInt64 (uint value)
1405 return (long)(ulong)value;
1408 [CLSCompliant (false)]
1409 public static long ToInt64 (ulong value)
1411 if (value > Int64.MaxValue)
1412 throw new OverflowException (Locale.GetText (
1413 "Value is greater than Int64.MaxValue"));
1418 [CLSCompliant (false)]
1419 public static long ToInt64 (ushort value)
1421 return (long)(ulong)value;
1424 public static long ToInt64 (object value)
1428 return ToInt64 (value, null);
1431 public static long ToInt64 (object value, IFormatProvider provider)
1435 return ((IConvertible) value).ToInt64 (provider);
1438 // ========== SByte Conversions ========== //
1440 [CLSCompliant (false)]
1441 public static sbyte ToSByte (bool value)
1443 return (sbyte)(value ? 1 : 0);
1446 [CLSCompliant (false)]
1447 public static sbyte ToSByte (byte value)
1449 if (value > SByte.MaxValue)
1450 throw new OverflowException (Locale.GetText (
1451 "Value is greater than SByte.MaxValue"));
1453 return (sbyte)value;
1456 [CLSCompliant (false)]
1457 public static sbyte ToSByte (char value)
1459 if (value > SByte.MaxValue)
1460 throw new OverflowException (Locale.GetText (
1461 "Value is greater than SByte.MaxValue"));
1463 return (sbyte)value;
1466 [CLSCompliant (false)]
1467 public static sbyte ToSByte (DateTime value)
1469 throw new InvalidCastException ("This conversion is not supported.");
1472 [CLSCompliant (false)]
1473 public static sbyte ToSByte (decimal value)
1475 if (value > SByte.MaxValue || value < SByte.MinValue)
1476 throw new OverflowException (Locale.GetText (
1477 "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
1479 // Returned Even-Rounded
1480 return (sbyte)(Math.Round (value));
1483 [CLSCompliant (false)]
1484 public static sbyte ToSByte (double value)
1486 if (value > SByte.MaxValue || value < SByte.MinValue)
1487 throw new OverflowException (Locale.GetText (
1488 "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
1490 // Returned Even-Rounded
1491 return (sbyte)(Math.Round (value));
1494 [CLSCompliant (false)]
1495 public static sbyte ToSByte (float value)
1497 if (value > SByte.MaxValue || value < SByte.MinValue)
1498 throw new OverflowException (Locale.GetText (
1499 "Value is greater than SByte.MaxValue or less than SByte.Minalue"));
1501 // Returned Even-Rounded, pass as double to Math
1502 return (sbyte)(Math.Round ( (double)value));
1505 [CLSCompliant (false)]
1506 public static sbyte ToSByte (int value)
1508 if (value > SByte.MaxValue || value < SByte.MinValue)
1509 throw new OverflowException (Locale.GetText (
1510 "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
1512 return (sbyte)value;
1515 [CLSCompliant (false)]
1516 public static sbyte ToSByte (long value)
1518 if (value > SByte.MaxValue || value < SByte.MinValue)
1519 throw new OverflowException (Locale.GetText (
1520 "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
1522 return (sbyte)value;
1525 [CLSCompliant (false)]
1526 public static sbyte ToSByte (sbyte value)
1531 [CLSCompliant (false)]
1532 public static sbyte ToSByte (short value)
1534 if (value > SByte.MaxValue || value < SByte.MinValue)
1535 throw new OverflowException (Locale.GetText (
1536 "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
1538 return (sbyte)value;
1541 [CLSCompliant (false)]
1542 public static sbyte ToSByte (string value)
1545 return 0; // LAMESPEC: Spec says throw ArgumentNullException
1546 return SByte.Parse (value);
1549 [CLSCompliant (false)]
1550 public static sbyte ToSByte (string value, IFormatProvider provider)
1553 throw new ArgumentNullException ("value");
1554 return SByte.Parse (value, provider);
1557 [CLSCompliant (false)]
1558 public static sbyte ToSByte (string value, int fromBase)
1560 int result = ConvertFromBase (value, fromBase, false);
1561 if (fromBase != 10) {
1562 // note: no sign are available to detect negatives
1563 if (result > SByte.MaxValue) {
1564 // return negative 2's complement
1565 return Convert.ToSByte (-(256 - result));
1568 return Convert.ToSByte (result);
1571 [CLSCompliant (false)]
1572 public static sbyte ToSByte (uint value)
1574 if (value > SByte.MaxValue)
1575 throw new OverflowException (Locale.GetText (
1576 "Value is greater than SByte.MaxValue"));
1578 return (sbyte)value;
1581 [CLSCompliant (false)]
1582 public static sbyte ToSByte (ulong value)
1584 if (value > (ulong)SByte.MaxValue)
1585 throw new OverflowException (Locale.GetText (
1586 "Value is greater than SByte.MaxValue"));
1588 return (sbyte)value;
1591 [CLSCompliant (false)]
1592 public static sbyte ToSByte (ushort value)
1594 if (value > SByte.MaxValue)
1595 throw new OverflowException (Locale.GetText (
1596 "Value is greater than SByte.MaxValue"));
1598 return (sbyte)value;
1601 [CLSCompliant (false)]
1602 public static sbyte ToSByte (object value)
1606 return ToSByte (value, null);
1609 [CLSCompliant (false)]
1610 public static sbyte ToSByte (object value, IFormatProvider provider)
1614 return ((IConvertible) value).ToSByte (provider);
1617 // ========== Single Conversions ========== //
1619 public static float ToSingle (bool value)
1621 return value ? 1 : 0;
1624 public static float ToSingle (byte value)
1626 return (float)value;
1629 public static float ToSingle (Char value)
1631 throw new InvalidCastException ("This conversion is not supported.");
1634 public static float ToSingle (DateTime value)
1636 throw new InvalidCastException ("This conversion is not supported.");
1639 public static float ToSingle (decimal value)
1641 return (float)value;
1644 public static float ToSingle (double value)
1646 return (float)value;
1649 public static float ToSingle (float value)
1654 public static float ToSingle (int value)
1656 return (float)value;
1659 public static float ToSingle (long value)
1661 return (float)value;
1664 [CLSCompliant (false)]
1665 public static float ToSingle (sbyte value)
1667 return (float)value;
1670 public static float ToSingle (short value)
1672 return (float)value;
1675 public static float ToSingle (string value)
1678 return 0.0f; // LAMESPEC: Spec says throw ArgumentNullException
1679 return Single.Parse (value);
1682 public static float ToSingle (string value, IFormatProvider provider)
1685 return 0.0f; // LAMESPEC: Spec says throw ArgumentNullException
1686 return Single.Parse (value, provider);
1689 [CLSCompliant (false)]
1690 public static float ToSingle (uint value)
1692 return (float)value;
1695 [CLSCompliant (false)]
1696 public static float ToSingle (ulong value)
1698 return (float)value;
1701 [CLSCompliant (false)]
1702 public static float ToSingle (ushort value)
1704 return (float)value;
1707 public static float ToSingle (object value)
1711 return ToSingle (value, null);
1714 // [CLSCompliant (false)]
1715 public static float ToSingle (object value, IFormatProvider provider)
1719 return ((IConvertible) value).ToSingle (provider);
1722 // ========== String Conversions ========== //
1724 public static string ToString (bool value)
1726 return value.ToString ();
1729 public static string ToString (bool value, IFormatProvider provider)
1731 return value.ToString (); // the same as ToString (bool).
1734 public static string ToString (byte value)
1736 return value.ToString ();
1739 public static string ToString (byte value, IFormatProvider provider)
1741 return value.ToString (provider);
1744 public static string ToString (byte value, int toBase)
1749 return value.ToString ();
1751 byte[] val = BitConverter.GetBytes (value);
1755 return ConvertToBase2 (val);
1757 return ConvertToBase8 (val);
1759 return ConvertToBase16 (val);
1761 throw new ArgumentException (Locale.GetText ("toBase is not valid."));
1765 public static string ToString (char value)
1767 return value.ToString ();
1770 public static string ToString (char value, IFormatProvider provider)
1772 return value.ToString (); // the same as ToString (char)
1775 public static string ToString (DateTime value)
1777 return value.ToString ();
1780 public static string ToString (DateTime value, IFormatProvider provider)
1782 return value.ToString (provider);
1785 public static string ToString (decimal value)
1787 return value.ToString ();
1790 public static string ToString (decimal value, IFormatProvider provider)
1792 return value.ToString (provider);
1795 public static string ToString (double value)
1797 return value.ToString ();
1800 public static string ToString (double value, IFormatProvider provider)
1802 return value.ToString (provider);
1805 public static string ToString (float value)
1807 return value.ToString ();
1810 public static string ToString (float value, IFormatProvider provider)
1812 return value.ToString (provider);
1815 public static string ToString (int value)
1817 return value.ToString ();
1820 public static string ToString (int value, int toBase)
1825 return value.ToString ();
1827 byte[] val = BitConverter.GetBytes (value);
1831 return ConvertToBase2 (val);
1833 return ConvertToBase8 (val);
1835 return ConvertToBase16 (val);
1837 throw new ArgumentException (Locale.GetText ("toBase is not valid."));
1841 public static string ToString (int value, IFormatProvider provider)
1843 return value.ToString (provider);
1846 public static string ToString (long value)
1848 return value.ToString ();
1851 public static string ToString (long value, int toBase)
1856 return value.ToString ();
1858 byte[] val = BitConverter.GetBytes (value);
1862 return ConvertToBase2 (val);
1864 return ConvertToBase8 (val);
1866 return ConvertToBase16 (val);
1868 throw new ArgumentException (Locale.GetText ("toBase is not valid."));
1872 public static string ToString (long value, IFormatProvider provider)
1874 return value.ToString (provider);
1877 public static string ToString (object value)
1879 return ToString (value, null);
1882 public static string ToString (object value, IFormatProvider provider)
1884 if (value is IConvertible)
1885 return ((IConvertible) value).ToString (provider);
1886 else if (value != null)
1887 return value.ToString ();
1888 return String.Empty;
1891 [CLSCompliant (false)]
1892 public static string ToString (sbyte value)
1894 return value.ToString ();
1897 [CLSCompliant (false)]
1898 public static string ToString (sbyte value, IFormatProvider provider)
1900 return value.ToString (provider);
1903 public static string ToString (short value)
1905 return value.ToString ();
1908 public static string ToString (short value, int toBase)
1913 return value.ToString ();
1915 byte[] val = BitConverter.GetBytes (value);
1919 return ConvertToBase2 (val);
1921 return ConvertToBase8 (val);
1923 return ConvertToBase16 (val);
1925 throw new ArgumentException (Locale.GetText ("toBase is not valid."));
1929 public static string ToString (short value, IFormatProvider provider)
1931 return value.ToString (provider);
1934 public static string ToString (string value)
1939 public static string ToString (string value, IFormatProvider provider)
1941 return value; // provider is ignored.
1944 [CLSCompliant (false)]
1945 public static string ToString (uint value)
1947 return value.ToString ();
1950 [CLSCompliant (false)]
1951 public static string ToString (uint value, IFormatProvider provider)
1953 return value.ToString (provider);
1956 [CLSCompliant (false)]
1957 public static string ToString (ulong value)
1959 return value.ToString ();
1962 [CLSCompliant (false)]
1963 public static string ToString (ulong value, IFormatProvider provider)
1965 return value.ToString (provider);
1968 [CLSCompliant (false)]
1969 public static string ToString (ushort value)
1971 return value.ToString ();
1974 [CLSCompliant (false)]
1975 public static string ToString (ushort value, IFormatProvider provider)
1977 return value.ToString (provider);
1980 // ========== UInt16 Conversions ========== //
1982 [CLSCompliant (false)]
1983 public static ushort ToUInt16 (bool value)
1985 return (ushort)(value ? 1 : 0);
1988 [CLSCompliant (false)]
1989 public static ushort ToUInt16 (byte value)
1991 return (ushort)value;
1994 [CLSCompliant (false)]
1995 public static ushort ToUInt16 (char value)
1997 return (ushort)value;
2000 [CLSCompliant (false)]
2001 public static ushort ToUInt16 (DateTime value)
2003 throw new InvalidCastException ("This conversion is not supported.");
2006 [CLSCompliant (false)]
2007 public static ushort ToUInt16 (decimal value)
2009 if (value > UInt16.MaxValue || value < UInt16.MinValue)
2010 throw new OverflowException (Locale.GetText (
2011 "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
2013 // Returned Even-Rounded
2014 return (ushort)(Math.Round (value));
2017 [CLSCompliant (false)]
2018 public static ushort ToUInt16 (double value)
2020 if (value > UInt16.MaxValue || value < UInt16.MinValue)
2021 throw new OverflowException (Locale.GetText (
2022 "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
2024 // Returned Even-Rounded
2025 return (ushort)(Math.Round (value));
2028 [CLSCompliant (false)]
2029 public static ushort ToUInt16 (float value)
2031 if (value > UInt16.MaxValue || value < UInt16.MinValue)
2032 throw new OverflowException (Locale.GetText (
2033 "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
2035 // Returned Even-Rounded, pass as double to Math
2036 return (ushort)(Math.Round ( (double)value));
2039 [CLSCompliant (false)]
2040 public static ushort ToUInt16 (int value)
2042 if (value > UInt16.MaxValue || value < UInt16.MinValue)
2043 throw new OverflowException (Locale.GetText (
2044 "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
2046 return (ushort)value;
2049 [CLSCompliant (false)]
2050 public static ushort ToUInt16 (long value)
2052 if (value > UInt16.MaxValue || value < UInt16.MinValue)
2053 throw new OverflowException (Locale.GetText (
2054 "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
2056 return (ushort)value;
2059 [CLSCompliant (false)]
2060 public static ushort ToUInt16 (sbyte value)
2062 if (value < UInt16.MinValue)
2063 throw new OverflowException (Locale.GetText (
2064 "Value is less than UInt16.MinValue"));
2066 return (ushort)value;
2069 [CLSCompliant (false)]
2070 public static ushort ToUInt16 (short value)
2072 if (value < UInt16.MinValue)
2073 throw new OverflowException (Locale.GetText (
2074 "Value is less than UInt16.MinValue"));
2076 return (ushort)value;
2079 [CLSCompliant (false)]
2080 public static ushort ToUInt16 (string value)
2083 return 0; // LAMESPEC: Spec says throw ArgumentNullException
2084 return UInt16.Parse (value);
2087 [CLSCompliant (false)]
2088 public static ushort ToUInt16 (string value, IFormatProvider provider)
2091 return 0; // LAMESPEC: Spec says throw ArgumentNullException
2092 return UInt16.Parse (value, provider);
2095 [CLSCompliant (false)]
2096 public static ushort ToUInt16 (string value, int fromBase)
2098 return ToUInt16 (ConvertFromBase (value, fromBase, true));
2101 [CLSCompliant (false)]
2102 public static ushort ToUInt16 (uint value)
2104 if (value > UInt16.MaxValue)
2105 throw new OverflowException (Locale.GetText (
2106 "Value is greater than UInt16.MaxValue"));
2108 return (ushort)value;
2111 [CLSCompliant (false)]
2112 public static ushort ToUInt16 (ulong value)
2114 if (value > (ulong)UInt16.MaxValue)
2115 throw new OverflowException (Locale.GetText (
2116 "Value is greater than UInt16.MaxValue"));
2118 return (ushort)value;
2121 [CLSCompliant (false)]
2122 public static ushort ToUInt16 (ushort value)
2127 [CLSCompliant (false)]
2128 public static ushort ToUInt16 (object value)
2132 return ToUInt16 (value, null);
2135 [CLSCompliant (false)]
2136 public static ushort ToUInt16 (object value, IFormatProvider provider)
2140 return ((IConvertible) value).ToUInt16 (provider);
2143 // ========== UInt32 Conversions ========== //
2145 [CLSCompliant (false)]
2146 public static uint ToUInt32 (bool value)
2148 return (uint)(value ? 1 : 0);
2151 [CLSCompliant (false)]
2152 public static uint ToUInt32 (byte value)
2157 [CLSCompliant (false)]
2158 public static uint ToUInt32 (char value)
2163 [CLSCompliant (false)]
2164 public static uint ToUInt32 (DateTime value)
2166 throw new InvalidCastException ("This conversion is not supported.");
2169 [CLSCompliant (false)]
2170 public static uint ToUInt32 (decimal value)
2172 if (value > UInt32.MaxValue || value < UInt32.MinValue)
2173 throw new OverflowException (Locale.GetText (
2174 "Value is greater than UInt32.MaxValue or less than UInt32.MinValue"));
2176 // Returned Even-Rounded
2177 return (uint)(Math.Round (value));
2180 [CLSCompliant (false)]
2181 public static uint ToUInt32 (double value)
2183 if (value > UInt32.MaxValue || value < UInt32.MinValue)
2184 throw new OverflowException (Locale.GetText (
2185 "Value is greater than UInt32.MaxValue or less than UInt32.MinValue"));
2187 // Returned Even-Rounded
2188 return (uint)(Math.Round (value));
2191 [CLSCompliant (false)]
2192 public static uint ToUInt32 (float value)
2194 if (value > UInt32.MaxValue || value < UInt32.MinValue)
2195 throw new OverflowException (Locale.GetText (
2196 "Value is greater than UInt32.MaxValue or less than UInt32.MinValue"));
2198 // Returned Even-Rounded, pass as double to Math
2199 return (uint)(Math.Round ( (double)value));
2202 [CLSCompliant (false)]
2203 public static uint ToUInt32 (int value)
2205 if (value < UInt32.MinValue)
2206 throw new OverflowException (Locale.GetText (
2207 "Value is less than UInt32.MinValue"));
2212 [CLSCompliant (false)]
2213 public static uint ToUInt32 (long value)
2215 if (value > UInt32.MaxValue || value < UInt32.MinValue)
2216 throw new OverflowException (Locale.GetText (
2217 "Value is greater than UInt32.MaxValue or less than UInt32.MinValue"));
2222 [CLSCompliant (false)]
2223 public static uint ToUInt32 (sbyte value)
2225 if (value < UInt32.MinValue)
2226 throw new OverflowException (Locale.GetText (
2227 "Value is less than UInt32.MinValue"));
2232 [CLSCompliant (false)]
2233 public static uint ToUInt32 (short value)
2235 if (value < UInt32.MinValue)
2236 throw new OverflowException (Locale.GetText (
2237 "Value is less than UInt32.MinValue"));
2242 [CLSCompliant (false)]
2243 public static uint ToUInt32 (string value)
2246 return 0; // LAMESPEC: Spec says throw ArgumentNullException
2247 return UInt32.Parse (value);
2250 [CLSCompliant (false)]
2251 public static uint ToUInt32 (string value, IFormatProvider provider)
2254 return 0; // LAMESPEC: Spec says throw ArgumentNullException
2255 return UInt32.Parse (value, provider);
2258 [CLSCompliant (false)]
2259 public static uint ToUInt32 (string value, int fromBase)
2261 return (uint) ConvertFromBase (value, fromBase, true);
2264 [CLSCompliant (false)]
2265 public static uint ToUInt32 (uint value)
2270 [CLSCompliant (false)]
2271 public static uint ToUInt32 (ulong value)
2273 if (value > UInt32.MaxValue)
2274 throw new OverflowException (Locale.GetText (
2275 "Value is greater than UInt32.MaxValue"));
2280 [CLSCompliant (false)]
2281 public static uint ToUInt32 (ushort value)
2286 [CLSCompliant (false)]
2287 public static uint ToUInt32 (object value)
2291 return ToUInt32 (value, null);
2294 [CLSCompliant (false)]
2295 public static uint ToUInt32 (object value, IFormatProvider provider)
2299 return ((IConvertible) value).ToUInt32 (provider);
2303 // ========== UInt64 Conversions ========== //
2305 [CLSCompliant (false)]
2306 public static ulong ToUInt64 (bool value)
2308 return (ulong)(value ? 1 : 0);
2311 [CLSCompliant (false)]
2312 public static ulong ToUInt64 (byte value)
2314 return (ulong)value;
2317 [CLSCompliant (false)]
2318 public static ulong ToUInt64 (char value)
2320 return (ulong)value;
2323 [CLSCompliant (false)]
2324 public static ulong ToUInt64 (DateTime value)
2326 throw new InvalidCastException ("The conversion is not supported.");
2329 [CLSCompliant (false)]
2330 public static ulong ToUInt64 (decimal value)
2332 if (value > UInt64.MaxValue || value < UInt64.MinValue)
2333 throw new OverflowException (Locale.GetText (
2334 "Value is greater than UInt64.MaxValue or less than UInt64.MinValue"));
2336 // Returned Even-Rounded
2337 return (ulong)(Math.Round (value));
2340 [CLSCompliant (false)]
2341 public static ulong ToUInt64 (double value)
2343 if (value > UInt64.MaxValue || value < UInt64.MinValue)
2344 throw new OverflowException (Locale.GetText (
2345 "Value is greater than UInt64.MaxValue or less than UInt64.MinValue"));
2347 // Returned Even-Rounded
2348 return (ulong)(Math.Round (value));
2351 [CLSCompliant (false)]
2352 public static ulong ToUInt64 (float value)
2354 if (value > UInt64.MaxValue || value < UInt64.MinValue)
2355 throw new OverflowException (Locale.GetText (
2356 "Value is greater than UInt64.MaxValue or less than UInt64.MinValue"));
2358 // Returned Even-Rounded, pass as a double to Math
2359 return (ulong)(Math.Round ( (double)value));
2362 [CLSCompliant (false)]
2363 public static ulong ToUInt64 (int value)
2365 if (value < (int)UInt64.MinValue)
2366 throw new OverflowException (Locale.GetText (
2367 "Value is less than UInt64.MinValue"));
2369 return (ulong)value;
2372 [CLSCompliant (false)]
2373 public static ulong ToUInt64 (long value)
2375 if (value < (long)UInt64.MinValue)
2376 throw new OverflowException (Locale.GetText (
2377 "Value is less than UInt64.MinValue"));
2379 return (ulong)value;
2382 [CLSCompliant (false)]
2383 public static ulong ToUInt64 (sbyte value)
2385 if (value < (sbyte)UInt64.MinValue)
2386 throw new OverflowException
2387 ("Value is less than UInt64.MinValue");
2389 return (ulong)value;
2392 [CLSCompliant (false)]
2393 public static ulong ToUInt64 (short value)
2395 if (value < (short)UInt64.MinValue)
2396 throw new OverflowException (Locale.GetText (
2397 "Value is less than UInt64.MinValue"));
2399 return (ulong)value;
2402 [CLSCompliant (false)]
2403 public static ulong ToUInt64 (string value)
2406 return 0; // LAMESPEC: Spec says throw ArgumentNullException
2407 return UInt64.Parse (value);
2410 [CLSCompliant (false)]
2411 public static ulong ToUInt64 (string value, IFormatProvider provider)
2414 return 0; // LAMESPEC: Spec says throw ArgumentNullException
2415 return UInt64.Parse (value, provider);
2418 [CLSCompliant (false)]
2419 public static ulong ToUInt64 (string value, int fromBase)
2421 return (ulong) ConvertFromBase64 (value, fromBase, true);
2424 [CLSCompliant (false)]
2425 public static ulong ToUInt64 (uint value)
2427 return (ulong)value;
2430 [CLSCompliant (false)]
2431 public static ulong ToUInt64 (ulong value)
2436 [CLSCompliant (false)]
2437 public static ulong ToUInt64 (ushort value)
2439 return (ulong)value;
2442 [CLSCompliant (false)]
2443 public static ulong ToUInt64 (object value)
2447 return ToUInt64 (value, null);
2450 [CLSCompliant (false)]
2451 public static ulong ToUInt64 (object value, IFormatProvider provider)
2455 return ((IConvertible) value).ToUInt64 (provider);
2459 // ========== Conversion / Helper Functions ========== //
2461 public static object ChangeType (object value, Type conversionType)
2463 if ((value != null) && (conversionType == null))
2464 throw new ArgumentNullException ("conversionType");
2465 CultureInfo ci = CultureInfo.CurrentCulture;
2466 IFormatProvider provider;
2467 if (conversionType == typeof(DateTime)) {
2468 provider = ci.DateTimeFormat;
2471 provider = ci.NumberFormat;
2473 return ToType (value, conversionType, provider, true);
2476 public static object ChangeType (object value, TypeCode typeCode)
2478 CultureInfo ci = CultureInfo.CurrentCulture;
2479 Type conversionType = conversionTable [(int) typeCode];
2480 IFormatProvider provider;
2481 if (conversionType == typeof(DateTime)) {
2482 provider = ci.DateTimeFormat;
2485 provider = ci.NumberFormat;
2487 return ToType (value, conversionType, provider, true);
2490 public static object ChangeType (object value, Type conversionType, IFormatProvider provider)
2492 if ((value != null) && (conversionType == null))
2493 throw new ArgumentNullException ("conversionType");
2494 return ToType (value, conversionType, provider, true);
2497 public static object ChangeType (object value, TypeCode typeCode, IFormatProvider provider)
2499 Type conversionType = conversionTable [(int)typeCode];
2500 return ToType (value, conversionType, provider, true);
2503 private static bool NotValidBase (int value)
2505 if ((value == 2) || (value == 8) ||
2506 (value == 10) || (value == 16))
2512 private static int ConvertFromBase (string value, int fromBase, bool unsigned)
2514 if (NotValidBase (fromBase))
2515 throw new ArgumentException ("fromBase is not valid.");
2524 int len = value.Length;
2525 bool negative = false;
2527 // special processing for some bases
2530 if (value.Substring (i, 1) == "-") {
2532 throw new OverflowException (
2533 Locale.GetText ("The string was being parsed as"
2534 + " an unsigned number and could not have a"
2535 + " negative sign."));
2542 if (value.Substring (i, 1) == "-") {
2543 throw new ArgumentException ("String cannot contain a "
2544 + "minus sign if the base is not 10.");
2548 if ((value[i] == '0') && ((value [i+1] == 'x') || (value [i+1] == 'X'))) {
2554 if (value.Substring (i, 1) == "-") {
2555 throw new ArgumentException ("String cannot contain a "
2556 + "minus sign if the base is not 10.");
2562 throw new FormatException ("Could not find any parsable digits.");
2565 if (value[i] == '+') {
2570 char c = value[i++];
2571 if (Char.IsNumber (c)) {
2572 digitValue = c - '0';
2573 } else if (Char.IsLetter (c)) {
2574 digitValue = Char.ToLowerInvariant (c) - 'a' + 10;
2577 throw new FormatException ("Additional unparsable "
2578 + "characters are at the end of the string.");
2580 throw new FormatException ("Could not find any parsable"
2585 if (digitValue >= fromBase) {
2587 throw new FormatException ("Additional unparsable "
2588 + "characters are at the end of the string.");
2590 throw new FormatException ("Could not find any parsable"
2595 result = (fromBase) * result + digitValue;
2600 throw new FormatException ("Could not find any parsable digits.");
2608 // note: this has nothing to do with base64 encoding (just base and Int64)
2609 private static long ConvertFromBase64 (string value, int fromBase, bool unsigned)
2611 if (NotValidBase (fromBase))
2612 throw new ArgumentException ("fromBase is not valid.");
2617 int digitValue = -1;
2619 bool negative = false;
2622 int len = value.Length;
2624 // special processing for some bases
2627 if (value.Substring(i, 1) == "-") {
2629 throw new OverflowException (
2630 Locale.GetText ("The string was being parsed as"
2631 + " an unsigned number and could not have a"
2632 + " negative sign."));
2639 if (value.Substring (i, 1) == "-") {
2640 throw new ArgumentException ("String cannot contain a "
2641 + "minus sign if the base is not 10.");
2645 if ((value[i] == '0') && ((value[i + 1] == 'x') || (value[i + 1] == 'X'))) {
2651 if (value.Substring (i, 1) == "-") {
2652 throw new ArgumentException ("String cannot contain a "
2653 + "minus sign if the base is not 10.");
2659 throw new FormatException ("Could not find any parsable digits.");
2662 if (value[i] == '+') {
2667 char c = value[i++];
2668 if (Char.IsNumber (c)) {
2669 digitValue = c - '0';
2670 } else if (Char.IsLetter (c)) {
2671 digitValue = Char.ToLowerInvariant (c) - 'a' + 10;
2674 throw new FormatException ("Additional unparsable "
2675 + "characters are at the end of the string.");
2677 throw new FormatException ("Could not find any parsable"
2682 if (digitValue >= fromBase) {
2684 throw new FormatException ("Additional unparsable "
2685 + "characters are at the end of the string.");
2687 throw new FormatException ("Could not find any parsable"
2692 result = (fromBase * result + digitValue);
2697 throw new FormatException ("Could not find any parsable digits.");
2705 private static void EndianSwap (ref byte[] value)
2707 byte[] buf = new byte[value.Length];
2708 for (int i = 0; i < value.Length; i++)
2709 buf[i] = value[value.Length-1-i];
2713 private static string ConvertToBase2 (byte[] value)
2715 if (!BitConverter.IsLittleEndian)
2716 EndianSwap (ref value);
2717 StringBuilder sb = new StringBuilder ();
2718 for (int i = value.Length - 1; i >= 0; i--) {
2720 for (int j = 0; j < 8; j++) {
2721 if ((b & 0x80) == 0x80) {
2731 return sb.ToString ();
2734 private static string ConvertToBase8 (byte[] value)
2737 switch (value.Length) {
2739 l = (ulong) value [0];
2742 l = (ulong) BitConverter.ToUInt16 (value, 0);
2745 l = (ulong) BitConverter.ToUInt32 (value, 0);
2748 l = BitConverter.ToUInt64 (value, 0);
2751 throw new ArgumentException ("value");
2754 StringBuilder sb = new StringBuilder ();
2755 for (int i = 21; i >= 0; i--) {
2756 // 3 bits at the time
2757 char val = (char) ((l >> i * 3) & 0x7);
2758 if ((val != 0) || (sb.Length > 0)) {
2763 return sb.ToString ();
2766 private static string ConvertToBase16 (byte[] value)
2768 if (!BitConverter.IsLittleEndian)
2769 EndianSwap (ref value);
2770 StringBuilder sb = new StringBuilder ();
2771 for (int i = value.Length - 1; i >= 0; i--) {
2772 char high = (char)((value[i] >> 4) & 0x0f);
2773 if ((high != 0) || (sb.Length > 0)) {
2783 char low = (char)(value[i] & 0x0f);
2784 if ((low != 0) || (sb.Length > 0)) {
2794 return sb.ToString ();
2797 // Lookup table for the conversion ToType method. Order
2798 // is important! Used by ToType for comparing the target
2799 // type, and uses hardcoded array indexes.
2800 private static readonly Type[] conversionTable = {
2801 // Valid ICovnertible Types
2803 typeof (object), // 1 TypeCode.Object
2804 typeof (DBNull), // 2 TypeCode.DBNull
2805 typeof (Boolean), // 3 TypeCode.Boolean
2806 typeof (Char), // 4 TypeCode.Char
2807 typeof (SByte), // 5 TypeCode.SByte
2808 typeof (Byte), // 6 TypeCode.Byte
2809 typeof (Int16), // 7 TypeCode.Int16
2810 typeof (UInt16), // 8 TypeCode.UInt16
2811 typeof (Int32), // 9 TypeCode.Int32
2812 typeof (UInt32), // 10 TypeCode.UInt32
2813 typeof (Int64), // 11 TypeCode.Int64
2814 typeof (UInt64), // 12 TypeCode.UInt64
2815 typeof (Single), // 13 TypeCode.Single
2816 typeof (Double), // 14 TypeCode.Double
2817 typeof (Decimal), // 15 TypeCode.Decimal
2818 typeof (DateTime), // 16 TypeCode.DateTime
2820 typeof (String), // 18 TypeCode.String
2823 // Function to convert an object to another type and return
2824 // it as an object. In place for the core data types to use
2825 // when implementing IConvertible. Uses hardcoded indexes in
2826 // the conversionTypes array, so if modify carefully.
2829 // The `try_target_to_type' boolean indicates if the code
2830 // should try to call the IConvertible.ToType method if everything
2833 // This should be true for invocations from Convert.cs, and
2834 // false from the mscorlib types that implement IConvertible that
2835 // all into this internal function.
2837 // This was added to keep the fix for #481687 working and to avoid
2838 // the regression that the simple fix introduced (485377)
2839 internal static object ToType (object value, Type conversionType, IFormatProvider provider, bool try_target_to_type)
2841 if (value == null) {
2842 if ((conversionType != null) && conversionType.IsValueType){
2843 throw new InvalidCastException ("Null object can not be converted to a value type.");
2848 if (conversionType == null)
2849 throw new InvalidCastException ("Cannot cast to destination type.");
2851 if (value.GetType () == conversionType)
2854 if (value is IConvertible) {
2855 IConvertible convertValue = (IConvertible) value;
2857 if (conversionType == conversionTable[0]) // 0 Empty
2858 throw new ArgumentNullException ();
2860 else if (conversionType == conversionTable[1]) // 1 TypeCode.Object
2861 return (object) value;
2863 else if (conversionType == conversionTable[2]) // 2 TypeCode.DBNull
2864 throw new InvalidCastException (
2865 "Cannot cast to DBNull, it's not IConvertible");
2867 else if (conversionType == conversionTable[3]) // 3 TypeCode.Boolean
2868 return (object) convertValue.ToBoolean (provider);
2870 else if (conversionType == conversionTable[4]) // 4 TypeCode.Char
2871 return (object) convertValue.ToChar (provider);
2873 else if (conversionType == conversionTable[5]) // 5 TypeCode.SByte
2874 return (object) convertValue.ToSByte (provider);
2876 else if (conversionType == conversionTable[6]) // 6 TypeCode.Byte
2877 return (object) convertValue.ToByte (provider);
2879 else if (conversionType == conversionTable[7]) // 7 TypeCode.Int16
2880 return (object) convertValue.ToInt16 (provider);
2882 else if (conversionType == conversionTable[8]) // 8 TypeCode.UInt16
2883 return (object) convertValue.ToUInt16 (provider);
2885 else if (conversionType == conversionTable[9]) // 9 TypeCode.Int32
2886 return (object) convertValue.ToInt32 (provider);
2888 else if (conversionType == conversionTable[10]) // 10 TypeCode.UInt32
2889 return (object) convertValue.ToUInt32 (provider);
2891 else if (conversionType == conversionTable[11]) // 11 TypeCode.Int64
2892 return (object) convertValue.ToInt64 (provider);
2894 else if (conversionType == conversionTable[12]) // 12 TypeCode.UInt64
2895 return (object) convertValue.ToUInt64 (provider);
2897 else if (conversionType == conversionTable[13]) // 13 TypeCode.Single
2898 return (object) convertValue.ToSingle (provider);
2900 else if (conversionType == conversionTable[14]) // 14 TypeCode.Double
2901 return (object) convertValue.ToDouble (provider);
2903 else if (conversionType == conversionTable[15]) // 15 TypeCode.Decimal
2904 return (object) convertValue.ToDecimal (provider);
2906 else if (conversionType == conversionTable[16]) // 16 TypeCode.DateTime
2907 return (object) convertValue.ToDateTime (provider);
2909 else if (conversionType == conversionTable[18]) // 18 TypeCode.String
2910 return (object) convertValue.ToString (provider);
2912 if (try_target_to_type)
2913 return convertValue.ToType (conversionType, provider);
2916 // Not in the conversion table
2917 throw new InvalidCastException ((Locale.GetText (
2918 "Value is not a convertible object: " + value.GetType().ToString() + " to " + conversionType.FullName)));