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)]
104 public static class Convert {
106 public sealed class Convert {
113 public static readonly object DBNull = System.DBNull.Value;
114 static ToBase64Transform toBase64Transform = new ToBase64Transform();
116 [MethodImplAttribute (MethodImplOptions.InternalCall)]
117 extern static byte [] InternalFromBase64String (string str, bool allowWhitespaceOnly);
119 [MethodImplAttribute (MethodImplOptions.InternalCall)]
120 extern static byte [] InternalFromBase64CharArray (char [] arr, int offset, int length);
122 public static byte[] FromBase64CharArray (char[] inArray, int offset, int length)
125 throw new ArgumentNullException ("inArray");
127 throw new ArgumentOutOfRangeException ("offset < 0");
129 throw new ArgumentOutOfRangeException ("length < 0");
130 // avoid integer overflow
131 if (offset > inArray.Length - length)
132 throw new ArgumentOutOfRangeException ("offset + length > array.Length");
134 return InternalFromBase64CharArray (inArray, offset, length);
137 public static byte[] FromBase64String (string s)
140 throw new ArgumentNullException ("s");
147 return InternalFromBase64String (s, true);
149 return InternalFromBase64String (s, false);
153 public static TypeCode GetTypeCode (object value)
156 return TypeCode.Empty;
158 return Type.GetTypeCode (value.GetType ());
161 public static bool IsDBNull (object value)
169 public static int ToBase64CharArray (byte[] inArray, int offsetIn, int length,
170 char[] outArray, int offsetOut)
173 throw new ArgumentNullException ("inArray");
174 if (outArray == null)
175 throw new ArgumentNullException ("outArray");
176 if (offsetIn < 0 || length < 0 || offsetOut < 0)
177 throw new ArgumentOutOfRangeException ("offsetIn, length, offsetOut < 0");
178 // avoid integer overflow
179 if (offsetIn > inArray.Length - length)
180 throw new ArgumentOutOfRangeException ("offsetIn + length > array.Length");
182 // note: normally ToBase64Transform doesn't support multiple block processing
183 byte[] outArr = toBase64Transform.InternalTransformFinalBlock (inArray, offsetIn, length);
185 char[] cOutArr = new ASCIIEncoding ().GetChars (outArr);
187 // avoid integer overflow
188 if (offsetOut > outArray.Length - cOutArr.Length)
189 throw new ArgumentOutOfRangeException ("offsetOut + cOutArr.Length > outArray.Length");
191 Array.Copy (cOutArr, 0, outArray, offsetOut, cOutArr.Length);
193 return cOutArr.Length;
196 public static string ToBase64String (byte[] inArray)
199 throw new ArgumentNullException ("inArray");
201 return ToBase64String (inArray, 0, inArray.Length);
204 public static string ToBase64String (byte[] inArray, int offset, int length)
207 throw new ArgumentNullException ("inArray");
208 if (offset < 0 || length < 0)
209 throw new ArgumentOutOfRangeException ("offset < 0 || length < 0");
210 // avoid integer overflow
211 if (offset > inArray.Length - length)
212 throw new ArgumentOutOfRangeException ("offset + length > array.Length");
214 // note: normally ToBase64Transform doesn't support multiple block processing
215 byte[] outArr = toBase64Transform.InternalTransformFinalBlock (inArray, offset, length);
217 return (new ASCIIEncoding ().GetString (outArr));
222 public static string ToBase64String (byte[] inArray, Base64FormattingOptions options)
225 throw new ArgumentNullException ("inArray");
226 return ToBase64String (inArray, 0, inArray.Length, options);
230 public static string ToBase64String (byte[] inArray, int offset, int length, Base64FormattingOptions options)
233 throw new ArgumentNullException ("inArray");
234 if (offset < 0 || length < 0)
235 throw new ArgumentOutOfRangeException ("offset < 0 || length < 0");
236 // avoid integer overflow
237 if (offset > inArray.Length - length)
238 throw new ArgumentOutOfRangeException ("offset + length > array.Length");
240 if (options == Base64FormattingOptions.InsertLineBreaks)
241 return ToBase64StringBuilderWithLine (inArray, offset, length).ToString ();
243 return Encoding.ASCII.GetString (toBase64Transform.InternalTransformFinalBlock (inArray, offset, length));
247 public static int ToBase64CharArray (byte[] inArray, int offsetIn, int length,
248 char[] outArray, int offsetOut, Base64FormattingOptions options)
251 throw new ArgumentNullException ("inArray");
252 if (outArray == null)
253 throw new ArgumentNullException ("outArray");
254 if (offsetIn < 0 || length < 0 || offsetOut < 0)
255 throw new ArgumentOutOfRangeException ("offsetIn, length, offsetOut < 0");
256 // avoid integer overflow
257 if (offsetIn > inArray.Length - length)
258 throw new ArgumentOutOfRangeException ("offsetIn + length > array.Length");
260 // note: normally ToBase64Transform doesn't support multiple block processing
261 if (options == Base64FormattingOptions.InsertLineBreaks) {
262 StringBuilder sb = ToBase64StringBuilderWithLine (inArray, offsetIn, length);
263 sb.CopyTo (0, outArray, offsetOut, sb.Length);
266 byte[] outArr = toBase64Transform.InternalTransformFinalBlock (inArray, offsetIn, length);
268 char[] cOutArr = Encoding.ASCII.GetChars (outArr);
270 // avoid integer overflow
271 if (offsetOut > outArray.Length - cOutArr.Length)
272 throw new ArgumentOutOfRangeException ("offsetOut + cOutArr.Length > outArray.Length");
274 Array.Copy (cOutArr, 0, outArray, offsetOut, cOutArr.Length);
275 return cOutArr.Length;
279 static StringBuilder ToBase64StringBuilderWithLine (byte [] inArray, int offset, int length)
281 BinaryReader reader = new BinaryReader (new MemoryStream (inArray, offset, length));
284 StringBuilder sb = new StringBuilder ();
286 // 54 bytes of input makes for 72 bytes of output.
287 b = reader.ReadBytes (54);
289 sb.AppendLine (Encoding.ASCII.GetString (toBase64Transform.InternalTransformFinalBlock (b, 0, b.Length)));
290 } while (b.Length > 0);
297 // ========== Boolean Conversions ========== //
299 public static bool ToBoolean (bool value)
304 public static bool ToBoolean (byte value)
309 public static bool ToBoolean (char value)
311 throw new InvalidCastException (Locale.GetText ("Can't convert char to bool"));
314 public static bool ToBoolean (DateTime value)
316 throw new InvalidCastException (Locale.GetText ("Can't convert date to bool"));
319 public static bool ToBoolean (decimal value)
321 return (value != 0M);
324 public static bool ToBoolean (double value)
329 public static bool ToBoolean (float value)
331 return (value != 0f);
334 public static bool ToBoolean (int value)
339 public static bool ToBoolean (long value)
344 [CLSCompliant (false)]
345 public static bool ToBoolean (sbyte value)
350 public static bool ToBoolean (short value)
355 public static bool ToBoolean (string value)
358 return false; // LAMESPEC: Spec says throw ArgumentNullException
359 return Boolean.Parse (value);
362 public static bool ToBoolean (string value, IFormatProvider provider)
365 return false; // LAMESPEC: Spec says throw ArgumentNullException
366 return Boolean.Parse (value); // provider is ignored.
369 [CLSCompliant (false)]
370 public static bool ToBoolean (uint value)
375 [CLSCompliant (false)]
376 public static bool ToBoolean (ulong value)
381 [CLSCompliant (false)]
382 public static bool ToBoolean (ushort value)
389 public static bool ToBoolean (object value)
393 return ToBoolean (value, null);
396 public static bool ToBoolean (object value, IFormatProvider provider)
400 return ((IConvertible) value).ToBoolean (provider);
403 // ========== Byte Conversions ========== //
405 public static byte ToByte (bool value)
407 return (byte)(value ? 1 : 0);
410 public static byte ToByte (byte value)
415 public static byte ToByte (char value)
417 if (value > Byte.MaxValue)
418 throw new OverflowException (Locale.GetText (
419 "Value is greater than Byte.MaxValue"));
424 public static byte ToByte (DateTime value)
426 throw new InvalidCastException ("This conversion is not supported.");
429 public static byte ToByte (decimal value)
431 if (value > Byte.MaxValue || value < Byte.MinValue)
432 throw new OverflowException (Locale.GetText (
433 "Value is greater than Byte.MaxValue or less than Byte.MinValue"));
435 // Returned Even-Rounded
436 return (byte)(Math.Round (value));
439 public static byte ToByte (double value)
441 if (value > Byte.MaxValue || value < Byte.MinValue)
442 throw new OverflowException (Locale.GetText (
443 "Value is greater than Byte.MaxValue or less than Byte.MinValue"));
445 // This and the float version of ToByte are the only ones
446 // the spec listed as checking for .NaN and Infinity overflow
447 if (Double.IsNaN(value) || Double.IsInfinity(value))
448 throw new OverflowException (Locale.GetText (
449 "Value is equal to Double.NaN, Double.PositiveInfinity, or Double.NegativeInfinity"));
451 // Returned Even-Rounded
452 return (byte)(Math.Round (value));
455 public static byte ToByte (float value)
457 if (value > Byte.MaxValue || value < Byte.MinValue)
458 throw new OverflowException (Locale.GetText (
459 "Value is greater than Byte.MaxValue or less than Byte.Minalue"));
461 // This and the double version of ToByte are the only ones
462 // the spec listed as checking for .NaN and Infinity overflow
463 if (Single.IsNaN(value) || Single.IsInfinity(value))
464 throw new OverflowException (Locale.GetText (
465 "Value is equal to Single.NaN, Single.PositiveInfinity, or Single.NegativeInfinity"));
467 // Returned Even-Rounded, pass it as a double, could have this
468 // method just call Convert.ToByte ( (double)value)
469 return (byte)(Math.Round ( (double)value));
472 public static byte ToByte (int value)
474 if (value > Byte.MaxValue || value < Byte.MinValue)
475 throw new OverflowException (Locale.GetText (
476 "Value is greater than Byte.MaxValue or less than Byte.MinValue"));
481 public static byte ToByte (long value)
483 if (value > Byte.MaxValue || value < Byte.MinValue)
484 throw new OverflowException (Locale.GetText (
485 "Value is greater than Byte.MaxValue or less than Byte.MinValue"));
490 [CLSCompliant (false)]
491 public static byte ToByte (sbyte value)
493 if (value < Byte.MinValue)
494 throw new OverflowException (Locale.GetText (
495 "Value is less than Byte.MinValue"));
500 public static byte ToByte (short value)
502 if (value > Byte.MaxValue || value < Byte.MinValue)
503 throw new OverflowException (Locale.GetText (
504 "Value is greater than Byte.MaxValue or less than Byte.MinValue"));
509 public static byte ToByte (string value)
512 return 0; // LAMESPEC: Spec says throw ArgumentNullException
513 return Byte.Parse (value);
516 public static byte ToByte (string value, IFormatProvider provider)
519 return 0; // LAMESPEC: Spec says throw ArgumentNullException
520 return Byte.Parse (value, provider);
523 public static byte ToByte (string value, int fromBase)
525 int retVal = ConvertFromBase (value, fromBase, true);
527 if (retVal < (int) Byte.MinValue || retVal > (int) Byte.MaxValue)
528 throw new OverflowException ();
530 return (byte) retVal;
533 [CLSCompliant (false)]
534 public static byte ToByte (uint value)
536 if (value > Byte.MaxValue)
537 throw new OverflowException (Locale.GetText (
538 "Value is greater than Byte.MaxValue"));
543 [CLSCompliant (false)]
544 public static byte ToByte (ulong value)
546 if (value > Byte.MaxValue)
547 throw new OverflowException (Locale.GetText (
548 "Value is greater than Byte.MaxValue"));
553 [CLSCompliant (false)]
554 public static byte ToByte (ushort value)
556 if (value > Byte.MaxValue)
557 throw new OverflowException (Locale.GetText (
558 "Value is greater than Byte.MaxValue"));
563 public static byte ToByte (object value)
567 return ToByte (value, null);
570 public static byte ToByte (object value, IFormatProvider provider)
574 return ((IConvertible) value).ToByte (provider);
577 // ========== Char Conversions ========== //
579 public static char ToChar (bool value)
581 throw new InvalidCastException ("This conversion is not supported.");
584 public static char ToChar (byte value)
589 public static char ToChar (char value)
594 public static char ToChar (DateTime value)
596 throw new InvalidCastException ("This conversion is not supported.");
599 public static char ToChar (decimal value)
601 throw new InvalidCastException ("This conversion is not supported.");
604 public static char ToChar (double value)
606 throw new InvalidCastException ("This conversion is not supported.");
609 public static char ToChar (int value)
611 if (value > Char.MaxValue || value < Char.MinValue)
612 throw new OverflowException (Locale.GetText (
613 "Value is greater than Char.MaxValue or less than Char.MinValue"));
618 public static char ToChar (long value)
620 if (value > Char.MaxValue || value < Char.MinValue)
621 throw new OverflowException (Locale.GetText (
622 "Value is greater than Char.MaxValue or less than Char.MinValue"));
627 public static char ToChar (float value)
629 throw new InvalidCastException ("This conversion is not supported.");
632 [CLSCompliant (false)]
633 public static char ToChar (sbyte value)
635 if (value < Char.MinValue)
636 throw new OverflowException (Locale.GetText (
637 "Value is less than Char.MinValue"));
642 public static char ToChar (short value)
644 if (value < Char.MinValue)
645 throw new OverflowException (Locale.GetText (
646 "Value is less than Char.MinValue"));
651 public static char ToChar (string value)
653 return Char.Parse (value);
656 public static char ToChar (string value, IFormatProvider provider)
658 return Char.Parse (value); // provider is ignored.
661 [CLSCompliant (false)]
662 public static char ToChar (uint value)
664 if (value > Char.MaxValue)
665 throw new OverflowException (Locale.GetText (
666 "Value is greater than Char.MaxValue"));
671 [CLSCompliant (false)]
672 public static char ToChar (ulong value)
674 if (value > Char.MaxValue)
675 throw new OverflowException (Locale.GetText (
676 "Value is greater than Char.MaxValue"));
681 [CLSCompliant (false)]
682 public static char ToChar (ushort value)
687 public static char ToChar (object value)
691 return ToChar (value, null);
694 public static char ToChar (object value, IFormatProvider provider)
698 return ((IConvertible) value).ToChar (provider);
701 // ========== DateTime Conversions ========== //
703 public static DateTime ToDateTime (string value)
706 return DateTime.MinValue; // LAMESPEC: Spec says throw ArgumentNullException
707 return DateTime.Parse (value);
710 public static DateTime ToDateTime (string value, IFormatProvider provider)
713 return DateTime.MinValue; // LAMESPEC: Spec says throw ArgumentNullException
714 return DateTime.Parse (value, provider);
717 public static DateTime ToDateTime (bool value)
719 throw new InvalidCastException ("This conversion is not supported.");
722 public static DateTime ToDateTime (byte value)
724 throw new InvalidCastException ("This conversion is not supported.");
727 public static DateTime ToDateTime (char value)
729 throw new InvalidCastException ("This conversion is not supported.");
732 public static DateTime ToDateTime (DateTime value)
737 public static DateTime ToDateTime (decimal value)
739 throw new InvalidCastException ("This conversion is not supported.");
742 public static DateTime ToDateTime (double value)
744 throw new InvalidCastException ("This conversion is not supported.");
747 public static DateTime ToDateTime (short value)
749 throw new InvalidCastException ("This conversion is not supported.");
752 public static DateTime ToDateTime (int value)
754 throw new InvalidCastException ("This conversion is not supported.");
757 public static DateTime ToDateTime (long value)
759 throw new InvalidCastException ("This conversion is not supported.");
762 public static DateTime ToDateTime (float value)
764 throw new InvalidCastException ("This conversion is not supported.");
767 public static DateTime ToDateTime (object value)
770 return DateTime.MinValue;
771 return ToDateTime (value, null);
774 public static DateTime ToDateTime (object value, IFormatProvider provider)
777 return DateTime.MinValue;
778 return ((IConvertible) value).ToDateTime (provider);
781 [CLSCompliant (false)]
782 public static DateTime ToDateTime (sbyte value)
784 throw new InvalidCastException ("This conversion is not supported.");
786 [CLSCompliant (false)]
787 public static DateTime ToDateTime (ushort value)
789 throw new InvalidCastException ("This conversion is not supported.");
792 [CLSCompliant (false)]
793 public static DateTime ToDateTime (uint value)
795 throw new InvalidCastException ("This conversion is not supported.");
798 [CLSCompliant (false)]
799 public static DateTime ToDateTime (ulong value)
801 throw new InvalidCastException ("This conversion is not supported.");
804 // ========== Decimal Conversions ========== //
806 public static decimal ToDecimal (bool value)
808 return value ? 1 : 0;
811 public static decimal ToDecimal (byte value)
813 return (decimal)value;
816 public static decimal ToDecimal (char value)
818 throw new InvalidCastException ("This conversion is not supported.");
821 public static decimal ToDecimal (DateTime value)
823 throw new InvalidCastException ("This conversion is not supported.");
826 public static decimal ToDecimal (decimal value)
831 public static decimal ToDecimal (double value)
833 return (decimal) value;
836 public static decimal ToDecimal (float value)
838 return (decimal) value;
841 public static decimal ToDecimal (int value)
843 return (decimal)value;
846 public static decimal ToDecimal (long value)
848 return (decimal)value;
851 [CLSCompliant (false)]
852 public static decimal ToDecimal (sbyte value)
854 return (decimal)value;
857 public static decimal ToDecimal (short value)
859 return (decimal)value;
862 public static decimal ToDecimal (string value)
865 return new Decimal (0); // LAMESPEC: Spec says throw ArgumentNullException
866 return Decimal.Parse (value);
869 public static decimal ToDecimal (string value, IFormatProvider provider)
872 return new Decimal (0); // LAMESPEC: Spec says throw ArgumentNullException
873 return Decimal.Parse (value, provider);
876 [CLSCompliant (false)]
877 public static decimal ToDecimal (uint value)
879 return (decimal)value;
882 [CLSCompliant (false)]
883 public static decimal ToDecimal (ulong value)
885 return (decimal)value;
888 [CLSCompliant (false)]
889 public static decimal ToDecimal (ushort value)
891 return (decimal)value;
894 public static decimal ToDecimal (object value)
897 return new Decimal (0);
898 return ToDecimal (value, null);
901 public static decimal ToDecimal (object value, IFormatProvider provider)
904 return new Decimal (0);
905 return ((IConvertible) value).ToDecimal (provider);
909 // ========== Double Conversions ========== //
911 public static double ToDouble (bool value)
913 return value ? 1 : 0;
916 public static double ToDouble (byte value)
918 return (double) value;
921 public static double ToDouble (char value)
923 throw new InvalidCastException ("This conversion is not supported.");
926 public static double ToDouble (DateTime value)
928 throw new InvalidCastException ("This conversion is not supported.");
931 public static double ToDouble (decimal value)
933 return (double)value;
936 public static double ToDouble (double value)
941 public static double ToDouble (float value)
943 return (double) value;
946 public static double ToDouble (int value)
948 return (double)value;
951 public static double ToDouble (long value)
953 return (double)value;
956 [CLSCompliant (false)]
957 public static double ToDouble (sbyte value)
959 return (double)value;
962 public static double ToDouble (short value)
964 return (double)value;
967 public static double ToDouble (string value)
970 return 0.0; // LAMESPEC: Spec says throw ArgumentNullException
971 return Double.Parse (value);
974 public static double ToDouble (string value, IFormatProvider provider)
977 return 0.0; // LAMESPEC: Spec says throw ArgumentNullException
978 return Double.Parse (value, provider);
981 [CLSCompliant (false)]
982 public static double ToDouble (uint value)
984 return (double)value;
987 [CLSCompliant (false)]
988 public static double ToDouble (ulong value)
990 return (double)value;
993 [CLSCompliant (false)]
994 public static double ToDouble (ushort value)
996 return (double)value;
999 public static double ToDouble (object value)
1003 return ToDouble (value, null);
1006 public static double ToDouble (object value, IFormatProvider provider)
1010 return ((IConvertible) value).ToDouble (provider);
1013 // ========== Int16 Conversions ========== //
1015 public static short ToInt16 (bool value)
1017 return (short)(value ? 1 : 0);
1020 public static short ToInt16 (byte value)
1022 return (short)value;
1025 public static short ToInt16 (char value)
1027 if (value > Int16.MaxValue)
1028 throw new OverflowException (Locale.GetText (
1029 "Value is greater than Int16.MaxValue"));
1031 return (short)value;
1034 public static short ToInt16 (DateTime value)
1036 throw new InvalidCastException ("This conversion is not supported.");
1039 public static short ToInt16 (decimal value)
1041 if (value > Int16.MaxValue || value < Int16.MinValue)
1042 throw new OverflowException (Locale.GetText (
1043 "Value is greater than Int16.MaxValue or less than Int16.MinValue"));
1045 // Returned Even-Rounded
1046 return (short)(Math.Round (value));
1049 public static short ToInt16 (double value)
1051 if (value > Int16.MaxValue || value < Int16.MinValue)
1052 throw new OverflowException (Locale.GetText (
1053 "Value is greater than Int16.MaxValue or less than Int16.MinValue"));
1055 // Returned Even-Rounded
1056 return (short)(Math.Round (value));
1059 public static short ToInt16 (float value)
1061 if (value > Int16.MaxValue || value < Int16.MinValue)
1062 throw new OverflowException (Locale.GetText (
1063 "Value is greater than Int16.MaxValue or less than Int16.MinValue"));
1065 // Returned Even-Rounded, use Math.Round pass as a double.
1066 return (short)Math.Round ( (double)value);
1069 public static short ToInt16 (int value)
1071 if (value > Int16.MaxValue || value < Int16.MinValue)
1072 throw new OverflowException (Locale.GetText (
1073 "Value is greater than Int16.MaxValue or less than Int16.MinValue"));
1075 return (short)value;
1078 public static short ToInt16 (long value)
1080 if (value > Int16.MaxValue || value < Int16.MinValue)
1081 throw new OverflowException (Locale.GetText (
1082 "Value is greater than Int16.MaxValue or less than Int16.MinValue"));
1084 return (short)value;
1087 [CLSCompliant (false)]
1088 public static short ToInt16 (sbyte value)
1090 return (short)value;
1093 public static short ToInt16 (short value)
1098 public static short ToInt16 (string value)
1101 return 0; // LAMESPEC: Spec says throw ArgumentNullException
1102 return Int16.Parse (value);
1105 public static short ToInt16 (string value, IFormatProvider provider)
1108 return 0; // LAMESPEC: Spec says throw ArgumentNullException
1109 return Int16.Parse (value, provider);
1112 public static short ToInt16 (string value, int fromBase)
1114 int result = ConvertFromBase (value, fromBase, false);
1115 if (fromBase != 10) {
1116 if (result > ushort.MaxValue) {
1117 throw new OverflowException ("Value was either too large or too small for an Int16.");
1120 // note: no sign are available to detect negatives
1121 if (result > Int16.MaxValue) {
1122 // return negative 2's complement
1123 return Convert.ToInt16 (-(65536 - result));
1126 return Convert.ToInt16 (result);
1129 [CLSCompliant (false)]
1130 public static short ToInt16 (uint value)
1132 if (value > Int16.MaxValue)
1133 throw new OverflowException (Locale.GetText (
1134 "Value is greater than Int16.MaxValue"));
1136 return (short)value;
1139 [CLSCompliant (false)]
1140 public static short ToInt16 (ulong value)
1142 if (value > (ulong)Int16.MaxValue)
1143 throw new OverflowException (Locale.GetText (
1144 "Value is greater than Int16.MaxValue"));
1145 return (short)value;
1148 [CLSCompliant (false)]
1149 public static short ToInt16 (ushort value)
1151 if (value > Int16.MaxValue)
1152 throw new OverflowException (Locale.GetText (
1153 "Value is greater than Int16.MaxValue"));
1155 return (short)value;
1158 public static short ToInt16 (object value)
1162 return ToInt16 (value, null);
1165 public static short ToInt16 (object value, IFormatProvider provider)
1169 return ((IConvertible) value).ToInt16 (provider);
1172 // ========== Int32 Conversions ========== //
1174 public static int ToInt32 (bool value)
1176 return value ? 1 : 0;
1179 public static int ToInt32 (byte value)
1184 public static int ToInt32 (char value)
1189 public static int ToInt32 (DateTime value)
1191 throw new InvalidCastException ("This conversion is not supported.");
1194 public static int ToInt32 (decimal value)
1196 if (value > Int32.MaxValue || value < Int32.MinValue)
1197 throw new OverflowException (Locale.GetText (
1198 "Value is greater than Int32.MaxValue or less than Int32.MinValue"));
1200 // Returned Even-Rounded
1201 return (int)(Math.Round (value));
1204 public static int ToInt32 (double value)
1206 if (value > Int32.MaxValue || value < Int32.MinValue)
1207 throw new OverflowException (Locale.GetText (
1208 "Value is greater than Int32.MaxValue or less than Int32.MinValue"));
1210 // Returned Even-Rounded
1211 return (int)(Math.Round (value));
1214 public static int ToInt32 (float value)
1216 if (value > Int32.MaxValue || value < Int32.MinValue)
1217 throw new OverflowException (Locale.GetText (
1218 "Value is greater than Int32.MaxValue or less than Int32.MinValue"));
1220 // Returned Even-Rounded, pass as a double, could just call
1221 // Convert.ToInt32 ( (double)value);
1222 return (int)(Math.Round ( (double)value));
1225 public static int ToInt32 (int value)
1230 public static int ToInt32 (long value)
1232 if (value > Int32.MaxValue || value < Int32.MinValue)
1233 throw new OverflowException (Locale.GetText (
1234 "Value is greater than Int32.MaxValue or less than Int32.MinValue"));
1239 [CLSCompliant (false)]
1240 public static int ToInt32 (sbyte value)
1245 public static int ToInt32 (short value)
1250 public static int ToInt32 (string value)
1253 return 0; // LAMESPEC: Spec says throw ArgumentNullException
1254 return Int32.Parse (value);
1257 public static int ToInt32 (string value, IFormatProvider provider)
1260 return 0; // LAMESPEC: Spec says throw ArgumentNullException
1261 return Int32.Parse (value, provider);
1265 public static int ToInt32 (string value, int fromBase)
1267 return ConvertFromBase (value, fromBase, false);
1270 [CLSCompliant (false)]
1271 public static int ToInt32 (uint value)
1273 if (value > Int32.MaxValue)
1274 throw new OverflowException (Locale.GetText (
1275 "Value is greater than Int32.MaxValue"));
1280 [CLSCompliant (false)]
1281 public static int ToInt32 (ulong value)
1283 if (value > Int32.MaxValue)
1284 throw new OverflowException (Locale.GetText (
1285 "Value is greater than Int32.MaxValue"));
1290 [CLSCompliant (false)]
1291 public static int ToInt32 (ushort value)
1296 public static int ToInt32 (object value)
1300 return ToInt32 (value, null);
1303 public static int ToInt32 (object value, IFormatProvider provider)
1307 return ((IConvertible) value).ToInt32 (provider);
1310 // ========== Int64 Conversions ========== //
1312 public static long ToInt64 (bool value)
1314 return value ? 1 : 0;
1317 public static long ToInt64 (byte value)
1319 return (long)(ulong)value;
1322 public static long ToInt64 (char value)
1327 public static long ToInt64 (DateTime value)
1329 throw new InvalidCastException ("This conversion is not supported.");
1332 public static long ToInt64 (decimal value)
1334 if (value > Int64.MaxValue || value < Int64.MinValue)
1335 throw new OverflowException (Locale.GetText (
1336 "Value is greater than Int64.MaxValue or less than Int64.MinValue"));
1338 // Returned Even-Rounded
1339 return (long)(Math.Round (value));
1342 public static long ToInt64 (double value)
1344 if (value > Int64.MaxValue || value < Int64.MinValue)
1345 throw new OverflowException (Locale.GetText (
1346 "Value is greater than Int64.MaxValue or less than Int64.MinValue"));
1348 // Returned Even-Rounded
1349 return (long)(Math.Round (value));
1352 public static long ToInt64 (float value)
1354 if (value > Int64.MaxValue || value < Int64.MinValue)
1355 throw new OverflowException (Locale.GetText (
1356 "Value is greater than Int64.MaxValue or less than Int64.MinValue"));
1358 // Returned Even-Rounded, pass to Math as a double, could
1359 // just call Convert.ToInt64 ( (double)value);
1360 return (long)(Math.Round ( (double)value));
1363 public static long ToInt64 (int value)
1368 public static long ToInt64 (long value)
1373 [CLSCompliant (false)]
1374 public static long ToInt64 (sbyte value)
1379 public static long ToInt64 (short value)
1384 public static long ToInt64 (string value)
1387 return 0; // LAMESPEC: Spec says throw ArgumentNullException
1388 return Int64.Parse (value);
1391 public static long ToInt64 (string value, IFormatProvider provider)
1394 return 0; // LAMESPEC: Spec says throw ArgumentNullException
1395 return Int64.Parse (value, provider);
1398 public static long ToInt64 (string value, int fromBase)
1400 return ConvertFromBase64 (value, fromBase, false);
1403 [CLSCompliant (false)]
1404 public static long ToInt64 (uint value)
1406 return (long)(ulong)value;
1409 [CLSCompliant (false)]
1410 public static long ToInt64 (ulong value)
1412 if (value > Int64.MaxValue)
1413 throw new OverflowException (Locale.GetText (
1414 "Value is greater than Int64.MaxValue"));
1419 [CLSCompliant (false)]
1420 public static long ToInt64 (ushort value)
1422 return (long)(ulong)value;
1425 public static long ToInt64 (object value)
1429 return ToInt64 (value, null);
1432 public static long ToInt64 (object value, IFormatProvider provider)
1436 return ((IConvertible) value).ToInt64 (provider);
1439 // ========== SByte Conversions ========== //
1441 [CLSCompliant (false)]
1442 public static sbyte ToSByte (bool value)
1444 return (sbyte)(value ? 1 : 0);
1447 [CLSCompliant (false)]
1448 public static sbyte ToSByte (byte value)
1450 if (value > SByte.MaxValue)
1451 throw new OverflowException (Locale.GetText (
1452 "Value is greater than SByte.MaxValue"));
1454 return (sbyte)value;
1457 [CLSCompliant (false)]
1458 public static sbyte ToSByte (char value)
1460 if (value > SByte.MaxValue)
1461 throw new OverflowException (Locale.GetText (
1462 "Value is greater than SByte.MaxValue"));
1464 return (sbyte)value;
1467 [CLSCompliant (false)]
1468 public static sbyte ToSByte (DateTime value)
1470 throw new InvalidCastException ("This conversion is not supported.");
1473 [CLSCompliant (false)]
1474 public static sbyte ToSByte (decimal value)
1476 if (value > SByte.MaxValue || value < SByte.MinValue)
1477 throw new OverflowException (Locale.GetText (
1478 "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
1480 // Returned Even-Rounded
1481 return (sbyte)(Math.Round (value));
1484 [CLSCompliant (false)]
1485 public static sbyte ToSByte (double value)
1487 if (value > SByte.MaxValue || value < SByte.MinValue)
1488 throw new OverflowException (Locale.GetText (
1489 "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
1491 // Returned Even-Rounded
1492 return (sbyte)(Math.Round (value));
1495 [CLSCompliant (false)]
1496 public static sbyte ToSByte (float value)
1498 if (value > SByte.MaxValue || value < SByte.MinValue)
1499 throw new OverflowException (Locale.GetText (
1500 "Value is greater than SByte.MaxValue or less than SByte.Minalue"));
1502 // Returned Even-Rounded, pass as double to Math
1503 return (sbyte)(Math.Round ( (double)value));
1506 [CLSCompliant (false)]
1507 public static sbyte ToSByte (int value)
1509 if (value > SByte.MaxValue || value < SByte.MinValue)
1510 throw new OverflowException (Locale.GetText (
1511 "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
1513 return (sbyte)value;
1516 [CLSCompliant (false)]
1517 public static sbyte ToSByte (long value)
1519 if (value > SByte.MaxValue || value < SByte.MinValue)
1520 throw new OverflowException (Locale.GetText (
1521 "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
1523 return (sbyte)value;
1526 [CLSCompliant (false)]
1527 public static sbyte ToSByte (sbyte value)
1532 [CLSCompliant (false)]
1533 public static sbyte ToSByte (short value)
1535 if (value > SByte.MaxValue || value < SByte.MinValue)
1536 throw new OverflowException (Locale.GetText (
1537 "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
1539 return (sbyte)value;
1542 [CLSCompliant (false)]
1543 public static sbyte ToSByte (string value)
1546 return 0; // LAMESPEC: Spec says throw ArgumentNullException
1547 return SByte.Parse (value);
1550 [CLSCompliant (false)]
1551 public static sbyte ToSByte (string value, IFormatProvider provider)
1554 throw new ArgumentNullException ("value");
1555 return SByte.Parse (value, provider);
1558 [CLSCompliant (false)]
1559 public static sbyte ToSByte (string value, int fromBase)
1561 int result = ConvertFromBase (value, fromBase, false);
1562 if (fromBase != 10) {
1563 // note: no sign are available to detect negatives
1564 if (result > SByte.MaxValue) {
1565 // return negative 2's complement
1566 return Convert.ToSByte (-(256 - result));
1569 return Convert.ToSByte (result);
1572 [CLSCompliant (false)]
1573 public static sbyte ToSByte (uint value)
1575 if (value > SByte.MaxValue)
1576 throw new OverflowException (Locale.GetText (
1577 "Value is greater than SByte.MaxValue"));
1579 return (sbyte)value;
1582 [CLSCompliant (false)]
1583 public static sbyte ToSByte (ulong value)
1585 if (value > (ulong)SByte.MaxValue)
1586 throw new OverflowException (Locale.GetText (
1587 "Value is greater than SByte.MaxValue"));
1589 return (sbyte)value;
1592 [CLSCompliant (false)]
1593 public static sbyte ToSByte (ushort value)
1595 if (value > SByte.MaxValue)
1596 throw new OverflowException (Locale.GetText (
1597 "Value is greater than SByte.MaxValue"));
1599 return (sbyte)value;
1602 [CLSCompliant (false)]
1603 public static sbyte ToSByte (object value)
1607 return ToSByte (value, null);
1610 [CLSCompliant (false)]
1611 public static sbyte ToSByte (object value, IFormatProvider provider)
1615 return ((IConvertible) value).ToSByte (provider);
1618 // ========== Single Conversions ========== //
1620 public static float ToSingle (bool value)
1622 return value ? 1 : 0;
1625 public static float ToSingle (byte value)
1627 return (float)value;
1630 public static float ToSingle (Char value)
1632 throw new InvalidCastException ("This conversion is not supported.");
1635 public static float ToSingle (DateTime value)
1637 throw new InvalidCastException ("This conversion is not supported.");
1640 public static float ToSingle (decimal value)
1642 return (float)value;
1645 public static float ToSingle (double value)
1647 return (float)value;
1650 public static float ToSingle (float value)
1655 public static float ToSingle (int value)
1657 return (float)value;
1660 public static float ToSingle (long value)
1662 return (float)value;
1665 [CLSCompliant (false)]
1666 public static float ToSingle (sbyte value)
1668 return (float)value;
1671 public static float ToSingle (short value)
1673 return (float)value;
1676 public static float ToSingle (string value)
1679 return 0.0f; // LAMESPEC: Spec says throw ArgumentNullException
1680 return Single.Parse (value);
1683 public static float ToSingle (string value, IFormatProvider provider)
1686 return 0.0f; // LAMESPEC: Spec says throw ArgumentNullException
1687 return Single.Parse (value, provider);
1690 [CLSCompliant (false)]
1691 public static float ToSingle (uint value)
1693 return (float)value;
1696 [CLSCompliant (false)]
1697 public static float ToSingle (ulong value)
1699 return (float)value;
1702 [CLSCompliant (false)]
1703 public static float ToSingle (ushort value)
1705 return (float)value;
1708 public static float ToSingle (object value)
1712 return ToSingle (value, null);
1715 // [CLSCompliant (false)]
1716 public static float ToSingle (object value, IFormatProvider provider)
1720 return ((IConvertible) value).ToSingle (provider);
1723 // ========== String Conversions ========== //
1725 public static string ToString (bool value)
1727 return value.ToString ();
1730 public static string ToString (bool value, IFormatProvider provider)
1732 return value.ToString (); // the same as ToString (bool).
1735 public static string ToString (byte value)
1737 return value.ToString ();
1740 public static string ToString (byte value, IFormatProvider provider)
1742 return value.ToString (provider);
1745 public static string ToString (byte value, int toBase)
1750 return value.ToString ();
1752 byte[] val = BitConverter.GetBytes (value);
1756 return ConvertToBase2 (val);
1758 return ConvertToBase8 (val);
1760 return ConvertToBase16 (val);
1762 throw new ArgumentException (Locale.GetText ("toBase is not valid."));
1766 public static string ToString (char value)
1768 return value.ToString ();
1771 public static string ToString (char value, IFormatProvider provider)
1773 return value.ToString (); // the same as ToString (char)
1776 public static string ToString (DateTime value)
1778 return value.ToString ();
1781 public static string ToString (DateTime value, IFormatProvider provider)
1783 return value.ToString (provider);
1786 public static string ToString (decimal value)
1788 return value.ToString ();
1791 public static string ToString (decimal value, IFormatProvider provider)
1793 return value.ToString (provider);
1796 public static string ToString (double value)
1798 return value.ToString ();
1801 public static string ToString (double value, IFormatProvider provider)
1803 return value.ToString (provider);
1806 public static string ToString (float value)
1808 return value.ToString ();
1811 public static string ToString (float value, IFormatProvider provider)
1813 return value.ToString (provider);
1816 public static string ToString (int value)
1818 return value.ToString ();
1821 public static string ToString (int value, int toBase)
1826 return value.ToString ();
1828 byte[] val = BitConverter.GetBytes (value);
1832 return ConvertToBase2 (val);
1834 return ConvertToBase8 (val);
1836 return ConvertToBase16 (val);
1838 throw new ArgumentException (Locale.GetText ("toBase is not valid."));
1842 public static string ToString (int value, IFormatProvider provider)
1844 return value.ToString (provider);
1847 public static string ToString (long value)
1849 return value.ToString ();
1852 public static string ToString (long value, int toBase)
1857 return value.ToString ();
1859 byte[] val = BitConverter.GetBytes (value);
1863 return ConvertToBase2 (val);
1865 return ConvertToBase8 (val);
1867 return ConvertToBase16 (val);
1869 throw new ArgumentException (Locale.GetText ("toBase is not valid."));
1873 public static string ToString (long value, IFormatProvider provider)
1875 return value.ToString (provider);
1878 public static string ToString (object value)
1880 return ToString (value, null);
1883 public static string ToString (object value, IFormatProvider provider)
1885 if (value is IConvertible)
1886 return ((IConvertible) value).ToString (provider);
1887 else if (value != null)
1888 return value.ToString ();
1889 return String.Empty;
1892 [CLSCompliant (false)]
1893 public static string ToString (sbyte value)
1895 return value.ToString ();
1898 [CLSCompliant (false)]
1899 public static string ToString (sbyte value, IFormatProvider provider)
1901 return value.ToString (provider);
1904 public static string ToString (short value)
1906 return value.ToString ();
1909 public static string ToString (short value, int toBase)
1914 return value.ToString ();
1916 byte[] val = BitConverter.GetBytes (value);
1920 return ConvertToBase2 (val);
1922 return ConvertToBase8 (val);
1924 return ConvertToBase16 (val);
1926 throw new ArgumentException (Locale.GetText ("toBase is not valid."));
1930 public static string ToString (short value, IFormatProvider provider)
1932 return value.ToString (provider);
1935 public static string ToString (string value)
1940 public static string ToString (string value, IFormatProvider provider)
1942 return value; // provider is ignored.
1945 [CLSCompliant (false)]
1946 public static string ToString (uint value)
1948 return value.ToString ();
1951 [CLSCompliant (false)]
1952 public static string ToString (uint value, IFormatProvider provider)
1954 return value.ToString (provider);
1957 [CLSCompliant (false)]
1958 public static string ToString (ulong value)
1960 return value.ToString ();
1963 [CLSCompliant (false)]
1964 public static string ToString (ulong value, IFormatProvider provider)
1966 return value.ToString (provider);
1969 [CLSCompliant (false)]
1970 public static string ToString (ushort value)
1972 return value.ToString ();
1975 [CLSCompliant (false)]
1976 public static string ToString (ushort value, IFormatProvider provider)
1978 return value.ToString (provider);
1981 // ========== UInt16 Conversions ========== //
1983 [CLSCompliant (false)]
1984 public static ushort ToUInt16 (bool value)
1986 return (ushort)(value ? 1 : 0);
1989 [CLSCompliant (false)]
1990 public static ushort ToUInt16 (byte value)
1992 return (ushort)value;
1995 [CLSCompliant (false)]
1996 public static ushort ToUInt16 (char value)
1998 return (ushort)value;
2001 [CLSCompliant (false)]
2002 public static ushort ToUInt16 (DateTime value)
2004 throw new InvalidCastException ("This conversion is not supported.");
2007 [CLSCompliant (false)]
2008 public static ushort ToUInt16 (decimal value)
2010 if (value > UInt16.MaxValue || value < UInt16.MinValue)
2011 throw new OverflowException (Locale.GetText (
2012 "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
2014 // Returned Even-Rounded
2015 return (ushort)(Math.Round (value));
2018 [CLSCompliant (false)]
2019 public static ushort ToUInt16 (double value)
2021 if (value > UInt16.MaxValue || value < UInt16.MinValue)
2022 throw new OverflowException (Locale.GetText (
2023 "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
2025 // Returned Even-Rounded
2026 return (ushort)(Math.Round (value));
2029 [CLSCompliant (false)]
2030 public static ushort ToUInt16 (float value)
2032 if (value > UInt16.MaxValue || value < UInt16.MinValue)
2033 throw new OverflowException (Locale.GetText (
2034 "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
2036 // Returned Even-Rounded, pass as double to Math
2037 return (ushort)(Math.Round ( (double)value));
2040 [CLSCompliant (false)]
2041 public static ushort ToUInt16 (int value)
2043 if (value > UInt16.MaxValue || value < UInt16.MinValue)
2044 throw new OverflowException (Locale.GetText (
2045 "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
2047 return (ushort)value;
2050 [CLSCompliant (false)]
2051 public static ushort ToUInt16 (long value)
2053 if (value > UInt16.MaxValue || value < UInt16.MinValue)
2054 throw new OverflowException (Locale.GetText (
2055 "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
2057 return (ushort)value;
2060 [CLSCompliant (false)]
2061 public static ushort ToUInt16 (sbyte value)
2063 if (value < UInt16.MinValue)
2064 throw new OverflowException (Locale.GetText (
2065 "Value is less than UInt16.MinValue"));
2067 return (ushort)value;
2070 [CLSCompliant (false)]
2071 public static ushort ToUInt16 (short value)
2073 if (value < UInt16.MinValue)
2074 throw new OverflowException (Locale.GetText (
2075 "Value is less than UInt16.MinValue"));
2077 return (ushort)value;
2080 [CLSCompliant (false)]
2081 public static ushort ToUInt16 (string value)
2084 return 0; // LAMESPEC: Spec says throw ArgumentNullException
2085 return UInt16.Parse (value);
2088 [CLSCompliant (false)]
2089 public static ushort ToUInt16 (string value, IFormatProvider provider)
2092 return 0; // LAMESPEC: Spec says throw ArgumentNullException
2093 return UInt16.Parse (value, provider);
2096 [CLSCompliant (false)]
2097 public static ushort ToUInt16 (string value, int fromBase)
2099 return ToUInt16 (ConvertFromBase (value, fromBase, true));
2102 [CLSCompliant (false)]
2103 public static ushort ToUInt16 (uint value)
2105 if (value > UInt16.MaxValue)
2106 throw new OverflowException (Locale.GetText (
2107 "Value is greater than UInt16.MaxValue"));
2109 return (ushort)value;
2112 [CLSCompliant (false)]
2113 public static ushort ToUInt16 (ulong value)
2115 if (value > (ulong)UInt16.MaxValue)
2116 throw new OverflowException (Locale.GetText (
2117 "Value is greater than UInt16.MaxValue"));
2119 return (ushort)value;
2122 [CLSCompliant (false)]
2123 public static ushort ToUInt16 (ushort value)
2128 [CLSCompliant (false)]
2129 public static ushort ToUInt16 (object value)
2133 return ToUInt16 (value, null);
2136 [CLSCompliant (false)]
2137 public static ushort ToUInt16 (object value, IFormatProvider provider)
2141 return ((IConvertible) value).ToUInt16 (provider);
2144 // ========== UInt32 Conversions ========== //
2146 [CLSCompliant (false)]
2147 public static uint ToUInt32 (bool value)
2149 return (uint)(value ? 1 : 0);
2152 [CLSCompliant (false)]
2153 public static uint ToUInt32 (byte value)
2158 [CLSCompliant (false)]
2159 public static uint ToUInt32 (char value)
2164 [CLSCompliant (false)]
2165 public static uint ToUInt32 (DateTime value)
2167 throw new InvalidCastException ("This conversion is not supported.");
2170 [CLSCompliant (false)]
2171 public static uint ToUInt32 (decimal value)
2173 if (value > UInt32.MaxValue || value < UInt32.MinValue)
2174 throw new OverflowException (Locale.GetText (
2175 "Value is greater than UInt32.MaxValue or less than UInt32.MinValue"));
2177 // Returned Even-Rounded
2178 return (uint)(Math.Round (value));
2181 [CLSCompliant (false)]
2182 public static uint ToUInt32 (double value)
2184 if (value > UInt32.MaxValue || value < UInt32.MinValue)
2185 throw new OverflowException (Locale.GetText (
2186 "Value is greater than UInt32.MaxValue or less than UInt32.MinValue"));
2188 // Returned Even-Rounded
2189 return (uint)(Math.Round (value));
2192 [CLSCompliant (false)]
2193 public static uint ToUInt32 (float value)
2195 if (value > UInt32.MaxValue || value < UInt32.MinValue)
2196 throw new OverflowException (Locale.GetText (
2197 "Value is greater than UInt32.MaxValue or less than UInt32.MinValue"));
2199 // Returned Even-Rounded, pass as double to Math
2200 return (uint)(Math.Round ( (double)value));
2203 [CLSCompliant (false)]
2204 public static uint ToUInt32 (int value)
2206 if (value < UInt32.MinValue)
2207 throw new OverflowException (Locale.GetText (
2208 "Value is less than UInt32.MinValue"));
2213 [CLSCompliant (false)]
2214 public static uint ToUInt32 (long value)
2216 if (value > UInt32.MaxValue || value < UInt32.MinValue)
2217 throw new OverflowException (Locale.GetText (
2218 "Value is greater than UInt32.MaxValue or less than UInt32.MinValue"));
2223 [CLSCompliant (false)]
2224 public static uint ToUInt32 (sbyte value)
2226 if (value < UInt32.MinValue)
2227 throw new OverflowException (Locale.GetText (
2228 "Value is less than UInt32.MinValue"));
2233 [CLSCompliant (false)]
2234 public static uint ToUInt32 (short value)
2236 if (value < UInt32.MinValue)
2237 throw new OverflowException (Locale.GetText (
2238 "Value is less than UInt32.MinValue"));
2243 [CLSCompliant (false)]
2244 public static uint ToUInt32 (string value)
2247 return 0; // LAMESPEC: Spec says throw ArgumentNullException
2248 return UInt32.Parse (value);
2251 [CLSCompliant (false)]
2252 public static uint ToUInt32 (string value, IFormatProvider provider)
2255 return 0; // LAMESPEC: Spec says throw ArgumentNullException
2256 return UInt32.Parse (value, provider);
2259 [CLSCompliant (false)]
2260 public static uint ToUInt32 (string value, int fromBase)
2262 return (uint) ConvertFromBase (value, fromBase, true);
2265 [CLSCompliant (false)]
2266 public static uint ToUInt32 (uint value)
2271 [CLSCompliant (false)]
2272 public static uint ToUInt32 (ulong value)
2274 if (value > UInt32.MaxValue)
2275 throw new OverflowException (Locale.GetText (
2276 "Value is greater than UInt32.MaxValue"));
2281 [CLSCompliant (false)]
2282 public static uint ToUInt32 (ushort value)
2287 [CLSCompliant (false)]
2288 public static uint ToUInt32 (object value)
2292 return ToUInt32 (value, null);
2295 [CLSCompliant (false)]
2296 public static uint ToUInt32 (object value, IFormatProvider provider)
2300 return ((IConvertible) value).ToUInt32 (provider);
2304 // ========== UInt64 Conversions ========== //
2306 [CLSCompliant (false)]
2307 public static ulong ToUInt64 (bool value)
2309 return (ulong)(value ? 1 : 0);
2312 [CLSCompliant (false)]
2313 public static ulong ToUInt64 (byte value)
2315 return (ulong)value;
2318 [CLSCompliant (false)]
2319 public static ulong ToUInt64 (char value)
2321 return (ulong)value;
2324 [CLSCompliant (false)]
2325 public static ulong ToUInt64 (DateTime value)
2327 throw new InvalidCastException ("The conversion is not supported.");
2330 [CLSCompliant (false)]
2331 public static ulong ToUInt64 (decimal value)
2333 if (value > UInt64.MaxValue || value < UInt64.MinValue)
2334 throw new OverflowException (Locale.GetText (
2335 "Value is greater than UInt64.MaxValue or less than UInt64.MinValue"));
2337 // Returned Even-Rounded
2338 return (ulong)(Math.Round (value));
2341 [CLSCompliant (false)]
2342 public static ulong ToUInt64 (double value)
2344 if (value > UInt64.MaxValue || value < UInt64.MinValue)
2345 throw new OverflowException (Locale.GetText (
2346 "Value is greater than UInt64.MaxValue or less than UInt64.MinValue"));
2348 // Returned Even-Rounded
2349 return (ulong)(Math.Round (value));
2352 [CLSCompliant (false)]
2353 public static ulong ToUInt64 (float value)
2355 if (value > UInt64.MaxValue || value < UInt64.MinValue)
2356 throw new OverflowException (Locale.GetText (
2357 "Value is greater than UInt64.MaxValue or less than UInt64.MinValue"));
2359 // Returned Even-Rounded, pass as a double to Math
2360 return (ulong)(Math.Round ( (double)value));
2363 [CLSCompliant (false)]
2364 public static ulong ToUInt64 (int value)
2366 if (value < (int)UInt64.MinValue)
2367 throw new OverflowException (Locale.GetText (
2368 "Value is less than UInt64.MinValue"));
2370 return (ulong)value;
2373 [CLSCompliant (false)]
2374 public static ulong ToUInt64 (long value)
2376 if (value < (long)UInt64.MinValue)
2377 throw new OverflowException (Locale.GetText (
2378 "Value is less than UInt64.MinValue"));
2380 return (ulong)value;
2383 [CLSCompliant (false)]
2384 public static ulong ToUInt64 (sbyte value)
2386 if (value < (sbyte)UInt64.MinValue)
2387 throw new OverflowException
2388 ("Value is less than UInt64.MinValue");
2390 return (ulong)value;
2393 [CLSCompliant (false)]
2394 public static ulong ToUInt64 (short value)
2396 if (value < (short)UInt64.MinValue)
2397 throw new OverflowException (Locale.GetText (
2398 "Value is less than UInt64.MinValue"));
2400 return (ulong)value;
2403 [CLSCompliant (false)]
2404 public static ulong ToUInt64 (string value)
2407 return 0; // LAMESPEC: Spec says throw ArgumentNullException
2408 return UInt64.Parse (value);
2411 [CLSCompliant (false)]
2412 public static ulong ToUInt64 (string value, IFormatProvider provider)
2415 return 0; // LAMESPEC: Spec says throw ArgumentNullException
2416 return UInt64.Parse (value, provider);
2419 [CLSCompliant (false)]
2420 public static ulong ToUInt64 (string value, int fromBase)
2422 return (ulong) ConvertFromBase64 (value, fromBase, true);
2425 [CLSCompliant (false)]
2426 public static ulong ToUInt64 (uint value)
2428 return (ulong)value;
2431 [CLSCompliant (false)]
2432 public static ulong ToUInt64 (ulong value)
2437 [CLSCompliant (false)]
2438 public static ulong ToUInt64 (ushort value)
2440 return (ulong)value;
2443 [CLSCompliant (false)]
2444 public static ulong ToUInt64 (object value)
2448 return ToUInt64 (value, null);
2451 [CLSCompliant (false)]
2452 public static ulong ToUInt64 (object value, IFormatProvider provider)
2456 return ((IConvertible) value).ToUInt64 (provider);
2460 // ========== Conversion / Helper Functions ========== //
2462 public static object ChangeType (object value, Type conversionType)
2464 if ((value != null) && (conversionType == null))
2465 throw new ArgumentNullException ("conversionType");
2466 CultureInfo ci = CultureInfo.CurrentCulture;
2467 IFormatProvider provider;
2468 if (conversionType == typeof(DateTime)) {
2469 provider = ci.DateTimeFormat;
2472 provider = ci.NumberFormat;
2474 return ToType (value, conversionType, provider);
2477 public static object ChangeType (object value, TypeCode typeCode)
2479 CultureInfo ci = CultureInfo.CurrentCulture;
2480 Type conversionType = conversionTable [(int) typeCode];
2481 IFormatProvider provider;
2482 if (conversionType == typeof(DateTime)) {
2483 provider = ci.DateTimeFormat;
2486 provider = ci.NumberFormat;
2488 return ToType (value, conversionType, provider);
2491 public static object ChangeType (object value, Type conversionType, IFormatProvider provider)
2493 if ((value != null) && (conversionType == null))
2494 throw new ArgumentNullException ("conversionType");
2495 return ToType (value, conversionType, provider);
2498 public static object ChangeType (object value, TypeCode typeCode, IFormatProvider provider)
2500 Type conversionType = conversionTable [(int)typeCode];
2501 return ToType (value, conversionType, provider);
2504 private static bool NotValidBase (int value)
2506 if ((value == 2) || (value == 8) ||
2507 (value == 10) || (value == 16))
2513 private static int ConvertFromBase (string value, int fromBase, bool unsigned)
2515 if (NotValidBase (fromBase))
2516 throw new ArgumentException ("fromBase is not valid.");
2525 int len = value.Length;
2526 bool negative = false;
2528 // special processing for some bases
2531 if (value.Substring (i, 1) == "-") {
2533 throw new OverflowException (
2534 Locale.GetText ("The string was being parsed as"
2535 + " an unsigned number and could not have a"
2536 + " negative sign."));
2543 if (value.Substring (i, 1) == "-") {
2544 throw new ArgumentException ("String cannot contain a "
2545 + "minus sign if the base is not 10.");
2549 if ((value[i] == '0') && ((value [i+1] == 'x') || (value [i+1] == 'X'))) {
2555 if (value.Substring (i, 1) == "-") {
2556 throw new ArgumentException ("String cannot contain a "
2557 + "minus sign if the base is not 10.");
2563 throw new FormatException ("Could not find any parsable digits.");
2566 if (value[i] == '+') {
2571 char c = value[i++];
2572 if (Char.IsNumber (c)) {
2573 digitValue = c - '0';
2574 } else if (Char.IsLetter (c)) {
2575 digitValue = Char.ToLowerInvariant (c) - 'a' + 10;
2578 throw new FormatException ("Additional unparsable "
2579 + "characters are at the end of the string.");
2581 throw new FormatException ("Could not find any parsable"
2586 if (digitValue >= fromBase) {
2588 throw new FormatException ("Additional unparsable "
2589 + "characters are at the end of the string.");
2591 throw new FormatException ("Could not find any parsable"
2596 result = (fromBase) * result + digitValue;
2601 throw new FormatException ("Could not find any parsable digits.");
2609 // note: this has nothing to do with base64 encoding (just base and Int64)
2610 private static long ConvertFromBase64 (string value, int fromBase, bool unsigned)
2612 if (NotValidBase (fromBase))
2613 throw new ArgumentException ("fromBase is not valid.");
2618 int digitValue = -1;
2620 bool negative = false;
2623 int len = value.Length;
2625 // special processing for some bases
2628 if (value.Substring(i, 1) == "-") {
2630 throw new OverflowException (
2631 Locale.GetText ("The string was being parsed as"
2632 + " an unsigned number and could not have a"
2633 + " negative sign."));
2640 if (value.Substring (i, 1) == "-") {
2641 throw new ArgumentException ("String cannot contain a "
2642 + "minus sign if the base is not 10.");
2646 if ((value[i] == '0') && ((value[i + 1] == 'x') || (value[i + 1] == 'X'))) {
2652 if (value.Substring (i, 1) == "-") {
2653 throw new ArgumentException ("String cannot contain a "
2654 + "minus sign if the base is not 10.");
2660 throw new FormatException ("Could not find any parsable digits.");
2663 if (value[i] == '+') {
2668 char c = value[i++];
2669 if (Char.IsNumber (c)) {
2670 digitValue = c - '0';
2671 } else if (Char.IsLetter (c)) {
2672 digitValue = Char.ToLowerInvariant (c) - 'a' + 10;
2675 throw new FormatException ("Additional unparsable "
2676 + "characters are at the end of the string.");
2678 throw new FormatException ("Could not find any parsable"
2683 if (digitValue >= fromBase) {
2685 throw new FormatException ("Additional unparsable "
2686 + "characters are at the end of the string.");
2688 throw new FormatException ("Could not find any parsable"
2693 result = (fromBase * result + digitValue);
2698 throw new FormatException ("Could not find any parsable digits.");
2706 private static void EndianSwap (ref byte[] value)
2708 byte[] buf = new byte[value.Length];
2709 for (int i = 0; i < value.Length; i++)
2710 buf[i] = value[value.Length-1-i];
2714 private static string ConvertToBase2 (byte[] value)
2716 if (!BitConverter.IsLittleEndian)
2717 EndianSwap (ref value);
2718 StringBuilder sb = new StringBuilder ();
2719 for (int i = value.Length - 1; i >= 0; i--) {
2721 for (int j = 0; j < 8; j++) {
2722 if ((b & 0x80) == 0x80) {
2732 return sb.ToString ();
2735 private static string ConvertToBase8 (byte[] value)
2738 switch (value.Length) {
2740 l = (ulong) value [0];
2743 l = (ulong) BitConverter.ToUInt16 (value, 0);
2746 l = (ulong) BitConverter.ToUInt32 (value, 0);
2749 l = BitConverter.ToUInt64 (value, 0);
2752 throw new ArgumentException ("value");
2755 StringBuilder sb = new StringBuilder ();
2756 for (int i = 21; i >= 0; i--) {
2757 // 3 bits at the time
2758 char val = (char) ((l >> i * 3) & 0x7);
2759 if ((val != 0) || (sb.Length > 0)) {
2764 return sb.ToString ();
2767 private static string ConvertToBase16 (byte[] value)
2769 if (!BitConverter.IsLittleEndian)
2770 EndianSwap (ref value);
2771 StringBuilder sb = new StringBuilder ();
2772 for (int i = value.Length - 1; i >= 0; i--) {
2773 char high = (char)((value[i] >> 4) & 0x0f);
2774 if ((high != 0) || (sb.Length > 0)) {
2784 char low = (char)(value[i] & 0x0f);
2785 if ((low != 0) || (sb.Length > 0)) {
2795 return sb.ToString ();
2798 // Lookup table for the conversion ToType method. Order
2799 // is important! Used by ToType for comparing the target
2800 // type, and uses hardcoded array indexes.
2801 private static readonly Type[] conversionTable = {
2802 // Valid ICovnertible Types
2804 typeof (object), // 1 TypeCode.Object
2805 typeof (DBNull), // 2 TypeCode.DBNull
2806 typeof (Boolean), // 3 TypeCode.Boolean
2807 typeof (Char), // 4 TypeCode.Char
2808 typeof (SByte), // 5 TypeCode.SByte
2809 typeof (Byte), // 6 TypeCode.Byte
2810 typeof (Int16), // 7 TypeCode.Int16
2811 typeof (UInt16), // 8 TypeCode.UInt16
2812 typeof (Int32), // 9 TypeCode.Int32
2813 typeof (UInt32), // 10 TypeCode.UInt32
2814 typeof (Int64), // 11 TypeCode.Int64
2815 typeof (UInt64), // 12 TypeCode.UInt64
2816 typeof (Single), // 13 TypeCode.Single
2817 typeof (Double), // 14 TypeCode.Double
2818 typeof (Decimal), // 15 TypeCode.Decimal
2819 typeof (DateTime), // 16 TypeCode.DateTime
2821 typeof (String), // 18 TypeCode.String
2824 // Function to convert an object to another type and return
2825 // it as an object. In place for the core data types to use
2826 // when implementing IConvertible. Uses hardcoded indexes in
2827 // the conversionTypes array, so if modify carefully.
2828 internal static object ToType (object value, Type conversionType,
2829 IFormatProvider provider)
2831 if (value == null) {
2832 if ((conversionType != null) && conversionType.IsValueType){
2834 throw new InvalidCastException ("Null object can not be converted to a value type.");
2837 // Bug compatibility with 1.0
2839 throw new NullReferenceException ("Null object can not be converted to a value type.");
2845 if (conversionType == null)
2846 throw new InvalidCastException ("Cannot cast to destination type.");
2848 if (value.GetType () == conversionType)
2851 if (value is IConvertible) {
2852 IConvertible convertValue = (IConvertible) value;
2854 if (conversionType == conversionTable[0]) // 0 Empty
2855 throw new ArgumentNullException ();
2857 else if (conversionType == conversionTable[1]) // 1 TypeCode.Object
2858 return (object) value;
2860 else if (conversionType == conversionTable[2]) // 2 TypeCode.DBNull
2861 throw new InvalidCastException (
2862 "Cannot cast to DBNull, it's not IConvertible");
2864 else if (conversionType == conversionTable[3]) // 3 TypeCode.Boolean
2865 return (object) convertValue.ToBoolean (provider);
2867 else if (conversionType == conversionTable[4]) // 4 TypeCode.Char
2868 return (object) convertValue.ToChar (provider);
2870 else if (conversionType == conversionTable[5]) // 5 TypeCode.SByte
2871 return (object) convertValue.ToSByte (provider);
2873 else if (conversionType == conversionTable[6]) // 6 TypeCode.Byte
2874 return (object) convertValue.ToByte (provider);
2876 else if (conversionType == conversionTable[7]) // 7 TypeCode.Int16
2877 return (object) convertValue.ToInt16 (provider);
2879 else if (conversionType == conversionTable[8]) // 8 TypeCode.UInt16
2880 return (object) convertValue.ToUInt16 (provider);
2882 else if (conversionType == conversionTable[9]) // 9 TypeCode.Int32
2883 return (object) convertValue.ToInt32 (provider);
2885 else if (conversionType == conversionTable[10]) // 10 TypeCode.UInt32
2886 return (object) convertValue.ToUInt32 (provider);
2888 else if (conversionType == conversionTable[11]) // 11 TypeCode.Int64
2889 return (object) convertValue.ToInt64 (provider);
2891 else if (conversionType == conversionTable[12]) // 12 TypeCode.UInt64
2892 return (object) convertValue.ToUInt64 (provider);
2894 else if (conversionType == conversionTable[13]) // 13 TypeCode.Single
2895 return (object) convertValue.ToSingle (provider);
2897 else if (conversionType == conversionTable[14]) // 14 TypeCode.Double
2898 return (object) convertValue.ToDouble (provider);
2900 else if (conversionType == conversionTable[15]) // 15 TypeCode.Decimal
2901 return (object) convertValue.ToDecimal (provider);
2903 else if (conversionType == conversionTable[16]) // 16 TypeCode.DateTime
2904 return (object) convertValue.ToDateTime (provider);
2906 else if (conversionType == conversionTable[18]) // 18 TypeCode.String
2907 return (object) convertValue.ToString (provider);
2909 return convertValue.ToType (conversionType, provider);
2912 // Not in the conversion table
2913 throw new InvalidCastException ((Locale.GetText (
2914 "Value is not a convertible object: " + value.GetType().ToString() + " to " + conversionType.FullName)));