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)
684 if (value > Char.MaxValue)
685 throw new OverflowException (Locale.GetText (
686 "Value is greater than Char.MaxValue"));
691 public static char ToChar (object value)
695 return ToChar (value, null);
698 public static char ToChar (object value, IFormatProvider provider)
702 return ((IConvertible) value).ToChar (provider);
705 // ========== DateTime Conversions ========== //
707 public static DateTime ToDateTime (string value)
710 return DateTime.MinValue; // LAMESPEC: Spec says throw ArgumentNullException
711 return DateTime.Parse (value);
714 public static DateTime ToDateTime (string value, IFormatProvider provider)
717 return DateTime.MinValue; // LAMESPEC: Spec says throw ArgumentNullException
718 return DateTime.Parse (value, provider);
721 public static DateTime ToDateTime (bool value)
723 throw new InvalidCastException ("This conversion is not supported.");
726 public static DateTime ToDateTime (byte value)
728 throw new InvalidCastException ("This conversion is not supported.");
731 public static DateTime ToDateTime (char value)
733 throw new InvalidCastException ("This conversion is not supported.");
736 public static DateTime ToDateTime (DateTime value)
741 public static DateTime ToDateTime (decimal value)
743 throw new InvalidCastException ("This conversion is not supported.");
746 public static DateTime ToDateTime (double value)
748 throw new InvalidCastException ("This conversion is not supported.");
751 public static DateTime ToDateTime (short value)
753 throw new InvalidCastException ("This conversion is not supported.");
756 public static DateTime ToDateTime (int value)
758 throw new InvalidCastException ("This conversion is not supported.");
761 public static DateTime ToDateTime (long value)
763 throw new InvalidCastException ("This conversion is not supported.");
766 public static DateTime ToDateTime (float value)
768 throw new InvalidCastException ("This conversion is not supported.");
771 public static DateTime ToDateTime (object value)
774 return DateTime.MinValue;
775 return ToDateTime (value, null);
778 public static DateTime ToDateTime (object value, IFormatProvider provider)
781 return DateTime.MinValue;
782 return ((IConvertible) value).ToDateTime (provider);
785 [CLSCompliant (false)]
786 public static DateTime ToDateTime (sbyte value)
788 throw new InvalidCastException ("This conversion is not supported.");
790 [CLSCompliant (false)]
791 public static DateTime ToDateTime (ushort value)
793 throw new InvalidCastException ("This conversion is not supported.");
796 [CLSCompliant (false)]
797 public static DateTime ToDateTime (uint value)
799 throw new InvalidCastException ("This conversion is not supported.");
802 [CLSCompliant (false)]
803 public static DateTime ToDateTime (ulong value)
805 throw new InvalidCastException ("This conversion is not supported.");
808 // ========== Decimal Conversions ========== //
810 public static decimal ToDecimal (bool value)
812 return value ? 1 : 0;
815 public static decimal ToDecimal (byte value)
817 return (decimal)value;
820 public static decimal ToDecimal (char value)
822 throw new InvalidCastException ("This conversion is not supported.");
825 public static decimal ToDecimal (DateTime value)
827 throw new InvalidCastException ("This conversion is not supported.");
830 public static decimal ToDecimal (decimal value)
835 public static decimal ToDecimal (double value)
837 if (value > (double)Decimal.MaxValue || value < (double)Decimal.MinValue)
838 throw new OverflowException (Locale.GetText (
839 "Value is greater than Decimal.MaxValue or less than Decimal.MinValue"));
841 return (decimal)value;
844 public static decimal ToDecimal (float value)
846 return (decimal) value;
849 public static decimal ToDecimal (int value)
851 return (decimal)value;
854 public static decimal ToDecimal (long value)
856 return (decimal)value;
859 [CLSCompliant (false)]
860 public static decimal ToDecimal (sbyte value)
862 return (decimal)value;
865 public static decimal ToDecimal (short value)
867 return (decimal)value;
870 public static decimal ToDecimal (string value)
873 return new Decimal (0); // LAMESPEC: Spec says throw ArgumentNullException
874 return Decimal.Parse (value);
877 public static decimal ToDecimal (string value, IFormatProvider provider)
880 return new Decimal (0); // LAMESPEC: Spec says throw ArgumentNullException
881 return Decimal.Parse (value, provider);
884 [CLSCompliant (false)]
885 public static decimal ToDecimal (uint value)
887 return (decimal)value;
890 [CLSCompliant (false)]
891 public static decimal ToDecimal (ulong value)
893 return (decimal)value;
896 [CLSCompliant (false)]
897 public static decimal ToDecimal (ushort value)
899 return (decimal)value;
902 public static decimal ToDecimal (object value)
905 return new Decimal (0);
906 return ToDecimal (value, null);
909 public static decimal ToDecimal (object value, IFormatProvider provider)
912 return new Decimal (0);
913 return ((IConvertible) value).ToDecimal (provider);
917 // ========== Double Conversions ========== //
919 public static double ToDouble (bool value)
921 return value ? 1 : 0;
924 public static double ToDouble (byte value)
926 return (double) value;
929 public static double ToDouble (char value)
931 throw new InvalidCastException ("This conversion is not supported.");
934 public static double ToDouble (DateTime value)
936 throw new InvalidCastException ("This conversion is not supported.");
939 public static double ToDouble (decimal value)
941 return (double)value;
944 public static double ToDouble (double value)
949 public static double ToDouble (float value)
951 return (double) value;
954 public static double ToDouble (int value)
956 return (double)value;
959 public static double ToDouble (long value)
961 return (double)value;
964 [CLSCompliant (false)]
965 public static double ToDouble (sbyte value)
967 return (double)value;
970 public static double ToDouble (short value)
972 return (double)value;
975 public static double ToDouble (string value)
978 return 0.0; // LAMESPEC: Spec says throw ArgumentNullException
979 return Double.Parse (value);
982 public static double ToDouble (string value, IFormatProvider provider)
985 return 0.0; // LAMESPEC: Spec says throw ArgumentNullException
986 return Double.Parse (value, provider);
989 [CLSCompliant (false)]
990 public static double ToDouble (uint value)
992 return (double)value;
995 [CLSCompliant (false)]
996 public static double ToDouble (ulong value)
998 return (double)value;
1001 [CLSCompliant (false)]
1002 public static double ToDouble (ushort value)
1004 return (double)value;
1007 public static double ToDouble (object value)
1011 return ToDouble (value, null);
1014 public static double ToDouble (object value, IFormatProvider provider)
1018 return ((IConvertible) value).ToDouble (provider);
1021 // ========== Int16 Conversions ========== //
1023 public static short ToInt16 (bool value)
1025 return (short)(value ? 1 : 0);
1028 public static short ToInt16 (byte value)
1030 return (short)value;
1033 public static short ToInt16 (char value)
1035 if (value > Int16.MaxValue)
1036 throw new OverflowException (Locale.GetText (
1037 "Value is greater than Int16.MaxValue"));
1039 return (short)value;
1042 public static short ToInt16 (DateTime value)
1044 throw new InvalidCastException ("This conversion is not supported.");
1047 public static short ToInt16 (decimal value)
1049 if (value > Int16.MaxValue || value < Int16.MinValue)
1050 throw new OverflowException (Locale.GetText (
1051 "Value is greater than Int16.MaxValue or less than Int16.MinValue"));
1053 // Returned Even-Rounded
1054 return (short)(Math.Round (value));
1057 public static short ToInt16 (double value)
1059 if (value > Int16.MaxValue || value < Int16.MinValue)
1060 throw new OverflowException (Locale.GetText (
1061 "Value is greater than Int16.MaxValue or less than Int16.MinValue"));
1063 // Returned Even-Rounded
1064 return (short)(Math.Round (value));
1067 public static short ToInt16 (float value)
1069 if (value > Int16.MaxValue || value < Int16.MinValue)
1070 throw new OverflowException (Locale.GetText (
1071 "Value is greater than Int16.MaxValue or less than Int16.MinValue"));
1073 // Returned Even-Rounded, use Math.Round pass as a double.
1074 return (short)Math.Round ( (double)value);
1077 public static short ToInt16 (int 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 public static short ToInt16 (long value)
1088 if (value > Int16.MaxValue || value < Int16.MinValue)
1089 throw new OverflowException (Locale.GetText (
1090 "Value is greater than Int16.MaxValue or less than Int16.MinValue"));
1092 return (short)value;
1095 [CLSCompliant (false)]
1096 public static short ToInt16 (sbyte value)
1098 return (short)value;
1101 public static short ToInt16 (short value)
1106 public static short ToInt16 (string value)
1109 return 0; // LAMESPEC: Spec says throw ArgumentNullException
1110 return Int16.Parse (value);
1113 public static short ToInt16 (string value, IFormatProvider provider)
1116 return 0; // LAMESPEC: Spec says throw ArgumentNullException
1117 return Int16.Parse (value, provider);
1120 public static short ToInt16 (string value, int fromBase)
1122 int result = ConvertFromBase (value, fromBase, false);
1123 if (fromBase != 10) {
1124 if (result > ushort.MaxValue) {
1125 throw new OverflowException ("Value was either too large or too small for an Int16.");
1128 // note: no sign are available to detect negatives
1129 if (result > Int16.MaxValue) {
1130 // return negative 2's complement
1131 return Convert.ToInt16 (-(65536 - result));
1134 return Convert.ToInt16 (result);
1137 [CLSCompliant (false)]
1138 public static short ToInt16 (uint value)
1140 if (value > Int16.MaxValue)
1141 throw new OverflowException (Locale.GetText (
1142 "Value is greater than Int16.MaxValue"));
1144 return (short)value;
1147 [CLSCompliant (false)]
1148 public static short ToInt16 (ulong value)
1150 if (value > (ulong)Int16.MaxValue)
1151 throw new OverflowException (Locale.GetText (
1152 "Value is greater than Int16.MaxValue"));
1153 return (short)value;
1156 [CLSCompliant (false)]
1157 public static short ToInt16 (ushort value)
1159 if (value > Int16.MaxValue)
1160 throw new OverflowException (Locale.GetText (
1161 "Value is greater than Int16.MaxValue"));
1163 return (short)value;
1166 public static short ToInt16 (object value)
1170 return ToInt16 (value, null);
1173 public static short ToInt16 (object value, IFormatProvider provider)
1177 return ((IConvertible) value).ToInt16 (provider);
1180 // ========== Int32 Conversions ========== //
1182 public static int ToInt32 (bool value)
1184 return value ? 1 : 0;
1187 public static int ToInt32 (byte value)
1192 public static int ToInt32 (char value)
1197 public static int ToInt32 (DateTime value)
1199 throw new InvalidCastException ("This conversion is not supported.");
1202 public static int ToInt32 (decimal value)
1204 if (value > Int32.MaxValue || value < Int32.MinValue)
1205 throw new OverflowException (Locale.GetText (
1206 "Value is greater than Int32.MaxValue or less than Int32.MinValue"));
1208 // Returned Even-Rounded
1209 return (int)(Math.Round (value));
1212 public static int ToInt32 (double value)
1214 if (value > Int32.MaxValue || value < Int32.MinValue)
1215 throw new OverflowException (Locale.GetText (
1216 "Value is greater than Int32.MaxValue or less than Int32.MinValue"));
1218 // Returned Even-Rounded
1219 return (int)(Math.Round (value));
1222 public static int ToInt32 (float value)
1224 if (value > Int32.MaxValue || value < Int32.MinValue)
1225 throw new OverflowException (Locale.GetText (
1226 "Value is greater than Int32.MaxValue or less than Int32.MinValue"));
1228 // Returned Even-Rounded, pass as a double, could just call
1229 // Convert.ToInt32 ( (double)value);
1230 return (int)(Math.Round ( (double)value));
1233 public static int ToInt32 (int value)
1238 public static int ToInt32 (long value)
1240 if (value > Int32.MaxValue || value < Int32.MinValue)
1241 throw new OverflowException (Locale.GetText (
1242 "Value is greater than Int32.MaxValue or less than Int32.MinValue"));
1247 [CLSCompliant (false)]
1248 public static int ToInt32 (sbyte value)
1253 public static int ToInt32 (short value)
1258 public static int ToInt32 (string value)
1261 return 0; // LAMESPEC: Spec says throw ArgumentNullException
1262 return Int32.Parse (value);
1265 public static int ToInt32 (string value, IFormatProvider provider)
1268 return 0; // LAMESPEC: Spec says throw ArgumentNullException
1269 return Int32.Parse (value, provider);
1273 public static int ToInt32 (string value, int fromBase)
1275 return ConvertFromBase (value, fromBase, false);
1278 [CLSCompliant (false)]
1279 public static int ToInt32 (uint value)
1281 if (value > Int32.MaxValue)
1282 throw new OverflowException (Locale.GetText (
1283 "Value is greater than Int32.MaxValue"));
1288 [CLSCompliant (false)]
1289 public static int ToInt32 (ulong value)
1291 if (value > Int32.MaxValue)
1292 throw new OverflowException (Locale.GetText (
1293 "Value is greater than Int32.MaxValue"));
1298 [CLSCompliant (false)]
1299 public static int ToInt32 (ushort value)
1304 public static int ToInt32 (object value)
1308 return ToInt32 (value, null);
1311 public static int ToInt32 (object value, IFormatProvider provider)
1315 return ((IConvertible) value).ToInt32 (provider);
1318 // ========== Int64 Conversions ========== //
1320 public static long ToInt64 (bool value)
1322 return value ? 1 : 0;
1325 public static long ToInt64 (byte value)
1327 return (long)(ulong)value;
1330 public static long ToInt64 (char value)
1335 public static long ToInt64 (DateTime value)
1337 throw new InvalidCastException ("This conversion is not supported.");
1340 public static long ToInt64 (decimal value)
1342 if (value > Int64.MaxValue || value < Int64.MinValue)
1343 throw new OverflowException (Locale.GetText (
1344 "Value is greater than Int64.MaxValue or less than Int64.MinValue"));
1346 // Returned Even-Rounded
1347 return (long)(Math.Round (value));
1350 public static long ToInt64 (double value)
1352 if (value > Int64.MaxValue || value < Int64.MinValue)
1353 throw new OverflowException (Locale.GetText (
1354 "Value is greater than Int64.MaxValue or less than Int64.MinValue"));
1356 // Returned Even-Rounded
1357 return (long)(Math.Round (value));
1360 public static long ToInt64 (float value)
1362 if (value > Int64.MaxValue || value < Int64.MinValue)
1363 throw new OverflowException (Locale.GetText (
1364 "Value is greater than Int64.MaxValue or less than Int64.MinValue"));
1366 // Returned Even-Rounded, pass to Math as a double, could
1367 // just call Convert.ToInt64 ( (double)value);
1368 return (long)(Math.Round ( (double)value));
1371 public static long ToInt64 (int value)
1376 public static long ToInt64 (long value)
1381 [CLSCompliant (false)]
1382 public static long ToInt64 (sbyte value)
1387 public static long ToInt64 (short value)
1392 public static long ToInt64 (string value)
1395 return 0; // LAMESPEC: Spec says throw ArgumentNullException
1396 return Int64.Parse (value);
1399 public static long ToInt64 (string value, IFormatProvider provider)
1402 return 0; // LAMESPEC: Spec says throw ArgumentNullException
1403 return Int64.Parse (value, provider);
1406 public static long ToInt64 (string value, int fromBase)
1408 return ConvertFromBase64 (value, fromBase, false);
1411 [CLSCompliant (false)]
1412 public static long ToInt64 (uint value)
1414 return (long)(ulong)value;
1417 [CLSCompliant (false)]
1418 public static long ToInt64 (ulong value)
1420 if (value > Int64.MaxValue)
1421 throw new OverflowException (Locale.GetText (
1422 "Value is greater than Int64.MaxValue"));
1427 [CLSCompliant (false)]
1428 public static long ToInt64 (ushort value)
1430 return (long)(ulong)value;
1433 public static long ToInt64 (object value)
1437 return ToInt64 (value, null);
1440 public static long ToInt64 (object value, IFormatProvider provider)
1444 return ((IConvertible) value).ToInt64 (provider);
1447 // ========== SByte Conversions ========== //
1449 [CLSCompliant (false)]
1450 public static sbyte ToSByte (bool value)
1452 return (sbyte)(value ? 1 : 0);
1455 [CLSCompliant (false)]
1456 public static sbyte ToSByte (byte value)
1458 if (value > SByte.MaxValue)
1459 throw new OverflowException (Locale.GetText (
1460 "Value is greater than SByte.MaxValue"));
1462 return (sbyte)value;
1465 [CLSCompliant (false)]
1466 public static sbyte ToSByte (char value)
1468 if (value > SByte.MaxValue)
1469 throw new OverflowException (Locale.GetText (
1470 "Value is greater than SByte.MaxValue"));
1472 return (sbyte)value;
1475 [CLSCompliant (false)]
1476 public static sbyte ToSByte (DateTime value)
1478 throw new InvalidCastException ("This conversion is not supported.");
1481 [CLSCompliant (false)]
1482 public static sbyte ToSByte (decimal value)
1484 if (value > SByte.MaxValue || value < SByte.MinValue)
1485 throw new OverflowException (Locale.GetText (
1486 "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
1488 // Returned Even-Rounded
1489 return (sbyte)(Math.Round (value));
1492 [CLSCompliant (false)]
1493 public static sbyte ToSByte (double value)
1495 if (value > SByte.MaxValue || value < SByte.MinValue)
1496 throw new OverflowException (Locale.GetText (
1497 "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
1499 // Returned Even-Rounded
1500 return (sbyte)(Math.Round (value));
1503 [CLSCompliant (false)]
1504 public static sbyte ToSByte (float value)
1506 if (value > SByte.MaxValue || value < SByte.MinValue)
1507 throw new OverflowException (Locale.GetText (
1508 "Value is greater than SByte.MaxValue or less than SByte.Minalue"));
1510 // Returned Even-Rounded, pass as double to Math
1511 return (sbyte)(Math.Round ( (double)value));
1514 [CLSCompliant (false)]
1515 public static sbyte ToSByte (int value)
1517 if (value > SByte.MaxValue || value < SByte.MinValue)
1518 throw new OverflowException (Locale.GetText (
1519 "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
1521 return (sbyte)value;
1524 [CLSCompliant (false)]
1525 public static sbyte ToSByte (long value)
1527 if (value > SByte.MaxValue || value < SByte.MinValue)
1528 throw new OverflowException (Locale.GetText (
1529 "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
1531 return (sbyte)value;
1534 [CLSCompliant (false)]
1535 public static sbyte ToSByte (sbyte value)
1540 [CLSCompliant (false)]
1541 public static sbyte ToSByte (short value)
1543 if (value > SByte.MaxValue || value < SByte.MinValue)
1544 throw new OverflowException (Locale.GetText (
1545 "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
1547 return (sbyte)value;
1550 [CLSCompliant (false)]
1551 public static sbyte ToSByte (string value)
1554 return 0; // LAMESPEC: Spec says throw ArgumentNullException
1555 return SByte.Parse (value);
1558 [CLSCompliant (false)]
1559 public static sbyte ToSByte (string value, IFormatProvider provider)
1562 throw new ArgumentNullException ("value");
1563 return SByte.Parse (value, provider);
1566 [CLSCompliant (false)]
1567 public static sbyte ToSByte (string value, int fromBase)
1569 int result = ConvertFromBase (value, fromBase, false);
1570 if (fromBase != 10) {
1571 // note: no sign are available to detect negatives
1572 if (result > SByte.MaxValue) {
1573 // return negative 2's complement
1574 return Convert.ToSByte (-(256 - result));
1577 return Convert.ToSByte (result);
1580 [CLSCompliant (false)]
1581 public static sbyte ToSByte (uint value)
1583 if (value > SByte.MaxValue)
1584 throw new OverflowException (Locale.GetText (
1585 "Value is greater than SByte.MaxValue"));
1587 return (sbyte)value;
1590 [CLSCompliant (false)]
1591 public static sbyte ToSByte (ulong value)
1593 if (value > (ulong)SByte.MaxValue)
1594 throw new OverflowException (Locale.GetText (
1595 "Value is greater than SByte.MaxValue"));
1597 return (sbyte)value;
1600 [CLSCompliant (false)]
1601 public static sbyte ToSByte (ushort value)
1603 if (value > SByte.MaxValue)
1604 throw new OverflowException (Locale.GetText (
1605 "Value is greater than SByte.MaxValue"));
1607 return (sbyte)value;
1610 [CLSCompliant (false)]
1611 public static sbyte ToSByte (object value)
1615 return ToSByte (value, null);
1618 [CLSCompliant (false)]
1619 public static sbyte ToSByte (object value, IFormatProvider provider)
1623 return ((IConvertible) value).ToSByte (provider);
1626 // ========== Single Conversions ========== //
1628 public static float ToSingle (bool value)
1630 return value ? 1 : 0;
1633 public static float ToSingle (byte value)
1635 return (float)value;
1638 public static float ToSingle (Char value)
1640 throw new InvalidCastException ("This conversion is not supported.");
1643 public static float ToSingle (DateTime value)
1645 throw new InvalidCastException ("This conversion is not supported.");
1648 public static float ToSingle (decimal value)
1650 return (float)value;
1653 public static float ToSingle (double value)
1655 return (float)value;
1658 public static float ToSingle (float value)
1663 public static float ToSingle (int value)
1665 return (float)value;
1668 public static float ToSingle (long value)
1670 return (float)value;
1673 [CLSCompliant (false)]
1674 public static float ToSingle (sbyte value)
1676 return (float)value;
1679 public static float ToSingle (short value)
1681 return (float)value;
1684 public static float ToSingle (string value)
1687 return 0.0f; // LAMESPEC: Spec says throw ArgumentNullException
1688 return Single.Parse (value);
1691 public static float ToSingle (string value, IFormatProvider provider)
1694 return 0.0f; // LAMESPEC: Spec says throw ArgumentNullException
1695 return Single.Parse (value, provider);
1698 [CLSCompliant (false)]
1699 public static float ToSingle (uint value)
1701 return (float)value;
1704 [CLSCompliant (false)]
1705 public static float ToSingle (ulong value)
1707 return (float)value;
1710 [CLSCompliant (false)]
1711 public static float ToSingle (ushort value)
1713 return (float)value;
1716 public static float ToSingle (object value)
1720 return ToSingle (value, null);
1723 // [CLSCompliant (false)]
1724 public static float ToSingle (object value, IFormatProvider provider)
1728 return ((IConvertible) value).ToSingle (provider);
1731 // ========== String Conversions ========== //
1733 public static string ToString (bool value)
1735 return value.ToString ();
1738 public static string ToString (bool value, IFormatProvider provider)
1740 return value.ToString (); // the same as ToString (bool).
1743 public static string ToString (byte value)
1745 return value.ToString ();
1748 public static string ToString (byte value, IFormatProvider provider)
1750 return value.ToString (provider);
1753 public static string ToString (byte value, int toBase)
1758 return value.ToString ();
1760 byte[] val = BitConverter.GetBytes (value);
1764 return ConvertToBase2 (val);
1766 return ConvertToBase8 (val);
1768 return ConvertToBase16 (val);
1770 throw new ArgumentException (Locale.GetText ("toBase is not valid."));
1774 public static string ToString (char value)
1776 return value.ToString ();
1779 public static string ToString (char value, IFormatProvider provider)
1781 return value.ToString (); // the same as ToString (char)
1784 public static string ToString (DateTime value)
1786 return value.ToString ();
1789 public static string ToString (DateTime value, IFormatProvider provider)
1791 return value.ToString (provider);
1794 public static string ToString (decimal value)
1796 return value.ToString ();
1799 public static string ToString (decimal value, IFormatProvider provider)
1801 return value.ToString (provider);
1804 public static string ToString (double value)
1806 return value.ToString ();
1809 public static string ToString (double value, IFormatProvider provider)
1811 return value.ToString (provider);
1814 public static string ToString (float value)
1816 return value.ToString ();
1819 public static string ToString (float value, IFormatProvider provider)
1821 return value.ToString (provider);
1824 public static string ToString (int value)
1826 return value.ToString ();
1829 public static string ToString (int value, int toBase)
1834 return value.ToString ();
1836 byte[] val = BitConverter.GetBytes (value);
1840 return ConvertToBase2 (val);
1842 return ConvertToBase8 (val);
1844 return ConvertToBase16 (val);
1846 throw new ArgumentException (Locale.GetText ("toBase is not valid."));
1850 public static string ToString (int value, IFormatProvider provider)
1852 return value.ToString (provider);
1855 public static string ToString (long value)
1857 return value.ToString ();
1860 public static string ToString (long value, int toBase)
1865 return value.ToString ();
1867 byte[] val = BitConverter.GetBytes (value);
1871 return ConvertToBase2 (val);
1873 return ConvertToBase8 (val);
1875 return ConvertToBase16 (val);
1877 throw new ArgumentException (Locale.GetText ("toBase is not valid."));
1881 public static string ToString (long value, IFormatProvider provider)
1883 return value.ToString (provider);
1886 public static string ToString (object value)
1888 return ToString (value, null);
1891 public static string ToString (object value, IFormatProvider provider)
1893 if (value is IConvertible)
1894 return ((IConvertible) value).ToString (provider);
1895 else if (value != null)
1896 return value.ToString ();
1897 return String.Empty;
1900 [CLSCompliant (false)]
1901 public static string ToString (sbyte value)
1903 return value.ToString ();
1906 [CLSCompliant (false)]
1907 public static string ToString (sbyte value, IFormatProvider provider)
1909 return value.ToString (provider);
1912 public static string ToString (short value)
1914 return value.ToString ();
1917 public static string ToString (short value, int toBase)
1922 return value.ToString ();
1924 byte[] val = BitConverter.GetBytes (value);
1928 return ConvertToBase2 (val);
1930 return ConvertToBase8 (val);
1932 return ConvertToBase16 (val);
1934 throw new ArgumentException (Locale.GetText ("toBase is not valid."));
1938 public static string ToString (short value, IFormatProvider provider)
1940 return value.ToString (provider);
1943 public static string ToString (string value)
1948 public static string ToString (string value, IFormatProvider provider)
1950 return value; // provider is ignored.
1953 [CLSCompliant (false)]
1954 public static string ToString (uint value)
1956 return value.ToString ();
1959 [CLSCompliant (false)]
1960 public static string ToString (uint value, IFormatProvider provider)
1962 return value.ToString (provider);
1965 [CLSCompliant (false)]
1966 public static string ToString (ulong value)
1968 return value.ToString ();
1971 [CLSCompliant (false)]
1972 public static string ToString (ulong value, IFormatProvider provider)
1974 return value.ToString (provider);
1977 [CLSCompliant (false)]
1978 public static string ToString (ushort value)
1980 return value.ToString ();
1983 [CLSCompliant (false)]
1984 public static string ToString (ushort value, IFormatProvider provider)
1986 return value.ToString (provider);
1989 // ========== UInt16 Conversions ========== //
1991 [CLSCompliant (false)]
1992 public static ushort ToUInt16 (bool value)
1994 return (ushort)(value ? 1 : 0);
1997 [CLSCompliant (false)]
1998 public static ushort ToUInt16 (byte value)
2000 return (ushort)value;
2003 [CLSCompliant (false)]
2004 public static ushort ToUInt16 (char value)
2006 return (ushort)value;
2009 [CLSCompliant (false)]
2010 public static ushort ToUInt16 (DateTime value)
2012 throw new InvalidCastException ("This conversion is not supported.");
2015 [CLSCompliant (false)]
2016 public static ushort ToUInt16 (decimal value)
2018 if (value > UInt16.MaxValue || value < UInt16.MinValue)
2019 throw new OverflowException (Locale.GetText (
2020 "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
2022 // Returned Even-Rounded
2023 return (ushort)(Math.Round (value));
2026 [CLSCompliant (false)]
2027 public static ushort ToUInt16 (double value)
2029 if (value > UInt16.MaxValue || value < UInt16.MinValue)
2030 throw new OverflowException (Locale.GetText (
2031 "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
2033 // Returned Even-Rounded
2034 return (ushort)(Math.Round (value));
2037 [CLSCompliant (false)]
2038 public static ushort ToUInt16 (float value)
2040 if (value > UInt16.MaxValue || value < UInt16.MinValue)
2041 throw new OverflowException (Locale.GetText (
2042 "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
2044 // Returned Even-Rounded, pass as double to Math
2045 return (ushort)(Math.Round ( (double)value));
2048 [CLSCompliant (false)]
2049 public static ushort ToUInt16 (int value)
2051 if (value > UInt16.MaxValue || value < UInt16.MinValue)
2052 throw new OverflowException (Locale.GetText (
2053 "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
2055 return (ushort)value;
2058 [CLSCompliant (false)]
2059 public static ushort ToUInt16 (long value)
2061 if (value > UInt16.MaxValue || value < UInt16.MinValue)
2062 throw new OverflowException (Locale.GetText (
2063 "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
2065 return (ushort)value;
2068 [CLSCompliant (false)]
2069 public static ushort ToUInt16 (sbyte value)
2071 if (value < UInt16.MinValue)
2072 throw new OverflowException (Locale.GetText (
2073 "Value is less than UInt16.MinValue"));
2075 return (ushort)value;
2078 [CLSCompliant (false)]
2079 public static ushort ToUInt16 (short value)
2081 if (value < UInt16.MinValue)
2082 throw new OverflowException (Locale.GetText (
2083 "Value is less than UInt16.MinValue"));
2085 return (ushort)value;
2088 [CLSCompliant (false)]
2089 public static ushort ToUInt16 (string value)
2092 return 0; // LAMESPEC: Spec says throw ArgumentNullException
2093 return UInt16.Parse (value);
2096 [CLSCompliant (false)]
2097 public static ushort ToUInt16 (string value, IFormatProvider provider)
2100 return 0; // LAMESPEC: Spec says throw ArgumentNullException
2101 return UInt16.Parse (value, provider);
2104 [CLSCompliant (false)]
2105 public static ushort ToUInt16 (string value, int fromBase)
2107 return ToUInt16 (ConvertFromBase (value, fromBase, true));
2110 [CLSCompliant (false)]
2111 public static ushort ToUInt16 (uint value)
2113 if (value > UInt16.MaxValue)
2114 throw new OverflowException (Locale.GetText (
2115 "Value is greater than UInt16.MaxValue"));
2117 return (ushort)value;
2120 [CLSCompliant (false)]
2121 public static ushort ToUInt16 (ulong value)
2123 if (value > (ulong)UInt16.MaxValue)
2124 throw new OverflowException (Locale.GetText (
2125 "Value is greater than UInt16.MaxValue"));
2127 return (ushort)value;
2130 [CLSCompliant (false)]
2131 public static ushort ToUInt16 (ushort value)
2136 [CLSCompliant (false)]
2137 public static ushort ToUInt16 (object value)
2141 return ToUInt16 (value, null);
2144 [CLSCompliant (false)]
2145 public static ushort ToUInt16 (object value, IFormatProvider provider)
2149 return ((IConvertible) value).ToUInt16 (provider);
2152 // ========== UInt32 Conversions ========== //
2154 [CLSCompliant (false)]
2155 public static uint ToUInt32 (bool value)
2157 return (uint)(value ? 1 : 0);
2160 [CLSCompliant (false)]
2161 public static uint ToUInt32 (byte value)
2166 [CLSCompliant (false)]
2167 public static uint ToUInt32 (char value)
2172 [CLSCompliant (false)]
2173 public static uint ToUInt32 (DateTime value)
2175 throw new InvalidCastException ("This conversion is not supported.");
2178 [CLSCompliant (false)]
2179 public static uint ToUInt32 (decimal value)
2181 if (value > UInt32.MaxValue || value < UInt32.MinValue)
2182 throw new OverflowException (Locale.GetText (
2183 "Value is greater than UInt32.MaxValue or less than UInt32.MinValue"));
2185 // Returned Even-Rounded
2186 return (uint)(Math.Round (value));
2189 [CLSCompliant (false)]
2190 public static uint ToUInt32 (double value)
2192 if (value > UInt32.MaxValue || value < UInt32.MinValue)
2193 throw new OverflowException (Locale.GetText (
2194 "Value is greater than UInt32.MaxValue or less than UInt32.MinValue"));
2196 // Returned Even-Rounded
2197 return (uint)(Math.Round (value));
2200 [CLSCompliant (false)]
2201 public static uint ToUInt32 (float value)
2203 if (value > UInt32.MaxValue || value < UInt32.MinValue)
2204 throw new OverflowException (Locale.GetText (
2205 "Value is greater than UInt32.MaxValue or less than UInt32.MinValue"));
2207 // Returned Even-Rounded, pass as double to Math
2208 return (uint)(Math.Round ( (double)value));
2211 [CLSCompliant (false)]
2212 public static uint ToUInt32 (int value)
2214 if (value < UInt32.MinValue)
2215 throw new OverflowException (Locale.GetText (
2216 "Value is less than UInt32.MinValue"));
2221 [CLSCompliant (false)]
2222 public static uint ToUInt32 (long value)
2224 if (value > UInt32.MaxValue || value < UInt32.MinValue)
2225 throw new OverflowException (Locale.GetText (
2226 "Value is greater than UInt32.MaxValue or less than UInt32.MinValue"));
2231 [CLSCompliant (false)]
2232 public static uint ToUInt32 (sbyte value)
2234 if (value < UInt32.MinValue)
2235 throw new OverflowException (Locale.GetText (
2236 "Value is less than UInt32.MinValue"));
2241 [CLSCompliant (false)]
2242 public static uint ToUInt32 (short value)
2244 if (value < UInt32.MinValue)
2245 throw new OverflowException (Locale.GetText (
2246 "Value is less than UInt32.MinValue"));
2251 [CLSCompliant (false)]
2252 public static uint ToUInt32 (string value)
2255 return 0; // LAMESPEC: Spec says throw ArgumentNullException
2256 return UInt32.Parse (value);
2259 [CLSCompliant (false)]
2260 public static uint ToUInt32 (string value, IFormatProvider provider)
2263 return 0; // LAMESPEC: Spec says throw ArgumentNullException
2264 return UInt32.Parse (value, provider);
2267 [CLSCompliant (false)]
2268 public static uint ToUInt32 (string value, int fromBase)
2270 return (uint) ConvertFromBase (value, fromBase, true);
2273 [CLSCompliant (false)]
2274 public static uint ToUInt32 (uint value)
2279 [CLSCompliant (false)]
2280 public static uint ToUInt32 (ulong value)
2282 if (value > UInt32.MaxValue)
2283 throw new OverflowException (Locale.GetText (
2284 "Value is greater than UInt32.MaxValue"));
2289 [CLSCompliant (false)]
2290 public static uint ToUInt32 (ushort value)
2295 [CLSCompliant (false)]
2296 public static uint ToUInt32 (object value)
2300 return ToUInt32 (value, null);
2303 [CLSCompliant (false)]
2304 public static uint ToUInt32 (object value, IFormatProvider provider)
2308 return ((IConvertible) value).ToUInt32 (provider);
2312 // ========== UInt64 Conversions ========== //
2314 [CLSCompliant (false)]
2315 public static ulong ToUInt64 (bool value)
2317 return (ulong)(value ? 1 : 0);
2320 [CLSCompliant (false)]
2321 public static ulong ToUInt64 (byte value)
2323 return (ulong)value;
2326 [CLSCompliant (false)]
2327 public static ulong ToUInt64 (char value)
2329 return (ulong)value;
2332 [CLSCompliant (false)]
2333 public static ulong ToUInt64 (DateTime value)
2335 throw new InvalidCastException ("The conversion is not supported.");
2338 [CLSCompliant (false)]
2339 public static ulong ToUInt64 (decimal value)
2341 if (value > UInt64.MaxValue || value < UInt64.MinValue)
2342 throw new OverflowException (Locale.GetText (
2343 "Value is greater than UInt64.MaxValue or less than UInt64.MinValue"));
2345 // Returned Even-Rounded
2346 return (ulong)(Math.Round (value));
2349 [CLSCompliant (false)]
2350 public static ulong ToUInt64 (double value)
2352 if (value > UInt64.MaxValue || value < UInt64.MinValue)
2353 throw new OverflowException (Locale.GetText (
2354 "Value is greater than UInt64.MaxValue or less than UInt64.MinValue"));
2356 // Returned Even-Rounded
2357 return (ulong)(Math.Round (value));
2360 [CLSCompliant (false)]
2361 public static ulong ToUInt64 (float value)
2363 if (value > UInt64.MaxValue || value < UInt64.MinValue)
2364 throw new OverflowException (Locale.GetText (
2365 "Value is greater than UInt64.MaxValue or less than UInt64.MinValue"));
2367 // Returned Even-Rounded, pass as a double to Math
2368 return (ulong)(Math.Round ( (double)value));
2371 [CLSCompliant (false)]
2372 public static ulong ToUInt64 (int value)
2374 if (value < (int)UInt64.MinValue)
2375 throw new OverflowException (Locale.GetText (
2376 "Value is less than UInt64.MinValue"));
2378 return (ulong)value;
2381 [CLSCompliant (false)]
2382 public static ulong ToUInt64 (long value)
2384 if (value < (long)UInt64.MinValue)
2385 throw new OverflowException (Locale.GetText (
2386 "Value is less than UInt64.MinValue"));
2388 return (ulong)value;
2391 [CLSCompliant (false)]
2392 public static ulong ToUInt64 (sbyte value)
2394 if (value < (sbyte)UInt64.MinValue)
2395 throw new OverflowException
2396 ("Value is less than UInt64.MinValue");
2398 return (ulong)value;
2401 [CLSCompliant (false)]
2402 public static ulong ToUInt64 (short value)
2404 if (value < (short)UInt64.MinValue)
2405 throw new OverflowException (Locale.GetText (
2406 "Value is less than UInt64.MinValue"));
2408 return (ulong)value;
2411 [CLSCompliant (false)]
2412 public static ulong ToUInt64 (string value)
2415 return 0; // LAMESPEC: Spec says throw ArgumentNullException
2416 return UInt64.Parse (value);
2419 [CLSCompliant (false)]
2420 public static ulong ToUInt64 (string value, IFormatProvider provider)
2423 return 0; // LAMESPEC: Spec says throw ArgumentNullException
2424 return UInt64.Parse (value, provider);
2427 [CLSCompliant (false)]
2428 public static ulong ToUInt64 (string value, int fromBase)
2430 return (ulong) ConvertFromBase64 (value, fromBase, true);
2433 [CLSCompliant (false)]
2434 public static ulong ToUInt64 (uint value)
2436 return (ulong)value;
2439 [CLSCompliant (false)]
2440 public static ulong ToUInt64 (ulong value)
2445 [CLSCompliant (false)]
2446 public static ulong ToUInt64 (ushort value)
2448 return (ulong)value;
2451 [CLSCompliant (false)]
2452 public static ulong ToUInt64 (object value)
2456 return ToUInt64 (value, null);
2459 [CLSCompliant (false)]
2460 public static ulong ToUInt64 (object value, IFormatProvider provider)
2464 return ((IConvertible) value).ToUInt64 (provider);
2468 // ========== Conversion / Helper Functions ========== //
2470 public static object ChangeType (object value, Type conversionType)
2472 if ((value != null) && (conversionType == null))
2473 throw new ArgumentNullException ("conversionType");
2474 CultureInfo ci = CultureInfo.CurrentCulture;
2475 IFormatProvider provider;
2476 if (conversionType == typeof(DateTime)) {
2477 provider = ci.DateTimeFormat;
2480 provider = ci.NumberFormat;
2482 return ToType (value, conversionType, provider);
2485 public static object ChangeType (object value, TypeCode typeCode)
2487 CultureInfo ci = CultureInfo.CurrentCulture;
2488 Type conversionType = conversionTable [(int) typeCode];
2489 IFormatProvider provider;
2490 if (conversionType == typeof(DateTime)) {
2491 provider = ci.DateTimeFormat;
2494 provider = ci.NumberFormat;
2496 return ToType (value, conversionType, provider);
2499 public static object ChangeType (object value, Type conversionType, IFormatProvider provider)
2501 if ((value != null) && (conversionType == null))
2502 throw new ArgumentNullException ("conversionType");
2503 return ToType (value, conversionType, provider);
2506 public static object ChangeType (object value, TypeCode typeCode, IFormatProvider provider)
2508 Type conversionType = conversionTable [(int)typeCode];
2509 return ToType (value, conversionType, provider);
2512 private static bool NotValidBase (int value)
2514 if ((value == 2) || (value == 8) ||
2515 (value == 10) || (value == 16))
2521 private static int ConvertFromBase (string value, int fromBase, bool unsigned)
2523 if (NotValidBase (fromBase))
2524 throw new ArgumentException ("fromBase is not valid.");
2533 int len = value.Length;
2534 bool negative = false;
2536 // special processing for some bases
2539 if (value.Substring (i, 1) == "-") {
2541 throw new OverflowException (
2542 Locale.GetText ("The string was being parsed as"
2543 + " an unsigned number and could not have a"
2544 + " negative sign."));
2551 if (value.Substring (i, 1) == "-") {
2552 throw new ArgumentException ("String cannot contain a "
2553 + "minus sign if the base is not 10.");
2557 if ((value[i] == '0') && ((value [i+1] == 'x') || (value [i+1] == 'X'))) {
2563 if (value.Substring (i, 1) == "-") {
2564 throw new ArgumentException ("String cannot contain a "
2565 + "minus sign if the base is not 10.");
2571 throw new FormatException ("Could not find any parsable digits.");
2574 if (value[i] == '+') {
2579 char c = value[i++];
2580 if (Char.IsNumber (c)) {
2581 digitValue = c - '0';
2582 } else if (Char.IsLetter (c)) {
2583 digitValue = Char.ToLowerInvariant (c) - 'a' + 10;
2586 throw new FormatException ("Additional unparsable "
2587 + "characters are at the end of the string.");
2589 throw new FormatException ("Could not find any parsable"
2594 if (digitValue >= fromBase) {
2596 throw new FormatException ("Additional unparsable "
2597 + "characters are at the end of the string.");
2599 throw new FormatException ("Could not find any parsable"
2604 result = (fromBase) * result + digitValue;
2609 throw new FormatException ("Could not find any parsable digits.");
2617 // note: this has nothing to do with base64 encoding (just base and Int64)
2618 private static long ConvertFromBase64 (string value, int fromBase, bool unsigned)
2620 if (NotValidBase (fromBase))
2621 throw new ArgumentException ("fromBase is not valid.");
2626 int digitValue = -1;
2628 bool negative = false;
2631 int len = value.Length;
2633 // special processing for some bases
2636 if (value.Substring(i, 1) == "-") {
2638 throw new OverflowException (
2639 Locale.GetText ("The string was being parsed as"
2640 + " an unsigned number and could not have a"
2641 + " negative sign."));
2648 if (value.Substring (i, 1) == "-") {
2649 throw new ArgumentException ("String cannot contain a "
2650 + "minus sign if the base is not 10.");
2654 if ((value[i] == '0') && ((value[i + 1] == 'x') || (value[i + 1] == 'X'))) {
2660 if (value.Substring (i, 1) == "-") {
2661 throw new ArgumentException ("String cannot contain a "
2662 + "minus sign if the base is not 10.");
2668 throw new FormatException ("Could not find any parsable digits.");
2671 if (value[i] == '+') {
2676 char c = value[i++];
2677 if (Char.IsNumber (c)) {
2678 digitValue = c - '0';
2679 } else if (Char.IsLetter (c)) {
2680 digitValue = Char.ToLowerInvariant (c) - 'a' + 10;
2683 throw new FormatException ("Additional unparsable "
2684 + "characters are at the end of the string.");
2686 throw new FormatException ("Could not find any parsable"
2691 if (digitValue >= fromBase) {
2693 throw new FormatException ("Additional unparsable "
2694 + "characters are at the end of the string.");
2696 throw new FormatException ("Could not find any parsable"
2701 result = (fromBase * result + digitValue);
2706 throw new FormatException ("Could not find any parsable digits.");
2714 private static void EndianSwap (ref byte[] value)
2716 byte[] buf = new byte[value.Length];
2717 for (int i = 0; i < value.Length; i++)
2718 buf[i] = value[value.Length-1-i];
2722 private static string ConvertToBase2 (byte[] value)
2724 if (!BitConverter.IsLittleEndian)
2725 EndianSwap (ref value);
2726 StringBuilder sb = new StringBuilder ();
2727 for (int i = value.Length - 1; i >= 0; i--) {
2729 for (int j = 0; j < 8; j++) {
2730 if ((b & 0x80) == 0x80) {
2740 return sb.ToString ();
2743 private static string ConvertToBase8 (byte[] value)
2746 switch (value.Length) {
2748 l = (ulong) value [0];
2751 l = (ulong) BitConverter.ToUInt16 (value, 0);
2754 l = (ulong) BitConverter.ToUInt32 (value, 0);
2757 l = BitConverter.ToUInt64 (value, 0);
2760 throw new ArgumentException ("value");
2763 StringBuilder sb = new StringBuilder ();
2764 for (int i = 21; i >= 0; i--) {
2765 // 3 bits at the time
2766 char val = (char) ((l >> i * 3) & 0x7);
2767 if ((val != 0) || (sb.Length > 0)) {
2772 return sb.ToString ();
2775 private static string ConvertToBase16 (byte[] value)
2777 if (!BitConverter.IsLittleEndian)
2778 EndianSwap (ref value);
2779 StringBuilder sb = new StringBuilder ();
2780 for (int i = value.Length - 1; i >= 0; i--) {
2781 char high = (char)((value[i] >> 4) & 0x0f);
2782 if ((high != 0) || (sb.Length > 0)) {
2792 char low = (char)(value[i] & 0x0f);
2793 if ((low != 0) || (sb.Length > 0)) {
2803 return sb.ToString ();
2806 // Lookup table for the conversion ToType method. Order
2807 // is important! Used by ToType for comparing the target
2808 // type, and uses hardcoded array indexes.
2809 private static readonly Type[] conversionTable = {
2810 // Valid ICovnertible Types
2812 typeof (object), // 1 TypeCode.Object
2813 typeof (DBNull), // 2 TypeCode.DBNull
2814 typeof (Boolean), // 3 TypeCode.Boolean
2815 typeof (Char), // 4 TypeCode.Char
2816 typeof (SByte), // 5 TypeCode.SByte
2817 typeof (Byte), // 6 TypeCode.Byte
2818 typeof (Int16), // 7 TypeCode.Int16
2819 typeof (UInt16), // 8 TypeCode.UInt16
2820 typeof (Int32), // 9 TypeCode.Int32
2821 typeof (UInt32), // 10 TypeCode.UInt32
2822 typeof (Int64), // 11 TypeCode.Int64
2823 typeof (UInt64), // 12 TypeCode.UInt64
2824 typeof (Single), // 13 TypeCode.Single
2825 typeof (Double), // 14 TypeCode.Double
2826 typeof (Decimal), // 15 TypeCode.Decimal
2827 typeof (DateTime), // 16 TypeCode.DateTime
2829 typeof (String), // 18 TypeCode.String
2832 // Function to convert an object to another type and return
2833 // it as an object. In place for the core data types to use
2834 // when implementing IConvertible. Uses hardcoded indexes in
2835 // the conversionTypes array, so if modify carefully.
2836 internal static object ToType (object value, Type conversionType,
2837 IFormatProvider provider)
2839 if (value == null) {
2840 if ((conversionType != null) && conversionType.IsValueType){
2842 throw new InvalidCastException ("Null object can not be converted to a value type.");
2845 // Bug compatibility with 1.0
2847 throw new NullReferenceException ("Null object can not be converted to a value type.");
2853 if (conversionType == null)
2854 throw new InvalidCastException ("Cannot cast to destination type.");
2856 if (value.GetType () == conversionType)
2859 if (value is IConvertible) {
2860 IConvertible convertValue = (IConvertible) value;
2862 if (conversionType == conversionTable[0]) // 0 Empty
2863 throw new ArgumentNullException ();
2865 else if (conversionType == conversionTable[1]) // 1 TypeCode.Object
2866 return (object) value;
2868 else if (conversionType == conversionTable[2]) // 2 TypeCode.DBNull
2869 throw new InvalidCastException (
2870 "Cannot cast to DBNull, it's not IConvertible");
2872 else if (conversionType == conversionTable[3]) // 3 TypeCode.Boolean
2873 return (object) convertValue.ToBoolean (provider);
2875 else if (conversionType == conversionTable[4]) // 4 TypeCode.Char
2876 return (object) convertValue.ToChar (provider);
2878 else if (conversionType == conversionTable[5]) // 5 TypeCode.SByte
2879 return (object) convertValue.ToSByte (provider);
2881 else if (conversionType == conversionTable[6]) // 6 TypeCode.Byte
2882 return (object) convertValue.ToByte (provider);
2884 else if (conversionType == conversionTable[7]) // 7 TypeCode.Int16
2885 return (object) convertValue.ToInt16 (provider);
2887 else if (conversionType == conversionTable[8]) // 8 TypeCode.UInt16
2888 return (object) convertValue.ToUInt16 (provider);
2890 else if (conversionType == conversionTable[9]) // 9 TypeCode.Int32
2891 return (object) convertValue.ToInt32 (provider);
2893 else if (conversionType == conversionTable[10]) // 10 TypeCode.UInt32
2894 return (object) convertValue.ToUInt32 (provider);
2896 else if (conversionType == conversionTable[11]) // 11 TypeCode.Int64
2897 return (object) convertValue.ToInt64 (provider);
2899 else if (conversionType == conversionTable[12]) // 12 TypeCode.UInt64
2900 return (object) convertValue.ToUInt64 (provider);
2902 else if (conversionType == conversionTable[13]) // 13 TypeCode.Single
2903 return (object) convertValue.ToSingle (provider);
2905 else if (conversionType == conversionTable[14]) // 14 TypeCode.Double
2906 return (object) convertValue.ToDouble (provider);
2908 else if (conversionType == conversionTable[15]) // 15 TypeCode.Decimal
2909 return (object) convertValue.ToDecimal (provider);
2911 else if (conversionType == conversionTable[16]) // 16 TypeCode.DateTime
2912 return (object) convertValue.ToDateTime (provider);
2914 else if (conversionType == conversionTable[18]) // 18 TypeCode.String
2915 return (object) convertValue.ToString (provider);
2917 throw new InvalidCastException (Locale.GetText ("Unknown target conversion type"));
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)));