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;
101 // [CLSCompliant(false)]
103 public static class Convert {
105 public sealed class Convert {
112 public static readonly object DBNull = System.DBNull.Value;
113 static ToBase64Transform toBase64Transform = new ToBase64Transform();
115 [MethodImplAttribute (MethodImplOptions.InternalCall)]
116 extern static byte [] InternalFromBase64String (string str, bool allowWhitespaceOnly);
118 [MethodImplAttribute (MethodImplOptions.InternalCall)]
119 extern static byte [] InternalFromBase64CharArray (char [] arr, int offset, int length);
121 public static byte[] FromBase64CharArray (char[] inArray, int offset, int length)
124 throw new ArgumentNullException ("inArray");
126 throw new ArgumentOutOfRangeException ("offset < 0");
128 throw new ArgumentOutOfRangeException ("length < 0");
129 // avoid integer overflow
130 if (offset > inArray.Length - length)
131 throw new ArgumentOutOfRangeException ("offset + length > array.Length");
133 return InternalFromBase64CharArray (inArray, offset, length);
136 public static byte[] FromBase64String (string s)
139 throw new ArgumentNullException ("s");
146 return InternalFromBase64String (s, true);
148 return InternalFromBase64String (s, false);
152 public static TypeCode GetTypeCode (object value)
155 return TypeCode.Empty;
157 return Type.GetTypeCode (value.GetType ());
160 public static bool IsDBNull (object value)
168 public static int ToBase64CharArray (byte[] inArray, int offsetIn, int length,
169 char[] outArray, int offsetOut)
172 throw new ArgumentNullException ("inArray");
173 if (outArray == null)
174 throw new ArgumentNullException ("outArray");
175 if (offsetIn < 0 || length < 0 || offsetOut < 0)
176 throw new ArgumentOutOfRangeException ("offsetIn, length, offsetOut < 0");
177 // avoid integer overflow
178 if (offsetIn > inArray.Length - length)
179 throw new ArgumentOutOfRangeException ("offsetIn + length > array.Length");
181 // note: normally ToBase64Transform doesn't support multiple block processing
182 byte[] outArr = toBase64Transform.InternalTransformFinalBlock (inArray, offsetIn, length);
184 char[] cOutArr = new ASCIIEncoding ().GetChars (outArr);
186 // avoid integer overflow
187 if (offsetOut > outArray.Length - cOutArr.Length)
188 throw new ArgumentOutOfRangeException ("offsetOut + cOutArr.Length > outArray.Length");
190 Array.Copy (cOutArr, 0, outArray, offsetOut, cOutArr.Length);
192 return cOutArr.Length;
195 public static string ToBase64String (byte[] inArray)
198 throw new ArgumentNullException ("inArray");
200 return ToBase64String (inArray, 0, inArray.Length);
203 public static string ToBase64String (byte[] inArray, int offset, int length)
206 throw new ArgumentNullException ("inArray");
207 if (offset < 0 || length < 0)
208 throw new ArgumentOutOfRangeException ("offset < 0 || length < 0");
209 // avoid integer overflow
210 if (offset > inArray.Length - length)
211 throw new ArgumentOutOfRangeException ("offset + length > array.Length");
213 // note: normally ToBase64Transform doesn't support multiple block processing
214 byte[] outArr = toBase64Transform.InternalTransformFinalBlock (inArray, offset, length);
216 return (new ASCIIEncoding ().GetString (outArr));
220 public static string ToBase64String (byte[] inArray, Base64FormattingOptions options)
223 throw new ArgumentNullException ("inArray");
224 return ToBase64String (inArray, 0, inArray.Length, options);
227 public static string ToBase64String (byte[] inArray, int offset, int length, Base64FormattingOptions options)
230 throw new ArgumentNullException ("inArray");
231 if (offset < 0 || length < 0)
232 throw new ArgumentOutOfRangeException ("offset < 0 || length < 0");
233 // avoid integer overflow
234 if (offset > inArray.Length - length)
235 throw new ArgumentOutOfRangeException ("offset + length > array.Length");
237 Encoding encoding = new ASCIIEncoding ();
238 BinaryReader reader = new BinaryReader (new MemoryStream (inArray, offset, length));
241 if (options == Base64FormattingOptions.InsertLineBreaks) {
242 StringBuilder sb = new StringBuilder ();
244 // 54 bytes of input makes for 72 bytes of output.
245 b = reader.ReadBytes (54);
247 sb.AppendLine (encoding.GetString (toBase64Transform.InternalTransformFinalBlock (b, 0, b.Length)));
248 } while (b.Length > 0);
249 return sb.ToString ();
251 return encoding.GetString (toBase64Transform.InternalTransformFinalBlock (inArray, offset, length));
258 // ========== Boolean Conversions ========== //
260 public static bool ToBoolean (bool value)
265 public static bool ToBoolean (byte value)
270 public static bool ToBoolean (char value)
272 throw new InvalidCastException (Locale.GetText ("Can't convert char to bool"));
275 public static bool ToBoolean (DateTime value)
277 throw new InvalidCastException (Locale.GetText ("Can't convert date to bool"));
280 public static bool ToBoolean (decimal value)
282 return (value != 0M);
285 public static bool ToBoolean (double value)
290 public static bool ToBoolean (float value)
292 return (value != 0f);
295 public static bool ToBoolean (int value)
300 public static bool ToBoolean (long value)
305 [CLSCompliant (false)]
306 public static bool ToBoolean (sbyte value)
311 public static bool ToBoolean (short value)
316 public static bool ToBoolean (string value)
319 return false; // LAMESPEC: Spec says throw ArgumentNullException
320 return Boolean.Parse (value);
323 public static bool ToBoolean (string value, IFormatProvider provider)
326 return false; // LAMESPEC: Spec says throw ArgumentNullException
327 return Boolean.Parse (value); // provider is ignored.
330 [CLSCompliant (false)]
331 public static bool ToBoolean (uint value)
336 [CLSCompliant (false)]
337 public static bool ToBoolean (ulong value)
342 [CLSCompliant (false)]
343 public static bool ToBoolean (ushort value)
350 public static bool ToBoolean (object value)
354 return ToBoolean (value, null);
357 public static bool ToBoolean (object value, IFormatProvider provider)
361 return ((IConvertible) value).ToBoolean (provider);
364 // ========== Byte Conversions ========== //
366 public static byte ToByte (bool value)
368 return (byte)(value ? 1 : 0);
371 public static byte ToByte (byte value)
376 public static byte ToByte (char value)
378 if (value > Byte.MaxValue)
379 throw new OverflowException (Locale.GetText (
380 "Value is greater than Byte.MaxValue"));
385 public static byte ToByte (DateTime value)
387 throw new InvalidCastException ("This conversion is not supported.");
390 public static byte ToByte (decimal value)
392 if (value > Byte.MaxValue || value < Byte.MinValue)
393 throw new OverflowException (Locale.GetText (
394 "Value is greater than Byte.MaxValue or less than Byte.MinValue"));
396 // Returned Even-Rounded
397 return (byte)(Math.Round (value));
400 public static byte ToByte (double value)
402 if (value > Byte.MaxValue || value < Byte.MinValue)
403 throw new OverflowException (Locale.GetText (
404 "Value is greater than Byte.MaxValue or less than Byte.MinValue"));
406 // This and the float version of ToByte are the only ones
407 // the spec listed as checking for .NaN and Infinity overflow
408 if (Double.IsNaN(value) || Double.IsInfinity(value))
409 throw new OverflowException (Locale.GetText (
410 "Value is equal to Double.NaN, Double.PositiveInfinity, or Double.NegativeInfinity"));
412 // Returned Even-Rounded
413 return (byte)(Math.Round (value));
416 public static byte ToByte (float value)
418 if (value > Byte.MaxValue || value < Byte.MinValue)
419 throw new OverflowException (Locale.GetText (
420 "Value is greater than Byte.MaxValue or less than Byte.Minalue"));
422 // This and the double version of ToByte are the only ones
423 // the spec listed as checking for .NaN and Infinity overflow
424 if (Single.IsNaN(value) || Single.IsInfinity(value))
425 throw new OverflowException (Locale.GetText (
426 "Value is equal to Single.NaN, Single.PositiveInfinity, or Single.NegativeInfinity"));
428 // Returned Even-Rounded, pass it as a double, could have this
429 // method just call Convert.ToByte ( (double)value)
430 return (byte)(Math.Round ( (double)value));
433 public static byte ToByte (int value)
435 if (value > Byte.MaxValue || value < Byte.MinValue)
436 throw new OverflowException (Locale.GetText (
437 "Value is greater than Byte.MaxValue or less than Byte.MinValue"));
442 public static byte ToByte (long value)
444 if (value > Byte.MaxValue || value < Byte.MinValue)
445 throw new OverflowException (Locale.GetText (
446 "Value is greater than Byte.MaxValue or less than Byte.MinValue"));
451 [CLSCompliant (false)]
452 public static byte ToByte (sbyte value)
454 if (value < Byte.MinValue)
455 throw new OverflowException (Locale.GetText (
456 "Value is less than Byte.MinValue"));
461 public static byte ToByte (short value)
463 if (value > Byte.MaxValue || value < Byte.MinValue)
464 throw new OverflowException (Locale.GetText (
465 "Value is greater than Byte.MaxValue or less than Byte.MinValue"));
470 public static byte ToByte (string value)
473 return 0; // LAMESPEC: Spec says throw ArgumentNullException
474 return Byte.Parse (value);
477 public static byte ToByte (string value, IFormatProvider provider)
480 return 0; // LAMESPEC: Spec says throw ArgumentNullException
481 return Byte.Parse (value, provider);
484 public static byte ToByte (string value, int fromBase)
486 int retVal = ConvertFromBase (value, fromBase, true);
488 if (retVal < (int) Byte.MinValue || retVal > (int) Byte.MaxValue)
489 throw new OverflowException ();
491 return (byte) retVal;
494 [CLSCompliant (false)]
495 public static byte ToByte (uint value)
497 if (value > Byte.MaxValue)
498 throw new OverflowException (Locale.GetText (
499 "Value is greater than Byte.MaxValue"));
504 [CLSCompliant (false)]
505 public static byte ToByte (ulong value)
507 if (value > Byte.MaxValue)
508 throw new OverflowException (Locale.GetText (
509 "Value is greater than Byte.MaxValue"));
514 [CLSCompliant (false)]
515 public static byte ToByte (ushort value)
517 if (value > Byte.MaxValue)
518 throw new OverflowException (Locale.GetText (
519 "Value is greater than Byte.MaxValue"));
524 public static byte ToByte (object value)
528 return ToByte (value, null);
531 public static byte ToByte (object value, IFormatProvider provider)
535 return ((IConvertible) value).ToByte (provider);
538 // ========== Char Conversions ========== //
540 public static char ToChar (bool value)
542 throw new InvalidCastException ("This conversion is not supported.");
545 public static char ToChar (byte value)
550 public static char ToChar (char value)
555 public static char ToChar (DateTime value)
557 throw new InvalidCastException ("This conversion is not supported.");
560 public static char ToChar (decimal value)
562 throw new InvalidCastException ("This conversion is not supported.");
565 public static char ToChar (double value)
567 throw new InvalidCastException ("This conversion is not supported.");
570 public static char ToChar (int value)
572 if (value > Char.MaxValue || value < Char.MinValue)
573 throw new OverflowException (Locale.GetText (
574 "Value is greater than Char.MaxValue or less than Char.MinValue"));
579 public static char ToChar (long value)
581 if (value > Char.MaxValue || value < Char.MinValue)
582 throw new OverflowException (Locale.GetText (
583 "Value is greater than Char.MaxValue or less than Char.MinValue"));
588 public static char ToChar (float value)
590 throw new InvalidCastException ("This conversion is not supported.");
593 [CLSCompliant (false)]
594 public static char ToChar (sbyte value)
596 if (value < Char.MinValue)
597 throw new OverflowException (Locale.GetText (
598 "Value is less than Char.MinValue"));
603 public static char ToChar (short value)
605 if (value < Char.MinValue)
606 throw new OverflowException (Locale.GetText (
607 "Value is less than Char.MinValue"));
612 public static char ToChar (string value)
614 return Char.Parse (value);
617 public static char ToChar (string value, IFormatProvider provider)
619 return Char.Parse (value); // provider is ignored.
622 [CLSCompliant (false)]
623 public static char ToChar (uint value)
625 if (value > Char.MaxValue)
626 throw new OverflowException (Locale.GetText (
627 "Value is greater than Char.MaxValue"));
632 [CLSCompliant (false)]
633 public static char ToChar (ulong value)
635 if (value > Char.MaxValue)
636 throw new OverflowException (Locale.GetText (
637 "Value is greater than Char.MaxValue"));
642 [CLSCompliant (false)]
643 public static char ToChar (ushort value)
645 if (value > Char.MaxValue)
646 throw new OverflowException (Locale.GetText (
647 "Value is greater than Char.MaxValue"));
652 public static char ToChar (object value)
656 return ToChar (value, null);
659 public static char ToChar (object value, IFormatProvider provider)
663 return ((IConvertible) value).ToChar (provider);
666 // ========== DateTime Conversions ========== //
668 public static DateTime ToDateTime (string value)
671 return DateTime.MinValue; // LAMESPEC: Spec says throw ArgumentNullException
672 return DateTime.Parse (value);
675 public static DateTime ToDateTime (string value, IFormatProvider provider)
678 return DateTime.MinValue; // LAMESPEC: Spec says throw ArgumentNullException
679 return DateTime.Parse (value, provider);
682 public static DateTime ToDateTime (bool value)
684 throw new InvalidCastException ("This conversion is not supported.");
687 public static DateTime ToDateTime (byte value)
689 throw new InvalidCastException ("This conversion is not supported.");
692 public static DateTime ToDateTime (char value)
694 throw new InvalidCastException ("This conversion is not supported.");
697 public static DateTime ToDateTime (DateTime value)
702 public static DateTime ToDateTime (decimal value)
704 throw new InvalidCastException ("This conversion is not supported.");
707 public static DateTime ToDateTime (double value)
709 throw new InvalidCastException ("This conversion is not supported.");
712 public static DateTime ToDateTime (short value)
714 throw new InvalidCastException ("This conversion is not supported.");
717 public static DateTime ToDateTime (int value)
719 throw new InvalidCastException ("This conversion is not supported.");
722 public static DateTime ToDateTime (long value)
724 throw new InvalidCastException ("This conversion is not supported.");
727 public static DateTime ToDateTime (float value)
729 throw new InvalidCastException ("This conversion is not supported.");
732 public static DateTime ToDateTime (object value)
735 return DateTime.MinValue;
736 return ToDateTime (value, null);
739 public static DateTime ToDateTime (object value, IFormatProvider provider)
742 return DateTime.MinValue;
743 return ((IConvertible) value).ToDateTime (provider);
746 [CLSCompliant (false)]
747 public static DateTime ToDateTime (sbyte value)
749 throw new InvalidCastException ("This conversion is not supported.");
751 [CLSCompliant (false)]
752 public static DateTime ToDateTime (ushort value)
754 throw new InvalidCastException ("This conversion is not supported.");
757 [CLSCompliant (false)]
758 public static DateTime ToDateTime (uint value)
760 throw new InvalidCastException ("This conversion is not supported.");
763 [CLSCompliant (false)]
764 public static DateTime ToDateTime (ulong value)
766 throw new InvalidCastException ("This conversion is not supported.");
769 // ========== Decimal Conversions ========== //
771 public static decimal ToDecimal (bool value)
773 return value ? 1 : 0;
776 public static decimal ToDecimal (byte value)
778 return (decimal)value;
781 public static decimal ToDecimal (char value)
783 throw new InvalidCastException ("This conversion is not supported.");
786 public static decimal ToDecimal (DateTime value)
788 throw new InvalidCastException ("This conversion is not supported.");
791 public static decimal ToDecimal (decimal value)
796 public static decimal ToDecimal (double value)
798 if (value > (double)Decimal.MaxValue || value < (double)Decimal.MinValue)
799 throw new OverflowException (Locale.GetText (
800 "Value is greater than Decimal.MaxValue or less than Decimal.MinValue"));
802 return (decimal)value;
805 public static decimal ToDecimal (float value)
807 return (decimal) value;
810 public static decimal ToDecimal (int value)
812 return (decimal)value;
815 public static decimal ToDecimal (long value)
817 return (decimal)value;
820 [CLSCompliant (false)]
821 public static decimal ToDecimal (sbyte value)
823 return (decimal)value;
826 public static decimal ToDecimal (short value)
828 return (decimal)value;
831 public static decimal ToDecimal (string value)
834 return new Decimal (0); // LAMESPEC: Spec says throw ArgumentNullException
835 return Decimal.Parse (value);
838 public static decimal ToDecimal (string value, IFormatProvider provider)
841 return new Decimal (0); // LAMESPEC: Spec says throw ArgumentNullException
842 return Decimal.Parse (value, provider);
845 [CLSCompliant (false)]
846 public static decimal ToDecimal (uint value)
848 return (decimal)value;
851 [CLSCompliant (false)]
852 public static decimal ToDecimal (ulong value)
854 return (decimal)value;
857 [CLSCompliant (false)]
858 public static decimal ToDecimal (ushort value)
860 return (decimal)value;
863 public static decimal ToDecimal (object value)
866 return new Decimal (0);
867 return ToDecimal (value, null);
870 public static decimal ToDecimal (object value, IFormatProvider provider)
873 return new Decimal (0);
874 return ((IConvertible) value).ToDecimal (provider);
878 // ========== Double Conversions ========== //
880 public static double ToDouble (bool value)
882 return value ? 1 : 0;
885 public static double ToDouble (byte value)
887 return (double) value;
890 public static double ToDouble (char value)
892 throw new InvalidCastException ("This conversion is not supported.");
895 public static double ToDouble (DateTime value)
897 throw new InvalidCastException ("This conversion is not supported.");
900 public static double ToDouble (decimal value)
902 return (double)value;
905 public static double ToDouble (double value)
910 public static double ToDouble (float value)
912 return (double) value;
915 public static double ToDouble (int value)
917 return (double)value;
920 public static double ToDouble (long value)
922 return (double)value;
925 [CLSCompliant (false)]
926 public static double ToDouble (sbyte value)
928 return (double)value;
931 public static double ToDouble (short value)
933 return (double)value;
936 public static double ToDouble (string value)
939 return 0.0; // LAMESPEC: Spec says throw ArgumentNullException
940 return Double.Parse (value);
943 public static double ToDouble (string value, IFormatProvider provider)
946 return 0.0; // LAMESPEC: Spec says throw ArgumentNullException
947 return Double.Parse (value, provider);
950 [CLSCompliant (false)]
951 public static double ToDouble (uint value)
953 return (double)value;
956 [CLSCompliant (false)]
957 public static double ToDouble (ulong value)
959 return (double)value;
962 [CLSCompliant (false)]
963 public static double ToDouble (ushort value)
965 return (double)value;
968 public static double ToDouble (object value)
972 return ToDouble (value, null);
975 public static double ToDouble (object value, IFormatProvider provider)
979 return ((IConvertible) value).ToDouble (provider);
982 // ========== Int16 Conversions ========== //
984 public static short ToInt16 (bool value)
986 return (short)(value ? 1 : 0);
989 public static short ToInt16 (byte value)
994 public static short ToInt16 (char value)
996 if (value > Int16.MaxValue)
997 throw new OverflowException (Locale.GetText (
998 "Value is greater than Int16.MaxValue"));
1000 return (short)value;
1003 public static short ToInt16 (DateTime value)
1005 throw new InvalidCastException ("This conversion is not supported.");
1008 public static short ToInt16 (decimal value)
1010 if (value > Int16.MaxValue || value < Int16.MinValue)
1011 throw new OverflowException (Locale.GetText (
1012 "Value is greater than Int16.MaxValue or less than Int16.MinValue"));
1014 // Returned Even-Rounded
1015 return (short)(Math.Round (value));
1018 public static short ToInt16 (double value)
1020 if (value > Int16.MaxValue || value < Int16.MinValue)
1021 throw new OverflowException (Locale.GetText (
1022 "Value is greater than Int16.MaxValue or less than Int16.MinValue"));
1024 // Returned Even-Rounded
1025 return (short)(Math.Round (value));
1028 public static short ToInt16 (float value)
1030 if (value > Int16.MaxValue || value < Int16.MinValue)
1031 throw new OverflowException (Locale.GetText (
1032 "Value is greater than Int16.MaxValue or less than Int16.MinValue"));
1034 // Returned Even-Rounded, use Math.Round pass as a double.
1035 return (short)Math.Round ( (double)value);
1038 public static short ToInt16 (int value)
1040 if (value > Int16.MaxValue || value < Int16.MinValue)
1041 throw new OverflowException (Locale.GetText (
1042 "Value is greater than Int16.MaxValue or less than Int16.MinValue"));
1044 return (short)value;
1047 public static short ToInt16 (long 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 return (short)value;
1056 [CLSCompliant (false)]
1057 public static short ToInt16 (sbyte value)
1059 return (short)value;
1062 public static short ToInt16 (short value)
1067 public static short ToInt16 (string value)
1070 return 0; // LAMESPEC: Spec says throw ArgumentNullException
1071 return Int16.Parse (value);
1074 public static short ToInt16 (string value, IFormatProvider provider)
1077 return 0; // LAMESPEC: Spec says throw ArgumentNullException
1078 return Int16.Parse (value, provider);
1081 public static short ToInt16 (string value, int fromBase)
1083 int result = ConvertFromBase (value, fromBase, false);
1084 if (fromBase != 10) {
1085 if (result > ushort.MaxValue) {
1086 throw new OverflowException ("Value was either too large or too small for an Int16.");
1089 // note: no sign are available to detect negatives
1090 if (result > Int16.MaxValue) {
1091 // return negative 2's complement
1092 return Convert.ToInt16 (-(65536 - result));
1095 return Convert.ToInt16 (result);
1098 [CLSCompliant (false)]
1099 public static short ToInt16 (uint value)
1101 if (value > Int16.MaxValue)
1102 throw new OverflowException (Locale.GetText (
1103 "Value is greater than Int16.MaxValue"));
1105 return (short)value;
1108 [CLSCompliant (false)]
1109 public static short ToInt16 (ulong value)
1111 if (value > (ulong)Int16.MaxValue)
1112 throw new OverflowException (Locale.GetText (
1113 "Value is greater than Int16.MaxValue"));
1114 return (short)value;
1117 [CLSCompliant (false)]
1118 public static short ToInt16 (ushort value)
1120 if (value > Int16.MaxValue)
1121 throw new OverflowException (Locale.GetText (
1122 "Value is greater than Int16.MaxValue"));
1124 return (short)value;
1127 public static short ToInt16 (object value)
1131 return ToInt16 (value, null);
1134 public static short ToInt16 (object value, IFormatProvider provider)
1138 return ((IConvertible) value).ToInt16 (provider);
1141 // ========== Int32 Conversions ========== //
1143 public static int ToInt32 (bool value)
1145 return value ? 1 : 0;
1148 public static int ToInt32 (byte value)
1153 public static int ToInt32 (char value)
1158 public static int ToInt32 (DateTime value)
1160 throw new InvalidCastException ("This conversion is not supported.");
1163 public static int ToInt32 (decimal value)
1165 if (value > Int32.MaxValue || value < Int32.MinValue)
1166 throw new OverflowException (Locale.GetText (
1167 "Value is greater than Int32.MaxValue or less than Int32.MinValue"));
1169 // Returned Even-Rounded
1170 return (int)(Math.Round (value));
1173 public static int ToInt32 (double value)
1175 if (value > Int32.MaxValue || value < Int32.MinValue)
1176 throw new OverflowException (Locale.GetText (
1177 "Value is greater than Int32.MaxValue or less than Int32.MinValue"));
1179 // Returned Even-Rounded
1180 return (int)(Math.Round (value));
1183 public static int ToInt32 (float value)
1185 if (value > Int32.MaxValue || value < Int32.MinValue)
1186 throw new OverflowException (Locale.GetText (
1187 "Value is greater than Int32.MaxValue or less than Int32.MinValue"));
1189 // Returned Even-Rounded, pass as a double, could just call
1190 // Convert.ToInt32 ( (double)value);
1191 return (int)(Math.Round ( (double)value));
1194 public static int ToInt32 (int value)
1199 public static int ToInt32 (long value)
1201 if (value > Int32.MaxValue || value < Int32.MinValue)
1202 throw new OverflowException (Locale.GetText (
1203 "Value is greater than Int32.MaxValue or less than Int32.MinValue"));
1208 [CLSCompliant (false)]
1209 public static int ToInt32 (sbyte value)
1214 public static int ToInt32 (short value)
1219 public static int ToInt32 (string value)
1222 return 0; // LAMESPEC: Spec says throw ArgumentNullException
1223 return Int32.Parse (value);
1226 public static int ToInt32 (string value, IFormatProvider provider)
1229 return 0; // LAMESPEC: Spec says throw ArgumentNullException
1230 return Int32.Parse (value, provider);
1234 public static int ToInt32 (string value, int fromBase)
1236 return ConvertFromBase (value, fromBase, false);
1239 [CLSCompliant (false)]
1240 public static int ToInt32 (uint value)
1242 if (value > Int32.MaxValue)
1243 throw new OverflowException (Locale.GetText (
1244 "Value is greater than Int32.MaxValue"));
1249 [CLSCompliant (false)]
1250 public static int ToInt32 (ulong value)
1252 if (value > Int32.MaxValue)
1253 throw new OverflowException (Locale.GetText (
1254 "Value is greater than Int32.MaxValue"));
1259 [CLSCompliant (false)]
1260 public static int ToInt32 (ushort value)
1265 public static int ToInt32 (object value)
1269 return ToInt32 (value, null);
1272 public static int ToInt32 (object value, IFormatProvider provider)
1276 return ((IConvertible) value).ToInt32 (provider);
1279 // ========== Int64 Conversions ========== //
1281 public static long ToInt64 (bool value)
1283 return value ? 1 : 0;
1286 public static long ToInt64 (byte value)
1288 return (long)(ulong)value;
1291 public static long ToInt64 (char value)
1296 public static long ToInt64 (DateTime value)
1298 throw new InvalidCastException ("This conversion is not supported.");
1301 public static long ToInt64 (decimal value)
1303 if (value > Int64.MaxValue || value < Int64.MinValue)
1304 throw new OverflowException (Locale.GetText (
1305 "Value is greater than Int64.MaxValue or less than Int64.MinValue"));
1307 // Returned Even-Rounded
1308 return (long)(Math.Round (value));
1311 public static long ToInt64 (double value)
1313 if (value > Int64.MaxValue || value < Int64.MinValue)
1314 throw new OverflowException (Locale.GetText (
1315 "Value is greater than Int64.MaxValue or less than Int64.MinValue"));
1317 // Returned Even-Rounded
1318 return (long)(Math.Round (value));
1321 public static long ToInt64 (float value)
1323 if (value > Int64.MaxValue || value < Int64.MinValue)
1324 throw new OverflowException (Locale.GetText (
1325 "Value is greater than Int64.MaxValue or less than Int64.MinValue"));
1327 // Returned Even-Rounded, pass to Math as a double, could
1328 // just call Convert.ToInt64 ( (double)value);
1329 return (long)(Math.Round ( (double)value));
1332 public static long ToInt64 (int value)
1337 public static long ToInt64 (long value)
1342 [CLSCompliant (false)]
1343 public static long ToInt64 (sbyte value)
1348 public static long ToInt64 (short value)
1353 public static long ToInt64 (string value)
1356 return 0; // LAMESPEC: Spec says throw ArgumentNullException
1357 return Int64.Parse (value);
1360 public static long ToInt64 (string value, IFormatProvider provider)
1363 return 0; // LAMESPEC: Spec says throw ArgumentNullException
1364 return Int64.Parse (value, provider);
1367 public static long ToInt64 (string value, int fromBase)
1369 return ConvertFromBase64 (value, fromBase, false);
1372 [CLSCompliant (false)]
1373 public static long ToInt64 (uint value)
1375 return (long)(ulong)value;
1378 [CLSCompliant (false)]
1379 public static long ToInt64 (ulong value)
1381 if (value > Int64.MaxValue)
1382 throw new OverflowException (Locale.GetText (
1383 "Value is greater than Int64.MaxValue"));
1388 [CLSCompliant (false)]
1389 public static long ToInt64 (ushort value)
1391 return (long)(ulong)value;
1394 public static long ToInt64 (object value)
1398 return ToInt64 (value, null);
1401 public static long ToInt64 (object value, IFormatProvider provider)
1405 return ((IConvertible) value).ToInt64 (provider);
1408 // ========== SByte Conversions ========== //
1410 [CLSCompliant (false)]
1411 public static sbyte ToSByte (bool value)
1413 return (sbyte)(value ? 1 : 0);
1416 [CLSCompliant (false)]
1417 public static sbyte ToSByte (byte value)
1419 if (value > SByte.MaxValue)
1420 throw new OverflowException (Locale.GetText (
1421 "Value is greater than SByte.MaxValue"));
1423 return (sbyte)value;
1426 [CLSCompliant (false)]
1427 public static sbyte ToSByte (char value)
1429 if (value > SByte.MaxValue)
1430 throw new OverflowException (Locale.GetText (
1431 "Value is greater than SByte.MaxValue"));
1433 return (sbyte)value;
1436 [CLSCompliant (false)]
1437 public static sbyte ToSByte (DateTime value)
1439 throw new InvalidCastException ("This conversion is not supported.");
1442 [CLSCompliant (false)]
1443 public static sbyte ToSByte (decimal value)
1445 if (value > SByte.MaxValue || value < SByte.MinValue)
1446 throw new OverflowException (Locale.GetText (
1447 "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
1449 // Returned Even-Rounded
1450 return (sbyte)(Math.Round (value));
1453 [CLSCompliant (false)]
1454 public static sbyte ToSByte (double value)
1456 if (value > SByte.MaxValue || value < SByte.MinValue)
1457 throw new OverflowException (Locale.GetText (
1458 "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
1460 // Returned Even-Rounded
1461 return (sbyte)(Math.Round (value));
1464 [CLSCompliant (false)]
1465 public static sbyte ToSByte (float value)
1467 if (value > SByte.MaxValue || value < SByte.MinValue)
1468 throw new OverflowException (Locale.GetText (
1469 "Value is greater than SByte.MaxValue or less than SByte.Minalue"));
1471 // Returned Even-Rounded, pass as double to Math
1472 return (sbyte)(Math.Round ( (double)value));
1475 [CLSCompliant (false)]
1476 public static sbyte ToSByte (int value)
1478 if (value > SByte.MaxValue || value < SByte.MinValue)
1479 throw new OverflowException (Locale.GetText (
1480 "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
1482 return (sbyte)value;
1485 [CLSCompliant (false)]
1486 public static sbyte ToSByte (long value)
1488 if (value > SByte.MaxValue || value < SByte.MinValue)
1489 throw new OverflowException (Locale.GetText (
1490 "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
1492 return (sbyte)value;
1495 [CLSCompliant (false)]
1496 public static sbyte ToSByte (sbyte value)
1501 [CLSCompliant (false)]
1502 public static sbyte ToSByte (short value)
1504 if (value > SByte.MaxValue || value < SByte.MinValue)
1505 throw new OverflowException (Locale.GetText (
1506 "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
1508 return (sbyte)value;
1511 [CLSCompliant (false)]
1512 public static sbyte ToSByte (string value)
1515 return 0; // LAMESPEC: Spec says throw ArgumentNullException
1516 return SByte.Parse (value);
1519 [CLSCompliant (false)]
1520 public static sbyte ToSByte (string value, IFormatProvider provider)
1523 throw new ArgumentNullException ("value");
1524 return SByte.Parse (value, provider);
1527 [CLSCompliant (false)]
1528 public static sbyte ToSByte (string value, int fromBase)
1530 int result = ConvertFromBase (value, fromBase, false);
1531 if (fromBase != 10) {
1532 // note: no sign are available to detect negatives
1533 if (result > SByte.MaxValue) {
1534 // return negative 2's complement
1535 return Convert.ToSByte (-(256 - result));
1538 return Convert.ToSByte (result);
1541 [CLSCompliant (false)]
1542 public static sbyte ToSByte (uint value)
1544 if (value > SByte.MaxValue)
1545 throw new OverflowException (Locale.GetText (
1546 "Value is greater than SByte.MaxValue"));
1548 return (sbyte)value;
1551 [CLSCompliant (false)]
1552 public static sbyte ToSByte (ulong value)
1554 if (value > (ulong)SByte.MaxValue)
1555 throw new OverflowException (Locale.GetText (
1556 "Value is greater than SByte.MaxValue"));
1558 return (sbyte)value;
1561 [CLSCompliant (false)]
1562 public static sbyte ToSByte (ushort value)
1564 if (value > SByte.MaxValue)
1565 throw new OverflowException (Locale.GetText (
1566 "Value is greater than SByte.MaxValue"));
1568 return (sbyte)value;
1571 [CLSCompliant (false)]
1572 public static sbyte ToSByte (object value)
1576 return ToSByte (value, null);
1579 [CLSCompliant (false)]
1580 public static sbyte ToSByte (object value, IFormatProvider provider)
1584 return ((IConvertible) value).ToSByte (provider);
1587 // ========== Single Conversions ========== //
1589 public static float ToSingle (bool value)
1591 return value ? 1 : 0;
1594 public static float ToSingle (byte value)
1596 return (float)value;
1599 public static float ToSingle (Char value)
1601 throw new InvalidCastException ("This conversion is not supported.");
1604 public static float ToSingle (DateTime value)
1606 throw new InvalidCastException ("This conversion is not supported.");
1609 public static float ToSingle (decimal value)
1611 return (float)value;
1614 public static float ToSingle (double value)
1616 return (float)value;
1619 public static float ToSingle (float value)
1624 public static float ToSingle (int value)
1626 return (float)value;
1629 public static float ToSingle (long value)
1631 return (float)value;
1634 [CLSCompliant (false)]
1635 public static float ToSingle (sbyte value)
1637 return (float)value;
1640 public static float ToSingle (short value)
1642 return (float)value;
1645 public static float ToSingle (string value)
1648 return 0.0f; // LAMESPEC: Spec says throw ArgumentNullException
1649 return Single.Parse (value);
1652 public static float ToSingle (string value, IFormatProvider provider)
1655 return 0.0f; // LAMESPEC: Spec says throw ArgumentNullException
1656 return Single.Parse (value, provider);
1659 [CLSCompliant (false)]
1660 public static float ToSingle (uint value)
1662 return (float)value;
1665 [CLSCompliant (false)]
1666 public static float ToSingle (ulong value)
1668 return (float)value;
1671 [CLSCompliant (false)]
1672 public static float ToSingle (ushort value)
1674 return (float)value;
1677 public static float ToSingle (object value)
1681 return ToSingle (value, null);
1684 // [CLSCompliant (false)]
1685 public static float ToSingle (object value, IFormatProvider provider)
1689 return ((IConvertible) value).ToSingle (provider);
1692 // ========== String Conversions ========== //
1694 public static string ToString (bool value)
1696 return value.ToString ();
1699 public static string ToString (bool value, IFormatProvider provider)
1701 return value.ToString (); // the same as ToString (bool).
1704 public static string ToString (byte value)
1706 return value.ToString ();
1709 public static string ToString (byte value, IFormatProvider provider)
1711 return value.ToString (provider);
1714 public static string ToString (byte value, int toBase)
1719 return value.ToString ();
1721 byte[] val = BitConverter.GetBytes (value);
1725 return ConvertToBase2 (val);
1727 return ConvertToBase8 (val);
1729 return ConvertToBase16 (val);
1731 throw new ArgumentException (Locale.GetText ("toBase is not valid."));
1735 public static string ToString (char value)
1737 return value.ToString ();
1740 public static string ToString (char value, IFormatProvider provider)
1742 return value.ToString (); // the same as ToString (char)
1745 public static string ToString (DateTime value)
1747 return value.ToString ();
1750 public static string ToString (DateTime value, IFormatProvider provider)
1752 return value.ToString (provider);
1755 public static string ToString (decimal value)
1757 return value.ToString ();
1760 public static string ToString (decimal value, IFormatProvider provider)
1762 return value.ToString (provider);
1765 public static string ToString (double value)
1767 return value.ToString ();
1770 public static string ToString (double value, IFormatProvider provider)
1772 return value.ToString (provider);
1775 public static string ToString (float value)
1777 return value.ToString ();
1780 public static string ToString (float value, IFormatProvider provider)
1782 return value.ToString (provider);
1785 public static string ToString (int value)
1787 return value.ToString ();
1790 public static string ToString (int value, int toBase)
1795 return value.ToString ();
1797 byte[] val = BitConverter.GetBytes (value);
1801 return ConvertToBase2 (val);
1803 return ConvertToBase8 (val);
1805 return ConvertToBase16 (val);
1807 throw new ArgumentException (Locale.GetText ("toBase is not valid."));
1811 public static string ToString (int value, IFormatProvider provider)
1813 return value.ToString (provider);
1816 public static string ToString (long value)
1818 return value.ToString ();
1821 public static string ToString (long 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 (long value, IFormatProvider provider)
1844 return value.ToString (provider);
1847 public static string ToString (object value)
1849 return ToString (value, null);
1852 public static string ToString (object value, IFormatProvider provider)
1854 if (value is IConvertible)
1855 return ((IConvertible) value).ToString (provider);
1856 else if (value != null)
1857 return value.ToString ();
1858 return String.Empty;
1861 [CLSCompliant (false)]
1862 public static string ToString (sbyte value)
1864 return value.ToString ();
1867 [CLSCompliant (false)]
1868 public static string ToString (sbyte value, IFormatProvider provider)
1870 return value.ToString (provider);
1873 public static string ToString (short value)
1875 return value.ToString ();
1878 public static string ToString (short value, int toBase)
1883 return value.ToString ();
1885 byte[] val = BitConverter.GetBytes (value);
1889 return ConvertToBase2 (val);
1891 return ConvertToBase8 (val);
1893 return ConvertToBase16 (val);
1895 throw new ArgumentException (Locale.GetText ("toBase is not valid."));
1899 public static string ToString (short value, IFormatProvider provider)
1901 return value.ToString (provider);
1904 public static string ToString (string value)
1909 public static string ToString (string value, IFormatProvider provider)
1911 return value; // provider is ignored.
1914 [CLSCompliant (false)]
1915 public static string ToString (uint value)
1917 return value.ToString ();
1920 [CLSCompliant (false)]
1921 public static string ToString (uint value, IFormatProvider provider)
1923 return value.ToString (provider);
1926 [CLSCompliant (false)]
1927 public static string ToString (ulong value)
1929 return value.ToString ();
1932 [CLSCompliant (false)]
1933 public static string ToString (ulong value, IFormatProvider provider)
1935 return value.ToString (provider);
1938 [CLSCompliant (false)]
1939 public static string ToString (ushort value)
1941 return value.ToString ();
1944 [CLSCompliant (false)]
1945 public static string ToString (ushort value, IFormatProvider provider)
1947 return value.ToString (provider);
1950 // ========== UInt16 Conversions ========== //
1952 [CLSCompliant (false)]
1953 public static ushort ToUInt16 (bool value)
1955 return (ushort)(value ? 1 : 0);
1958 [CLSCompliant (false)]
1959 public static ushort ToUInt16 (byte value)
1961 return (ushort)value;
1964 [CLSCompliant (false)]
1965 public static ushort ToUInt16 (char value)
1967 return (ushort)value;
1970 [CLSCompliant (false)]
1971 public static ushort ToUInt16 (DateTime value)
1973 throw new InvalidCastException ("This conversion is not supported.");
1976 [CLSCompliant (false)]
1977 public static ushort ToUInt16 (decimal value)
1979 if (value > UInt16.MaxValue || value < UInt16.MinValue)
1980 throw new OverflowException (Locale.GetText (
1981 "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
1983 // Returned Even-Rounded
1984 return (ushort)(Math.Round (value));
1987 [CLSCompliant (false)]
1988 public static ushort ToUInt16 (double value)
1990 if (value > UInt16.MaxValue || value < UInt16.MinValue)
1991 throw new OverflowException (Locale.GetText (
1992 "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
1994 // Returned Even-Rounded
1995 return (ushort)(Math.Round (value));
1998 [CLSCompliant (false)]
1999 public static ushort ToUInt16 (float value)
2001 if (value > UInt16.MaxValue || value < UInt16.MinValue)
2002 throw new OverflowException (Locale.GetText (
2003 "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
2005 // Returned Even-Rounded, pass as double to Math
2006 return (ushort)(Math.Round ( (double)value));
2009 [CLSCompliant (false)]
2010 public static ushort ToUInt16 (int value)
2012 if (value > UInt16.MaxValue || value < UInt16.MinValue)
2013 throw new OverflowException (Locale.GetText (
2014 "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
2016 return (ushort)value;
2019 [CLSCompliant (false)]
2020 public static ushort ToUInt16 (long value)
2022 if (value > UInt16.MaxValue || value < UInt16.MinValue)
2023 throw new OverflowException (Locale.GetText (
2024 "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
2026 return (ushort)value;
2029 [CLSCompliant (false)]
2030 public static ushort ToUInt16 (sbyte value)
2032 if (value < UInt16.MinValue)
2033 throw new OverflowException (Locale.GetText (
2034 "Value is less than UInt16.MinValue"));
2036 return (ushort)value;
2039 [CLSCompliant (false)]
2040 public static ushort ToUInt16 (short value)
2042 if (value < UInt16.MinValue)
2043 throw new OverflowException (Locale.GetText (
2044 "Value is less than UInt16.MinValue"));
2046 return (ushort)value;
2049 [CLSCompliant (false)]
2050 public static ushort ToUInt16 (string value)
2053 return 0; // LAMESPEC: Spec says throw ArgumentNullException
2054 return UInt16.Parse (value);
2057 [CLSCompliant (false)]
2058 public static ushort ToUInt16 (string value, IFormatProvider provider)
2061 return 0; // LAMESPEC: Spec says throw ArgumentNullException
2062 return UInt16.Parse (value, provider);
2065 [CLSCompliant (false)]
2066 public static ushort ToUInt16 (string value, int fromBase)
2068 return ToUInt16 (ConvertFromBase (value, fromBase, true));
2071 [CLSCompliant (false)]
2072 public static ushort ToUInt16 (uint value)
2074 if (value > UInt16.MaxValue)
2075 throw new OverflowException (Locale.GetText (
2076 "Value is greater than UInt16.MaxValue"));
2078 return (ushort)value;
2081 [CLSCompliant (false)]
2082 public static ushort ToUInt16 (ulong value)
2084 if (value > (ulong)UInt16.MaxValue)
2085 throw new OverflowException (Locale.GetText (
2086 "Value is greater than UInt16.MaxValue"));
2088 return (ushort)value;
2091 [CLSCompliant (false)]
2092 public static ushort ToUInt16 (ushort value)
2097 [CLSCompliant (false)]
2098 public static ushort ToUInt16 (object value)
2102 return ToUInt16 (value, null);
2105 [CLSCompliant (false)]
2106 public static ushort ToUInt16 (object value, IFormatProvider provider)
2110 return ((IConvertible) value).ToUInt16 (provider);
2113 // ========== UInt32 Conversions ========== //
2115 [CLSCompliant (false)]
2116 public static uint ToUInt32 (bool value)
2118 return (uint)(value ? 1 : 0);
2121 [CLSCompliant (false)]
2122 public static uint ToUInt32 (byte value)
2127 [CLSCompliant (false)]
2128 public static uint ToUInt32 (char value)
2133 [CLSCompliant (false)]
2134 public static uint ToUInt32 (DateTime value)
2136 throw new InvalidCastException ("This conversion is not supported.");
2139 [CLSCompliant (false)]
2140 public static uint ToUInt32 (decimal value)
2142 if (value > UInt32.MaxValue || value < UInt32.MinValue)
2143 throw new OverflowException (Locale.GetText (
2144 "Value is greater than UInt32.MaxValue or less than UInt32.MinValue"));
2146 // Returned Even-Rounded
2147 return (uint)(Math.Round (value));
2150 [CLSCompliant (false)]
2151 public static uint ToUInt32 (double value)
2153 if (value > UInt32.MaxValue || value < UInt32.MinValue)
2154 throw new OverflowException (Locale.GetText (
2155 "Value is greater than UInt32.MaxValue or less than UInt32.MinValue"));
2157 // Returned Even-Rounded
2158 return (uint)(Math.Round (value));
2161 [CLSCompliant (false)]
2162 public static uint ToUInt32 (float value)
2164 if (value > UInt32.MaxValue || value < UInt32.MinValue)
2165 throw new OverflowException (Locale.GetText (
2166 "Value is greater than UInt32.MaxValue or less than UInt32.MinValue"));
2168 // Returned Even-Rounded, pass as double to Math
2169 return (uint)(Math.Round ( (double)value));
2172 [CLSCompliant (false)]
2173 public static uint ToUInt32 (int value)
2175 if (value < UInt32.MinValue)
2176 throw new OverflowException (Locale.GetText (
2177 "Value is less than UInt32.MinValue"));
2182 [CLSCompliant (false)]
2183 public static uint ToUInt32 (long value)
2185 if (value > UInt32.MaxValue || value < UInt32.MinValue)
2186 throw new OverflowException (Locale.GetText (
2187 "Value is greater than UInt32.MaxValue or less than UInt32.MinValue"));
2192 [CLSCompliant (false)]
2193 public static uint ToUInt32 (sbyte value)
2195 if (value < UInt32.MinValue)
2196 throw new OverflowException (Locale.GetText (
2197 "Value is less than UInt32.MinValue"));
2202 [CLSCompliant (false)]
2203 public static uint ToUInt32 (short value)
2205 if (value < UInt32.MinValue)
2206 throw new OverflowException (Locale.GetText (
2207 "Value is less than UInt32.MinValue"));
2212 [CLSCompliant (false)]
2213 public static uint ToUInt32 (string value)
2216 return 0; // LAMESPEC: Spec says throw ArgumentNullException
2217 return UInt32.Parse (value);
2220 [CLSCompliant (false)]
2221 public static uint ToUInt32 (string value, IFormatProvider provider)
2224 return 0; // LAMESPEC: Spec says throw ArgumentNullException
2225 return UInt32.Parse (value, provider);
2228 [CLSCompliant (false)]
2229 public static uint ToUInt32 (string value, int fromBase)
2231 return (uint) ConvertFromBase (value, fromBase, true);
2234 [CLSCompliant (false)]
2235 public static uint ToUInt32 (uint value)
2240 [CLSCompliant (false)]
2241 public static uint ToUInt32 (ulong value)
2243 if (value > UInt32.MaxValue)
2244 throw new OverflowException (Locale.GetText (
2245 "Value is greater than UInt32.MaxValue"));
2250 [CLSCompliant (false)]
2251 public static uint ToUInt32 (ushort value)
2256 [CLSCompliant (false)]
2257 public static uint ToUInt32 (object value)
2261 return ToUInt32 (value, null);
2264 [CLSCompliant (false)]
2265 public static uint ToUInt32 (object value, IFormatProvider provider)
2269 return ((IConvertible) value).ToUInt32 (provider);
2273 // ========== UInt64 Conversions ========== //
2275 [CLSCompliant (false)]
2276 public static ulong ToUInt64 (bool value)
2278 return (ulong)(value ? 1 : 0);
2281 [CLSCompliant (false)]
2282 public static ulong ToUInt64 (byte value)
2284 return (ulong)value;
2287 [CLSCompliant (false)]
2288 public static ulong ToUInt64 (char value)
2290 return (ulong)value;
2293 [CLSCompliant (false)]
2294 public static ulong ToUInt64 (DateTime value)
2296 throw new InvalidCastException ("The conversion is not supported.");
2299 [CLSCompliant (false)]
2300 public static ulong ToUInt64 (decimal value)
2302 if (value > UInt64.MaxValue || value < UInt64.MinValue)
2303 throw new OverflowException (Locale.GetText (
2304 "Value is greater than UInt64.MaxValue or less than UInt64.MinValue"));
2306 // Returned Even-Rounded
2307 return (ulong)(Math.Round (value));
2310 [CLSCompliant (false)]
2311 public static ulong ToUInt64 (double value)
2313 if (value > UInt64.MaxValue || value < UInt64.MinValue)
2314 throw new OverflowException (Locale.GetText (
2315 "Value is greater than UInt64.MaxValue or less than UInt64.MinValue"));
2317 // Returned Even-Rounded
2318 return (ulong)(Math.Round (value));
2321 [CLSCompliant (false)]
2322 public static ulong ToUInt64 (float value)
2324 if (value > UInt64.MaxValue || value < UInt64.MinValue)
2325 throw new OverflowException (Locale.GetText (
2326 "Value is greater than UInt64.MaxValue or less than UInt64.MinValue"));
2328 // Returned Even-Rounded, pass as a double to Math
2329 return (ulong)(Math.Round ( (double)value));
2332 [CLSCompliant (false)]
2333 public static ulong ToUInt64 (int value)
2335 if (value < (int)UInt64.MinValue)
2336 throw new OverflowException (Locale.GetText (
2337 "Value is less than UInt64.MinValue"));
2339 return (ulong)value;
2342 [CLSCompliant (false)]
2343 public static ulong ToUInt64 (long value)
2345 if (value < (long)UInt64.MinValue)
2346 throw new OverflowException (Locale.GetText (
2347 "Value is less than UInt64.MinValue"));
2349 return (ulong)value;
2352 [CLSCompliant (false)]
2353 public static ulong ToUInt64 (sbyte value)
2355 if (value < (sbyte)UInt64.MinValue)
2356 throw new OverflowException
2357 ("Value is less than UInt64.MinValue");
2359 return (ulong)value;
2362 [CLSCompliant (false)]
2363 public static ulong ToUInt64 (short value)
2365 if (value < (short)UInt64.MinValue)
2366 throw new OverflowException (Locale.GetText (
2367 "Value is less than UInt64.MinValue"));
2369 return (ulong)value;
2372 [CLSCompliant (false)]
2373 public static ulong ToUInt64 (string value)
2376 return 0; // LAMESPEC: Spec says throw ArgumentNullException
2377 return UInt64.Parse (value);
2380 [CLSCompliant (false)]
2381 public static ulong ToUInt64 (string value, IFormatProvider provider)
2384 return 0; // LAMESPEC: Spec says throw ArgumentNullException
2385 return UInt64.Parse (value, provider);
2388 [CLSCompliant (false)]
2389 public static ulong ToUInt64 (string value, int fromBase)
2391 return (ulong) ConvertFromBase64 (value, fromBase, true);
2394 [CLSCompliant (false)]
2395 public static ulong ToUInt64 (uint value)
2397 return (ulong)value;
2400 [CLSCompliant (false)]
2401 public static ulong ToUInt64 (ulong value)
2406 [CLSCompliant (false)]
2407 public static ulong ToUInt64 (ushort value)
2409 return (ulong)value;
2412 [CLSCompliant (false)]
2413 public static ulong ToUInt64 (object value)
2417 return ToUInt64 (value, null);
2420 [CLSCompliant (false)]
2421 public static ulong ToUInt64 (object value, IFormatProvider provider)
2425 return ((IConvertible) value).ToUInt64 (provider);
2429 // ========== Conversion / Helper Functions ========== //
2431 public static object ChangeType (object value, Type conversionType)
2433 if ((value != null) && (conversionType == null))
2434 throw new ArgumentNullException ("conversionType");
2435 CultureInfo ci = CultureInfo.CurrentCulture;
2436 IFormatProvider provider;
2437 if (conversionType == typeof(DateTime)) {
2438 provider = ci.DateTimeFormat;
2441 provider = ci.NumberFormat;
2443 return ToType (value, conversionType, provider);
2446 public static object ChangeType (object value, TypeCode typeCode)
2448 CultureInfo ci = CultureInfo.CurrentCulture;
2449 Type conversionType = conversionTable [(int) typeCode];
2450 IFormatProvider provider;
2451 if (conversionType == typeof(DateTime)) {
2452 provider = ci.DateTimeFormat;
2455 provider = ci.NumberFormat;
2457 return ToType (value, conversionType, provider);
2460 public static object ChangeType (object value, Type conversionType, IFormatProvider provider)
2462 if ((value != null) && (conversionType == null))
2463 throw new ArgumentNullException ("conversionType");
2464 return ToType (value, conversionType, provider);
2467 public static object ChangeType (object value, TypeCode typeCode, IFormatProvider provider)
2469 Type conversionType = conversionTable [(int)typeCode];
2470 return ToType (value, conversionType, provider);
2473 private static bool NotValidBase (int value)
2475 if ((value == 2) || (value == 8) ||
2476 (value == 10) || (value == 16))
2482 private static int ConvertFromBase (string value, int fromBase, bool unsigned)
2484 if (NotValidBase (fromBase))
2485 throw new ArgumentException ("fromBase is not valid.");
2494 int len = value.Length;
2495 bool negative = false;
2497 // special processing for some bases
2500 if (value.Substring (i, 1) == "-") {
2502 throw new OverflowException (
2503 Locale.GetText ("The string was being parsed as"
2504 + " an unsigned number and could not have a"
2505 + " negative sign."));
2512 if (value.Substring (i, 1) == "-") {
2513 throw new ArgumentException ("String cannot contain a "
2514 + "minus sign if the base is not 10.");
2518 if ((value[i] == '0') && ((value [i+1] == 'x') || (value [i+1] == 'X'))) {
2524 if (value.Substring (i, 1) == "-") {
2525 throw new ArgumentException ("String cannot contain a "
2526 + "minus sign if the base is not 10.");
2532 throw new FormatException ("Could not find any parsable digits.");
2535 if (value[i] == '+') {
2540 char c = value[i++];
2541 if (Char.IsNumber (c)) {
2542 digitValue = c - '0';
2543 } else if (Char.IsLetter (c)) {
2544 digitValue = Char.ToLowerInvariant (c) - 'a' + 10;
2547 throw new FormatException ("Additional unparsable "
2548 + "characters are at the end of the string.");
2550 throw new FormatException ("Could not find any parsable"
2555 if (digitValue >= fromBase) {
2557 throw new FormatException ("Additional unparsable "
2558 + "characters are at the end of the string.");
2560 throw new FormatException ("Could not find any parsable"
2565 result = (fromBase) * result + digitValue;
2570 throw new FormatException ("Could not find any parsable digits.");
2578 // note: this has nothing to do with base64 encoding (just base and Int64)
2579 private static long ConvertFromBase64 (string value, int fromBase, bool unsigned)
2581 if (NotValidBase (fromBase))
2582 throw new ArgumentException ("fromBase is not valid.");
2587 int digitValue = -1;
2589 bool negative = false;
2592 int len = value.Length;
2594 // special processing for some bases
2597 if (value.Substring(i, 1) == "-") {
2599 throw new OverflowException (
2600 Locale.GetText ("The string was being parsed as"
2601 + " an unsigned number and could not have a"
2602 + " negative sign."));
2609 if (value.Substring (i, 1) == "-") {
2610 throw new ArgumentException ("String cannot contain a "
2611 + "minus sign if the base is not 10.");
2615 if ((value[i] == '0') && ((value[i + 1] == 'x') || (value[i + 1] == 'X'))) {
2621 if (value.Substring (i, 1) == "-") {
2622 throw new ArgumentException ("String cannot contain a "
2623 + "minus sign if the base is not 10.");
2629 throw new FormatException ("Could not find any parsable digits.");
2632 if (value[i] == '+') {
2637 char c = value[i++];
2638 if (Char.IsNumber (c)) {
2639 digitValue = c - '0';
2640 } else if (Char.IsLetter (c)) {
2641 digitValue = Char.ToLowerInvariant (c) - 'a' + 10;
2644 throw new FormatException ("Additional unparsable "
2645 + "characters are at the end of the string.");
2647 throw new FormatException ("Could not find any parsable"
2652 if (digitValue >= fromBase) {
2654 throw new FormatException ("Additional unparsable "
2655 + "characters are at the end of the string.");
2657 throw new FormatException ("Could not find any parsable"
2662 result = (fromBase * result + digitValue);
2667 throw new FormatException ("Could not find any parsable digits.");
2675 private static void EndianSwap (ref byte[] value)
2677 byte[] buf = new byte[value.Length];
2678 for (int i = 0; i < value.Length; i++)
2679 buf[i] = value[value.Length-1-i];
2683 private static string ConvertToBase2 (byte[] value)
2685 if (!BitConverter.IsLittleEndian)
2686 EndianSwap (ref value);
2687 StringBuilder sb = new StringBuilder ();
2688 for (int i = value.Length - 1; i >= 0; i--) {
2690 for (int j = 0; j < 8; j++) {
2691 if ((b & 0x80) == 0x80) {
2701 return sb.ToString ();
2704 private static string ConvertToBase8 (byte[] value)
2707 switch (value.Length) {
2709 l = (ulong) value [0];
2712 l = (ulong) BitConverter.ToUInt16 (value, 0);
2715 l = (ulong) BitConverter.ToUInt32 (value, 0);
2718 l = BitConverter.ToUInt64 (value, 0);
2721 throw new ArgumentException ("value");
2724 StringBuilder sb = new StringBuilder ();
2725 for (int i = 21; i >= 0; i--) {
2726 // 3 bits at the time
2727 char val = (char) ((l >> i * 3) & 0x7);
2728 if ((val != 0) || (sb.Length > 0)) {
2733 return sb.ToString ();
2736 private static string ConvertToBase16 (byte[] value)
2738 if (!BitConverter.IsLittleEndian)
2739 EndianSwap (ref value);
2740 StringBuilder sb = new StringBuilder ();
2741 for (int i = value.Length - 1; i >= 0; i--) {
2742 char high = (char)((value[i] >> 4) & 0x0f);
2743 if ((high != 0) || (sb.Length > 0)) {
2753 char low = (char)(value[i] & 0x0f);
2754 if ((low != 0) || (sb.Length > 0)) {
2764 return sb.ToString ();
2767 // Lookup table for the conversion ToType method. Order
2768 // is important! Used by ToType for comparing the target
2769 // type, and uses hardcoded array indexes.
2770 private static readonly Type[] conversionTable = {
2771 // Valid ICovnertible Types
2773 typeof (object), // 1 TypeCode.Object
2774 typeof (DBNull), // 2 TypeCode.DBNull
2775 typeof (Boolean), // 3 TypeCode.Boolean
2776 typeof (Char), // 4 TypeCode.Char
2777 typeof (SByte), // 5 TypeCode.SByte
2778 typeof (Byte), // 6 TypeCode.Byte
2779 typeof (Int16), // 7 TypeCode.Int16
2780 typeof (UInt16), // 8 TypeCode.UInt16
2781 typeof (Int32), // 9 TypeCode.Int32
2782 typeof (UInt32), // 10 TypeCode.UInt32
2783 typeof (Int64), // 11 TypeCode.Int64
2784 typeof (UInt64), // 12 TypeCode.UInt64
2785 typeof (Single), // 13 TypeCode.Single
2786 typeof (Double), // 14 TypeCode.Double
2787 typeof (Decimal), // 15 TypeCode.Decimal
2788 typeof (DateTime), // 16 TypeCode.DateTime
2790 typeof (String), // 18 TypeCode.String
2793 // Function to convert an object to another type and return
2794 // it as an object. In place for the core data types to use
2795 // when implementing IConvertible. Uses hardcoded indexes in
2796 // the conversionTypes array, so if modify carefully.
2797 internal static object ToType (object value, Type conversionType,
2798 IFormatProvider provider)
2800 if (value == null) {
2801 if ((conversionType != null) && conversionType.IsValueType){
2803 throw new InvalidCastException ("Null object can not be converted to a value type.");
2806 // Bug compatibility with 1.0
2808 throw new NullReferenceException ("Null object can not be converted to a value type.");
2814 if (conversionType == null)
2815 throw new InvalidCastException ("Cannot cast to destination type.");
2817 if (value.GetType () == conversionType)
2820 if (value is IConvertible) {
2821 IConvertible convertValue = (IConvertible) value;
2823 if (conversionType == conversionTable[0]) // 0 Empty
2824 throw new ArgumentNullException ();
2826 else if (conversionType == conversionTable[1]) // 1 TypeCode.Object
2827 return (object) value;
2829 else if (conversionType == conversionTable[2]) // 2 TypeCode.DBNull
2830 throw new InvalidCastException (
2831 "Cannot cast to DBNull, it's not IConvertible");
2833 else if (conversionType == conversionTable[3]) // 3 TypeCode.Boolean
2834 return (object) convertValue.ToBoolean (provider);
2836 else if (conversionType == conversionTable[4]) // 4 TypeCode.Char
2837 return (object) convertValue.ToChar (provider);
2839 else if (conversionType == conversionTable[5]) // 5 TypeCode.SByte
2840 return (object) convertValue.ToSByte (provider);
2842 else if (conversionType == conversionTable[6]) // 6 TypeCode.Byte
2843 return (object) convertValue.ToByte (provider);
2845 else if (conversionType == conversionTable[7]) // 7 TypeCode.Int16
2846 return (object) convertValue.ToInt16 (provider);
2848 else if (conversionType == conversionTable[8]) // 8 TypeCode.UInt16
2849 return (object) convertValue.ToUInt16 (provider);
2851 else if (conversionType == conversionTable[9]) // 9 TypeCode.Int32
2852 return (object) convertValue.ToInt32 (provider);
2854 else if (conversionType == conversionTable[10]) // 10 TypeCode.UInt32
2855 return (object) convertValue.ToUInt32 (provider);
2857 else if (conversionType == conversionTable[11]) // 11 TypeCode.Int64
2858 return (object) convertValue.ToInt64 (provider);
2860 else if (conversionType == conversionTable[12]) // 12 TypeCode.UInt64
2861 return (object) convertValue.ToUInt64 (provider);
2863 else if (conversionType == conversionTable[13]) // 13 TypeCode.Single
2864 return (object) convertValue.ToSingle (provider);
2866 else if (conversionType == conversionTable[14]) // 14 TypeCode.Double
2867 return (object) convertValue.ToDouble (provider);
2869 else if (conversionType == conversionTable[15]) // 15 TypeCode.Decimal
2870 return (object) convertValue.ToDecimal (provider);
2872 else if (conversionType == conversionTable[16]) // 16 TypeCode.DateTime
2873 return (object) convertValue.ToDateTime (provider);
2875 else if (conversionType == conversionTable[18]) // 18 TypeCode.String
2876 return (object) convertValue.ToString (provider);
2878 throw new InvalidCastException (Locale.GetText ("Unknown target conversion type"));
2881 // Not in the conversion table
2882 throw new InvalidCastException ((Locale.GetText (
2883 "Value is not a convertible object: " + value.GetType().ToString() + " to " + conversionType.FullName)));