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
1211 return (int)(Math.Round (value));
1215 public static int ToInt32 (float value)
1217 if (value > Int32.MaxValue || value < Int32.MinValue)
1218 throw new OverflowException (Locale.GetText (
1219 "Value is greater than Int32.MaxValue or less than Int32.MinValue"));
1221 // Returned Even-Rounded, pass as a double, could just call
1222 // Convert.ToInt32 ( (double)value);
1224 return (int)(Math.Round ( (double)value));
1228 public static int ToInt32 (int value)
1233 public static int ToInt32 (long value)
1235 if (value > Int32.MaxValue || value < Int32.MinValue)
1236 throw new OverflowException (Locale.GetText (
1237 "Value is greater than Int32.MaxValue or less than Int32.MinValue"));
1242 [CLSCompliant (false)]
1243 public static int ToInt32 (sbyte value)
1248 public static int ToInt32 (short value)
1253 public static int ToInt32 (string value)
1256 return 0; // LAMESPEC: Spec says throw ArgumentNullException
1257 return Int32.Parse (value);
1260 public static int ToInt32 (string value, IFormatProvider provider)
1263 return 0; // LAMESPEC: Spec says throw ArgumentNullException
1264 return Int32.Parse (value, provider);
1268 public static int ToInt32 (string value, int fromBase)
1270 return ConvertFromBase (value, fromBase, false);
1273 [CLSCompliant (false)]
1274 public static int ToInt32 (uint value)
1276 if (value > Int32.MaxValue)
1277 throw new OverflowException (Locale.GetText (
1278 "Value is greater than Int32.MaxValue"));
1283 [CLSCompliant (false)]
1284 public static int ToInt32 (ulong value)
1286 if (value > Int32.MaxValue)
1287 throw new OverflowException (Locale.GetText (
1288 "Value is greater than Int32.MaxValue"));
1293 [CLSCompliant (false)]
1294 public static int ToInt32 (ushort value)
1299 public static int ToInt32 (object value)
1303 return ToInt32 (value, null);
1306 public static int ToInt32 (object value, IFormatProvider provider)
1310 return ((IConvertible) value).ToInt32 (provider);
1313 // ========== Int64 Conversions ========== //
1315 public static long ToInt64 (bool value)
1317 return value ? 1 : 0;
1320 public static long ToInt64 (byte value)
1322 return (long)(ulong)value;
1325 public static long ToInt64 (char value)
1330 public static long ToInt64 (DateTime value)
1332 throw new InvalidCastException ("This conversion is not supported.");
1335 public static long ToInt64 (decimal value)
1337 if (value > Int64.MaxValue || value < Int64.MinValue)
1338 throw new OverflowException (Locale.GetText (
1339 "Value is greater than Int64.MaxValue or less than Int64.MinValue"));
1341 // Returned Even-Rounded
1342 return (long)(Math.Round (value));
1345 public static long ToInt64 (double value)
1347 if (value > Int64.MaxValue || value < Int64.MinValue)
1348 throw new OverflowException (Locale.GetText (
1349 "Value is greater than Int64.MaxValue or less than Int64.MinValue"));
1351 // Returned Even-Rounded
1352 return (long)(Math.Round (value));
1355 public static long ToInt64 (float value)
1357 if (value > Int64.MaxValue || value < Int64.MinValue)
1358 throw new OverflowException (Locale.GetText (
1359 "Value is greater than Int64.MaxValue or less than Int64.MinValue"));
1361 // Returned Even-Rounded, pass to Math as a double, could
1362 // just call Convert.ToInt64 ( (double)value);
1363 return (long)(Math.Round ( (double)value));
1366 public static long ToInt64 (int value)
1371 public static long ToInt64 (long value)
1376 [CLSCompliant (false)]
1377 public static long ToInt64 (sbyte value)
1382 public static long ToInt64 (short value)
1387 public static long ToInt64 (string value)
1390 return 0; // LAMESPEC: Spec says throw ArgumentNullException
1391 return Int64.Parse (value);
1394 public static long ToInt64 (string value, IFormatProvider provider)
1397 return 0; // LAMESPEC: Spec says throw ArgumentNullException
1398 return Int64.Parse (value, provider);
1401 public static long ToInt64 (string value, int fromBase)
1403 return ConvertFromBase64 (value, fromBase, false);
1406 [CLSCompliant (false)]
1407 public static long ToInt64 (uint value)
1409 return (long)(ulong)value;
1412 [CLSCompliant (false)]
1413 public static long ToInt64 (ulong value)
1415 if (value > Int64.MaxValue)
1416 throw new OverflowException (Locale.GetText (
1417 "Value is greater than Int64.MaxValue"));
1422 [CLSCompliant (false)]
1423 public static long ToInt64 (ushort value)
1425 return (long)(ulong)value;
1428 public static long ToInt64 (object value)
1432 return ToInt64 (value, null);
1435 public static long ToInt64 (object value, IFormatProvider provider)
1439 return ((IConvertible) value).ToInt64 (provider);
1442 // ========== SByte Conversions ========== //
1444 [CLSCompliant (false)]
1445 public static sbyte ToSByte (bool value)
1447 return (sbyte)(value ? 1 : 0);
1450 [CLSCompliant (false)]
1451 public static sbyte ToSByte (byte value)
1453 if (value > SByte.MaxValue)
1454 throw new OverflowException (Locale.GetText (
1455 "Value is greater than SByte.MaxValue"));
1457 return (sbyte)value;
1460 [CLSCompliant (false)]
1461 public static sbyte ToSByte (char value)
1463 if (value > SByte.MaxValue)
1464 throw new OverflowException (Locale.GetText (
1465 "Value is greater than SByte.MaxValue"));
1467 return (sbyte)value;
1470 [CLSCompliant (false)]
1471 public static sbyte ToSByte (DateTime value)
1473 throw new InvalidCastException ("This conversion is not supported.");
1476 [CLSCompliant (false)]
1477 public static sbyte ToSByte (decimal value)
1479 if (value > SByte.MaxValue || value < SByte.MinValue)
1480 throw new OverflowException (Locale.GetText (
1481 "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
1483 // Returned Even-Rounded
1484 return (sbyte)(Math.Round (value));
1487 [CLSCompliant (false)]
1488 public static sbyte ToSByte (double value)
1490 if (value > SByte.MaxValue || value < SByte.MinValue)
1491 throw new OverflowException (Locale.GetText (
1492 "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
1494 // Returned Even-Rounded
1495 return (sbyte)(Math.Round (value));
1498 [CLSCompliant (false)]
1499 public static sbyte ToSByte (float value)
1501 if (value > SByte.MaxValue || value < SByte.MinValue)
1502 throw new OverflowException (Locale.GetText (
1503 "Value is greater than SByte.MaxValue or less than SByte.Minalue"));
1505 // Returned Even-Rounded, pass as double to Math
1506 return (sbyte)(Math.Round ( (double)value));
1509 [CLSCompliant (false)]
1510 public static sbyte ToSByte (int value)
1512 if (value > SByte.MaxValue || value < SByte.MinValue)
1513 throw new OverflowException (Locale.GetText (
1514 "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
1516 return (sbyte)value;
1519 [CLSCompliant (false)]
1520 public static sbyte ToSByte (long value)
1522 if (value > SByte.MaxValue || value < SByte.MinValue)
1523 throw new OverflowException (Locale.GetText (
1524 "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
1526 return (sbyte)value;
1529 [CLSCompliant (false)]
1530 public static sbyte ToSByte (sbyte value)
1535 [CLSCompliant (false)]
1536 public static sbyte ToSByte (short value)
1538 if (value > SByte.MaxValue || value < SByte.MinValue)
1539 throw new OverflowException (Locale.GetText (
1540 "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
1542 return (sbyte)value;
1545 [CLSCompliant (false)]
1546 public static sbyte ToSByte (string value)
1549 return 0; // LAMESPEC: Spec says throw ArgumentNullException
1550 return SByte.Parse (value);
1553 [CLSCompliant (false)]
1554 public static sbyte ToSByte (string value, IFormatProvider provider)
1557 throw new ArgumentNullException ("value");
1558 return SByte.Parse (value, provider);
1561 [CLSCompliant (false)]
1562 public static sbyte ToSByte (string value, int fromBase)
1564 int result = ConvertFromBase (value, fromBase, false);
1565 if (fromBase != 10) {
1566 // note: no sign are available to detect negatives
1567 if (result > SByte.MaxValue) {
1568 // return negative 2's complement
1569 return Convert.ToSByte (-(256 - result));
1572 return Convert.ToSByte (result);
1575 [CLSCompliant (false)]
1576 public static sbyte ToSByte (uint value)
1578 if (value > SByte.MaxValue)
1579 throw new OverflowException (Locale.GetText (
1580 "Value is greater than SByte.MaxValue"));
1582 return (sbyte)value;
1585 [CLSCompliant (false)]
1586 public static sbyte ToSByte (ulong value)
1588 if (value > (ulong)SByte.MaxValue)
1589 throw new OverflowException (Locale.GetText (
1590 "Value is greater than SByte.MaxValue"));
1592 return (sbyte)value;
1595 [CLSCompliant (false)]
1596 public static sbyte ToSByte (ushort value)
1598 if (value > SByte.MaxValue)
1599 throw new OverflowException (Locale.GetText (
1600 "Value is greater than SByte.MaxValue"));
1602 return (sbyte)value;
1605 [CLSCompliant (false)]
1606 public static sbyte ToSByte (object value)
1610 return ToSByte (value, null);
1613 [CLSCompliant (false)]
1614 public static sbyte ToSByte (object value, IFormatProvider provider)
1618 return ((IConvertible) value).ToSByte (provider);
1621 // ========== Single Conversions ========== //
1623 public static float ToSingle (bool value)
1625 return value ? 1 : 0;
1628 public static float ToSingle (byte value)
1630 return (float)value;
1633 public static float ToSingle (Char value)
1635 throw new InvalidCastException ("This conversion is not supported.");
1638 public static float ToSingle (DateTime value)
1640 throw new InvalidCastException ("This conversion is not supported.");
1643 public static float ToSingle (decimal value)
1645 return (float)value;
1648 public static float ToSingle (double value)
1650 return (float)value;
1653 public static float ToSingle (float value)
1658 public static float ToSingle (int value)
1660 return (float)value;
1663 public static float ToSingle (long value)
1665 return (float)value;
1668 [CLSCompliant (false)]
1669 public static float ToSingle (sbyte value)
1671 return (float)value;
1674 public static float ToSingle (short value)
1676 return (float)value;
1679 public static float ToSingle (string value)
1682 return 0.0f; // LAMESPEC: Spec says throw ArgumentNullException
1683 return Single.Parse (value);
1686 public static float ToSingle (string value, IFormatProvider provider)
1689 return 0.0f; // LAMESPEC: Spec says throw ArgumentNullException
1690 return Single.Parse (value, provider);
1693 [CLSCompliant (false)]
1694 public static float ToSingle (uint value)
1696 return (float)value;
1699 [CLSCompliant (false)]
1700 public static float ToSingle (ulong value)
1702 return (float)value;
1705 [CLSCompliant (false)]
1706 public static float ToSingle (ushort value)
1708 return (float)value;
1711 public static float ToSingle (object value)
1715 return ToSingle (value, null);
1718 // [CLSCompliant (false)]
1719 public static float ToSingle (object value, IFormatProvider provider)
1723 return ((IConvertible) value).ToSingle (provider);
1726 // ========== String Conversions ========== //
1728 public static string ToString (bool value)
1730 return value.ToString ();
1733 public static string ToString (bool value, IFormatProvider provider)
1735 return value.ToString (); // the same as ToString (bool).
1738 public static string ToString (byte value)
1740 return value.ToString ();
1743 public static string ToString (byte value, IFormatProvider provider)
1745 return value.ToString (provider);
1748 public static string ToString (byte value, int toBase)
1753 return value.ToString ();
1755 byte[] val = BitConverter.GetBytes (value);
1759 return ConvertToBase2 (val);
1761 return ConvertToBase8 (val);
1763 return ConvertToBase16 (val);
1765 throw new ArgumentException (Locale.GetText ("toBase is not valid."));
1769 public static string ToString (char value)
1771 return value.ToString ();
1774 public static string ToString (char value, IFormatProvider provider)
1776 return value.ToString (); // the same as ToString (char)
1779 public static string ToString (DateTime value)
1781 return value.ToString ();
1784 public static string ToString (DateTime value, IFormatProvider provider)
1786 return value.ToString (provider);
1789 public static string ToString (decimal value)
1791 return value.ToString ();
1794 public static string ToString (decimal value, IFormatProvider provider)
1796 return value.ToString (provider);
1799 public static string ToString (double value)
1801 return value.ToString ();
1804 public static string ToString (double value, IFormatProvider provider)
1806 return value.ToString (provider);
1809 public static string ToString (float value)
1811 return value.ToString ();
1814 public static string ToString (float value, IFormatProvider provider)
1816 return value.ToString (provider);
1819 public static string ToString (int value)
1821 return value.ToString ();
1824 public static string ToString (int value, int toBase)
1829 return value.ToString ();
1831 byte[] val = BitConverter.GetBytes (value);
1835 return ConvertToBase2 (val);
1837 return ConvertToBase8 (val);
1839 return ConvertToBase16 (val);
1841 throw new ArgumentException (Locale.GetText ("toBase is not valid."));
1845 public static string ToString (int value, IFormatProvider provider)
1847 return value.ToString (provider);
1850 public static string ToString (long value)
1852 return value.ToString ();
1855 public static string ToString (long value, int toBase)
1860 return value.ToString ();
1862 byte[] val = BitConverter.GetBytes (value);
1866 return ConvertToBase2 (val);
1868 return ConvertToBase8 (val);
1870 return ConvertToBase16 (val);
1872 throw new ArgumentException (Locale.GetText ("toBase is not valid."));
1876 public static string ToString (long value, IFormatProvider provider)
1878 return value.ToString (provider);
1881 public static string ToString (object value)
1883 return ToString (value, null);
1886 public static string ToString (object value, IFormatProvider provider)
1888 if (value is IConvertible)
1889 return ((IConvertible) value).ToString (provider);
1890 else if (value != null)
1891 return value.ToString ();
1892 return String.Empty;
1895 [CLSCompliant (false)]
1896 public static string ToString (sbyte value)
1898 return value.ToString ();
1901 [CLSCompliant (false)]
1902 public static string ToString (sbyte value, IFormatProvider provider)
1904 return value.ToString (provider);
1907 public static string ToString (short value)
1909 return value.ToString ();
1912 public static string ToString (short value, int toBase)
1917 return value.ToString ();
1919 byte[] val = BitConverter.GetBytes (value);
1923 return ConvertToBase2 (val);
1925 return ConvertToBase8 (val);
1927 return ConvertToBase16 (val);
1929 throw new ArgumentException (Locale.GetText ("toBase is not valid."));
1933 public static string ToString (short value, IFormatProvider provider)
1935 return value.ToString (provider);
1938 public static string ToString (string value)
1943 public static string ToString (string value, IFormatProvider provider)
1945 return value; // provider is ignored.
1948 [CLSCompliant (false)]
1949 public static string ToString (uint value)
1951 return value.ToString ();
1954 [CLSCompliant (false)]
1955 public static string ToString (uint value, IFormatProvider provider)
1957 return value.ToString (provider);
1960 [CLSCompliant (false)]
1961 public static string ToString (ulong value)
1963 return value.ToString ();
1966 [CLSCompliant (false)]
1967 public static string ToString (ulong value, IFormatProvider provider)
1969 return value.ToString (provider);
1972 [CLSCompliant (false)]
1973 public static string ToString (ushort value)
1975 return value.ToString ();
1978 [CLSCompliant (false)]
1979 public static string ToString (ushort value, IFormatProvider provider)
1981 return value.ToString (provider);
1984 // ========== UInt16 Conversions ========== //
1986 [CLSCompliant (false)]
1987 public static ushort ToUInt16 (bool value)
1989 return (ushort)(value ? 1 : 0);
1992 [CLSCompliant (false)]
1993 public static ushort ToUInt16 (byte value)
1995 return (ushort)value;
1998 [CLSCompliant (false)]
1999 public static ushort ToUInt16 (char value)
2001 return (ushort)value;
2004 [CLSCompliant (false)]
2005 public static ushort ToUInt16 (DateTime value)
2007 throw new InvalidCastException ("This conversion is not supported.");
2010 [CLSCompliant (false)]
2011 public static ushort ToUInt16 (decimal value)
2013 if (value > UInt16.MaxValue || value < UInt16.MinValue)
2014 throw new OverflowException (Locale.GetText (
2015 "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
2017 // Returned Even-Rounded
2018 return (ushort)(Math.Round (value));
2021 [CLSCompliant (false)]
2022 public static ushort ToUInt16 (double value)
2024 if (value > UInt16.MaxValue || value < UInt16.MinValue)
2025 throw new OverflowException (Locale.GetText (
2026 "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
2028 // Returned Even-Rounded
2029 return (ushort)(Math.Round (value));
2032 [CLSCompliant (false)]
2033 public static ushort ToUInt16 (float value)
2035 if (value > UInt16.MaxValue || value < UInt16.MinValue)
2036 throw new OverflowException (Locale.GetText (
2037 "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
2039 // Returned Even-Rounded, pass as double to Math
2040 return (ushort)(Math.Round ( (double)value));
2043 [CLSCompliant (false)]
2044 public static ushort ToUInt16 (int value)
2046 if (value > UInt16.MaxValue || value < UInt16.MinValue)
2047 throw new OverflowException (Locale.GetText (
2048 "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
2050 return (ushort)value;
2053 [CLSCompliant (false)]
2054 public static ushort ToUInt16 (long value)
2056 if (value > UInt16.MaxValue || value < UInt16.MinValue)
2057 throw new OverflowException (Locale.GetText (
2058 "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
2060 return (ushort)value;
2063 [CLSCompliant (false)]
2064 public static ushort ToUInt16 (sbyte value)
2066 if (value < UInt16.MinValue)
2067 throw new OverflowException (Locale.GetText (
2068 "Value is less than UInt16.MinValue"));
2070 return (ushort)value;
2073 [CLSCompliant (false)]
2074 public static ushort ToUInt16 (short value)
2076 if (value < UInt16.MinValue)
2077 throw new OverflowException (Locale.GetText (
2078 "Value is less than UInt16.MinValue"));
2080 return (ushort)value;
2083 [CLSCompliant (false)]
2084 public static ushort ToUInt16 (string value)
2087 return 0; // LAMESPEC: Spec says throw ArgumentNullException
2088 return UInt16.Parse (value);
2091 [CLSCompliant (false)]
2092 public static ushort ToUInt16 (string value, IFormatProvider provider)
2095 return 0; // LAMESPEC: Spec says throw ArgumentNullException
2096 return UInt16.Parse (value, provider);
2099 [CLSCompliant (false)]
2100 public static ushort ToUInt16 (string value, int fromBase)
2102 return ToUInt16 (ConvertFromBase (value, fromBase, true));
2105 [CLSCompliant (false)]
2106 public static ushort ToUInt16 (uint value)
2108 if (value > UInt16.MaxValue)
2109 throw new OverflowException (Locale.GetText (
2110 "Value is greater than UInt16.MaxValue"));
2112 return (ushort)value;
2115 [CLSCompliant (false)]
2116 public static ushort ToUInt16 (ulong value)
2118 if (value > (ulong)UInt16.MaxValue)
2119 throw new OverflowException (Locale.GetText (
2120 "Value is greater than UInt16.MaxValue"));
2122 return (ushort)value;
2125 [CLSCompliant (false)]
2126 public static ushort ToUInt16 (ushort value)
2131 [CLSCompliant (false)]
2132 public static ushort ToUInt16 (object value)
2136 return ToUInt16 (value, null);
2139 [CLSCompliant (false)]
2140 public static ushort ToUInt16 (object value, IFormatProvider provider)
2144 return ((IConvertible) value).ToUInt16 (provider);
2147 // ========== UInt32 Conversions ========== //
2149 [CLSCompliant (false)]
2150 public static uint ToUInt32 (bool value)
2152 return (uint)(value ? 1 : 0);
2155 [CLSCompliant (false)]
2156 public static uint ToUInt32 (byte value)
2161 [CLSCompliant (false)]
2162 public static uint ToUInt32 (char value)
2167 [CLSCompliant (false)]
2168 public static uint ToUInt32 (DateTime value)
2170 throw new InvalidCastException ("This conversion is not supported.");
2173 [CLSCompliant (false)]
2174 public static uint ToUInt32 (decimal value)
2176 if (value > UInt32.MaxValue || value < UInt32.MinValue)
2177 throw new OverflowException (Locale.GetText (
2178 "Value is greater than UInt32.MaxValue or less than UInt32.MinValue"));
2180 // Returned Even-Rounded
2181 return (uint)(Math.Round (value));
2184 [CLSCompliant (false)]
2185 public static uint ToUInt32 (double value)
2187 if (value > UInt32.MaxValue || value < UInt32.MinValue)
2188 throw new OverflowException (Locale.GetText (
2189 "Value is greater than UInt32.MaxValue or less than UInt32.MinValue"));
2191 // Returned Even-Rounded
2192 return (uint)(Math.Round (value));
2195 [CLSCompliant (false)]
2196 public static uint ToUInt32 (float value)
2198 if (value > UInt32.MaxValue || value < UInt32.MinValue)
2199 throw new OverflowException (Locale.GetText (
2200 "Value is greater than UInt32.MaxValue or less than UInt32.MinValue"));
2202 // Returned Even-Rounded, pass as double to Math
2203 return (uint)(Math.Round ( (double)value));
2206 [CLSCompliant (false)]
2207 public static uint ToUInt32 (int value)
2209 if (value < UInt32.MinValue)
2210 throw new OverflowException (Locale.GetText (
2211 "Value is less than UInt32.MinValue"));
2216 [CLSCompliant (false)]
2217 public static uint ToUInt32 (long value)
2219 if (value > UInt32.MaxValue || value < UInt32.MinValue)
2220 throw new OverflowException (Locale.GetText (
2221 "Value is greater than UInt32.MaxValue or less than UInt32.MinValue"));
2226 [CLSCompliant (false)]
2227 public static uint ToUInt32 (sbyte value)
2229 if (value < UInt32.MinValue)
2230 throw new OverflowException (Locale.GetText (
2231 "Value is less than UInt32.MinValue"));
2236 [CLSCompliant (false)]
2237 public static uint ToUInt32 (short value)
2239 if (value < UInt32.MinValue)
2240 throw new OverflowException (Locale.GetText (
2241 "Value is less than UInt32.MinValue"));
2246 [CLSCompliant (false)]
2247 public static uint ToUInt32 (string value)
2250 return 0; // LAMESPEC: Spec says throw ArgumentNullException
2251 return UInt32.Parse (value);
2254 [CLSCompliant (false)]
2255 public static uint ToUInt32 (string value, IFormatProvider provider)
2258 return 0; // LAMESPEC: Spec says throw ArgumentNullException
2259 return UInt32.Parse (value, provider);
2262 [CLSCompliant (false)]
2263 public static uint ToUInt32 (string value, int fromBase)
2265 return (uint) ConvertFromBase (value, fromBase, true);
2268 [CLSCompliant (false)]
2269 public static uint ToUInt32 (uint value)
2274 [CLSCompliant (false)]
2275 public static uint ToUInt32 (ulong value)
2277 if (value > UInt32.MaxValue)
2278 throw new OverflowException (Locale.GetText (
2279 "Value is greater than UInt32.MaxValue"));
2284 [CLSCompliant (false)]
2285 public static uint ToUInt32 (ushort value)
2290 [CLSCompliant (false)]
2291 public static uint ToUInt32 (object value)
2295 return ToUInt32 (value, null);
2298 [CLSCompliant (false)]
2299 public static uint ToUInt32 (object value, IFormatProvider provider)
2303 return ((IConvertible) value).ToUInt32 (provider);
2307 // ========== UInt64 Conversions ========== //
2309 [CLSCompliant (false)]
2310 public static ulong ToUInt64 (bool value)
2312 return (ulong)(value ? 1 : 0);
2315 [CLSCompliant (false)]
2316 public static ulong ToUInt64 (byte value)
2318 return (ulong)value;
2321 [CLSCompliant (false)]
2322 public static ulong ToUInt64 (char value)
2324 return (ulong)value;
2327 [CLSCompliant (false)]
2328 public static ulong ToUInt64 (DateTime value)
2330 throw new InvalidCastException ("The conversion is not supported.");
2333 [CLSCompliant (false)]
2334 public static ulong ToUInt64 (decimal value)
2336 if (value > UInt64.MaxValue || value < UInt64.MinValue)
2337 throw new OverflowException (Locale.GetText (
2338 "Value is greater than UInt64.MaxValue or less than UInt64.MinValue"));
2340 // Returned Even-Rounded
2341 return (ulong)(Math.Round (value));
2344 [CLSCompliant (false)]
2345 public static ulong ToUInt64 (double value)
2347 if (value > UInt64.MaxValue || value < UInt64.MinValue)
2348 throw new OverflowException (Locale.GetText (
2349 "Value is greater than UInt64.MaxValue or less than UInt64.MinValue"));
2351 // Returned Even-Rounded
2352 return (ulong)(Math.Round (value));
2355 [CLSCompliant (false)]
2356 public static ulong ToUInt64 (float value)
2358 if (value > UInt64.MaxValue || value < UInt64.MinValue)
2359 throw new OverflowException (Locale.GetText (
2360 "Value is greater than UInt64.MaxValue or less than UInt64.MinValue"));
2362 // Returned Even-Rounded, pass as a double to Math
2363 return (ulong)(Math.Round ( (double)value));
2366 [CLSCompliant (false)]
2367 public static ulong ToUInt64 (int value)
2369 if (value < (int)UInt64.MinValue)
2370 throw new OverflowException (Locale.GetText (
2371 "Value is less than UInt64.MinValue"));
2373 return (ulong)value;
2376 [CLSCompliant (false)]
2377 public static ulong ToUInt64 (long value)
2379 if (value < (long)UInt64.MinValue)
2380 throw new OverflowException (Locale.GetText (
2381 "Value is less than UInt64.MinValue"));
2383 return (ulong)value;
2386 [CLSCompliant (false)]
2387 public static ulong ToUInt64 (sbyte value)
2389 if (value < (sbyte)UInt64.MinValue)
2390 throw new OverflowException
2391 ("Value is less than UInt64.MinValue");
2393 return (ulong)value;
2396 [CLSCompliant (false)]
2397 public static ulong ToUInt64 (short value)
2399 if (value < (short)UInt64.MinValue)
2400 throw new OverflowException (Locale.GetText (
2401 "Value is less than UInt64.MinValue"));
2403 return (ulong)value;
2406 [CLSCompliant (false)]
2407 public static ulong ToUInt64 (string value)
2410 return 0; // LAMESPEC: Spec says throw ArgumentNullException
2411 return UInt64.Parse (value);
2414 [CLSCompliant (false)]
2415 public static ulong ToUInt64 (string value, IFormatProvider provider)
2418 return 0; // LAMESPEC: Spec says throw ArgumentNullException
2419 return UInt64.Parse (value, provider);
2422 [CLSCompliant (false)]
2423 public static ulong ToUInt64 (string value, int fromBase)
2425 return (ulong) ConvertFromBase64 (value, fromBase, true);
2428 [CLSCompliant (false)]
2429 public static ulong ToUInt64 (uint value)
2431 return (ulong)value;
2434 [CLSCompliant (false)]
2435 public static ulong ToUInt64 (ulong value)
2440 [CLSCompliant (false)]
2441 public static ulong ToUInt64 (ushort value)
2443 return (ulong)value;
2446 [CLSCompliant (false)]
2447 public static ulong ToUInt64 (object value)
2451 return ToUInt64 (value, null);
2454 [CLSCompliant (false)]
2455 public static ulong ToUInt64 (object value, IFormatProvider provider)
2459 return ((IConvertible) value).ToUInt64 (provider);
2463 // ========== Conversion / Helper Functions ========== //
2465 public static object ChangeType (object value, Type conversionType)
2467 if ((value != null) && (conversionType == null))
2468 throw new ArgumentNullException ("conversionType");
2469 CultureInfo ci = CultureInfo.CurrentCulture;
2470 IFormatProvider provider;
2471 if (conversionType == typeof(DateTime)) {
2472 provider = ci.DateTimeFormat;
2475 provider = ci.NumberFormat;
2477 return ToType (value, conversionType, provider, true);
2480 public static object ChangeType (object value, TypeCode typeCode)
2482 CultureInfo ci = CultureInfo.CurrentCulture;
2483 Type conversionType = conversionTable [(int) typeCode];
2484 IFormatProvider provider;
2485 if (conversionType == typeof(DateTime)) {
2486 provider = ci.DateTimeFormat;
2489 provider = ci.NumberFormat;
2491 return ToType (value, conversionType, provider, true);
2494 public static object ChangeType (object value, Type conversionType, IFormatProvider provider)
2496 if ((value != null) && (conversionType == null))
2497 throw new ArgumentNullException ("conversionType");
2498 return ToType (value, conversionType, provider, true);
2501 public static object ChangeType (object value, TypeCode typeCode, IFormatProvider provider)
2503 Type conversionType = conversionTable [(int)typeCode];
2504 return ToType (value, conversionType, provider, true);
2507 private static bool NotValidBase (int value)
2509 if ((value == 2) || (value == 8) ||
2510 (value == 10) || (value == 16))
2516 private static int ConvertFromBase (string value, int fromBase, bool unsigned)
2518 if (NotValidBase (fromBase))
2519 throw new ArgumentException ("fromBase is not valid.");
2528 int len = value.Length;
2529 bool negative = false;
2531 // special processing for some bases
2534 if (value.Substring (i, 1) == "-") {
2536 throw new OverflowException (
2537 Locale.GetText ("The string was being parsed as"
2538 + " an unsigned number and could not have a"
2539 + " negative sign."));
2546 if (value.Substring (i, 1) == "-") {
2547 throw new ArgumentException ("String cannot contain a "
2548 + "minus sign if the base is not 10.");
2552 if ((value[i] == '0') && ((value [i+1] == 'x') || (value [i+1] == 'X'))) {
2558 if (value.Substring (i, 1) == "-") {
2559 throw new ArgumentException ("String cannot contain a "
2560 + "minus sign if the base is not 10.");
2566 throw new FormatException ("Could not find any parsable digits.");
2569 if (value[i] == '+') {
2574 char c = value[i++];
2575 if (Char.IsNumber (c)) {
2576 digitValue = c - '0';
2577 } else if (Char.IsLetter (c)) {
2578 digitValue = Char.ToLowerInvariant (c) - 'a' + 10;
2581 throw new FormatException ("Additional unparsable "
2582 + "characters are at the end of the string.");
2584 throw new FormatException ("Could not find any parsable"
2589 if (digitValue >= fromBase) {
2591 throw new FormatException ("Additional unparsable "
2592 + "characters are at the end of the string.");
2594 throw new FormatException ("Could not find any parsable"
2599 result = (fromBase) * result + digitValue;
2604 throw new FormatException ("Could not find any parsable digits.");
2612 // note: this has nothing to do with base64 encoding (just base and Int64)
2613 private static long ConvertFromBase64 (string value, int fromBase, bool unsigned)
2615 if (NotValidBase (fromBase))
2616 throw new ArgumentException ("fromBase is not valid.");
2621 int digitValue = -1;
2623 bool negative = false;
2626 int len = value.Length;
2628 // special processing for some bases
2631 if (value.Substring(i, 1) == "-") {
2633 throw new OverflowException (
2634 Locale.GetText ("The string was being parsed as"
2635 + " an unsigned number and could not have a"
2636 + " negative sign."));
2643 if (value.Substring (i, 1) == "-") {
2644 throw new ArgumentException ("String cannot contain a "
2645 + "minus sign if the base is not 10.");
2649 if ((value[i] == '0') && ((value[i + 1] == 'x') || (value[i + 1] == 'X'))) {
2655 if (value.Substring (i, 1) == "-") {
2656 throw new ArgumentException ("String cannot contain a "
2657 + "minus sign if the base is not 10.");
2663 throw new FormatException ("Could not find any parsable digits.");
2666 if (value[i] == '+') {
2671 char c = value[i++];
2672 if (Char.IsNumber (c)) {
2673 digitValue = c - '0';
2674 } else if (Char.IsLetter (c)) {
2675 digitValue = Char.ToLowerInvariant (c) - 'a' + 10;
2678 throw new FormatException ("Additional unparsable "
2679 + "characters are at the end of the string.");
2681 throw new FormatException ("Could not find any parsable"
2686 if (digitValue >= fromBase) {
2688 throw new FormatException ("Additional unparsable "
2689 + "characters are at the end of the string.");
2691 throw new FormatException ("Could not find any parsable"
2696 result = (fromBase * result + digitValue);
2701 throw new FormatException ("Could not find any parsable digits.");
2709 private static void EndianSwap (ref byte[] value)
2711 byte[] buf = new byte[value.Length];
2712 for (int i = 0; i < value.Length; i++)
2713 buf[i] = value[value.Length-1-i];
2717 private static string ConvertToBase2 (byte[] value)
2719 if (!BitConverter.IsLittleEndian)
2720 EndianSwap (ref value);
2721 StringBuilder sb = new StringBuilder ();
2722 for (int i = value.Length - 1; i >= 0; i--) {
2724 for (int j = 0; j < 8; j++) {
2725 if ((b & 0x80) == 0x80) {
2735 return sb.ToString ();
2738 private static string ConvertToBase8 (byte[] value)
2741 switch (value.Length) {
2743 l = (ulong) value [0];
2746 l = (ulong) BitConverter.ToUInt16 (value, 0);
2749 l = (ulong) BitConverter.ToUInt32 (value, 0);
2752 l = BitConverter.ToUInt64 (value, 0);
2755 throw new ArgumentException ("value");
2758 StringBuilder sb = new StringBuilder ();
2759 for (int i = 21; i >= 0; i--) {
2760 // 3 bits at the time
2761 char val = (char) ((l >> i * 3) & 0x7);
2762 if ((val != 0) || (sb.Length > 0)) {
2767 return sb.ToString ();
2770 private static string ConvertToBase16 (byte[] value)
2772 if (!BitConverter.IsLittleEndian)
2773 EndianSwap (ref value);
2774 StringBuilder sb = new StringBuilder ();
2775 for (int i = value.Length - 1; i >= 0; i--) {
2776 char high = (char)((value[i] >> 4) & 0x0f);
2777 if ((high != 0) || (sb.Length > 0)) {
2787 char low = (char)(value[i] & 0x0f);
2788 if ((low != 0) || (sb.Length > 0)) {
2798 return sb.ToString ();
2801 // Lookup table for the conversion ToType method. Order
2802 // is important! Used by ToType for comparing the target
2803 // type, and uses hardcoded array indexes.
2804 private static readonly Type[] conversionTable = {
2805 // Valid ICovnertible Types
2807 typeof (object), // 1 TypeCode.Object
2808 typeof (DBNull), // 2 TypeCode.DBNull
2809 typeof (Boolean), // 3 TypeCode.Boolean
2810 typeof (Char), // 4 TypeCode.Char
2811 typeof (SByte), // 5 TypeCode.SByte
2812 typeof (Byte), // 6 TypeCode.Byte
2813 typeof (Int16), // 7 TypeCode.Int16
2814 typeof (UInt16), // 8 TypeCode.UInt16
2815 typeof (Int32), // 9 TypeCode.Int32
2816 typeof (UInt32), // 10 TypeCode.UInt32
2817 typeof (Int64), // 11 TypeCode.Int64
2818 typeof (UInt64), // 12 TypeCode.UInt64
2819 typeof (Single), // 13 TypeCode.Single
2820 typeof (Double), // 14 TypeCode.Double
2821 typeof (Decimal), // 15 TypeCode.Decimal
2822 typeof (DateTime), // 16 TypeCode.DateTime
2824 typeof (String), // 18 TypeCode.String
2827 // Function to convert an object to another type and return
2828 // it as an object. In place for the core data types to use
2829 // when implementing IConvertible. Uses hardcoded indexes in
2830 // the conversionTypes array, so if modify carefully.
2833 // The `try_target_to_type' boolean indicates if the code
2834 // should try to call the IConvertible.ToType method if everything
2837 // This should be true for invocations from Convert.cs, and
2838 // false from the mscorlib types that implement IConvertible that
2839 // all into this internal function.
2841 // This was added to keep the fix for #481687 working and to avoid
2842 // the regression that the simple fix introduced (485377)
2843 internal static object ToType (object value, Type conversionType, IFormatProvider provider, bool try_target_to_type)
2845 if (value == null) {
2846 if ((conversionType != null) && conversionType.IsValueType){
2847 throw new InvalidCastException ("Null object can not be converted to a value type.");
2852 if (conversionType == null)
2853 throw new InvalidCastException ("Cannot cast to destination type.");
2855 if (value.GetType () == conversionType)
2858 if (value is IConvertible) {
2859 IConvertible convertValue = (IConvertible) value;
2861 if (conversionType == conversionTable[0]) // 0 Empty
2862 throw new ArgumentNullException ();
2864 else if (conversionType == conversionTable[1]) // 1 TypeCode.Object
2865 return (object) value;
2867 else if (conversionType == conversionTable[2]) // 2 TypeCode.DBNull
2868 throw new InvalidCastException (
2869 "Cannot cast to DBNull, it's not IConvertible");
2871 else if (conversionType == conversionTable[3]) // 3 TypeCode.Boolean
2872 return (object) convertValue.ToBoolean (provider);
2874 else if (conversionType == conversionTable[4]) // 4 TypeCode.Char
2875 return (object) convertValue.ToChar (provider);
2877 else if (conversionType == conversionTable[5]) // 5 TypeCode.SByte
2878 return (object) convertValue.ToSByte (provider);
2880 else if (conversionType == conversionTable[6]) // 6 TypeCode.Byte
2881 return (object) convertValue.ToByte (provider);
2883 else if (conversionType == conversionTable[7]) // 7 TypeCode.Int16
2884 return (object) convertValue.ToInt16 (provider);
2886 else if (conversionType == conversionTable[8]) // 8 TypeCode.UInt16
2887 return (object) convertValue.ToUInt16 (provider);
2889 else if (conversionType == conversionTable[9]) // 9 TypeCode.Int32
2890 return (object) convertValue.ToInt32 (provider);
2892 else if (conversionType == conversionTable[10]) // 10 TypeCode.UInt32
2893 return (object) convertValue.ToUInt32 (provider);
2895 else if (conversionType == conversionTable[11]) // 11 TypeCode.Int64
2896 return (object) convertValue.ToInt64 (provider);
2898 else if (conversionType == conversionTable[12]) // 12 TypeCode.UInt64
2899 return (object) convertValue.ToUInt64 (provider);
2901 else if (conversionType == conversionTable[13]) // 13 TypeCode.Single
2902 return (object) convertValue.ToSingle (provider);
2904 else if (conversionType == conversionTable[14]) // 14 TypeCode.Double
2905 return (object) convertValue.ToDouble (provider);
2907 else if (conversionType == conversionTable[15]) // 15 TypeCode.Decimal
2908 return (object) convertValue.ToDecimal (provider);
2910 else if (conversionType == conversionTable[16]) // 16 TypeCode.DateTime
2911 return (object) convertValue.ToDateTime (provider);
2913 else if (conversionType == conversionTable[18]) // 18 TypeCode.String
2914 return (object) convertValue.ToString (provider);
2916 if (try_target_to_type)
2917 return convertValue.ToType (conversionType, provider);
2920 // Not in the conversion table
2921 throw new InvalidCastException ((Locale.GetText (
2922 "Value is not a convertible object: " + value.GetType().ToString() + " to " + conversionType.FullName)));