5 // Derek Holden (dholden@draper.com)
\r
6 // Duncan Mak (duncan@ximian.com)
\r
8 // (C) Ximian, Inc. http://www.ximian.com
\r
12 // System.Convert class. This was written word for word off the
\r
13 // Library specification for System.Convert in the ECMA TC39 TG2
\r
14 // and TG3 working documents. The first page of which has a table
\r
15 // for all legal conversion scenerios.
\r
17 // This header and the one above it can be formatted however, just trying
\r
18 // to keep it consistent w/ the existing mcs headers.
\r
20 // This Convert class could be written another way, with each type
\r
21 // implementing IConvertible and defining their own conversion functions,
\r
22 // and this class just calling the type's implementation. Or, they can
\r
23 // be defined here and the implementing type can use these functions when
\r
24 // defining their IConvertible interface. Byte's ToBoolean() calls
\r
25 // Convert.ToBoolean(byte), or Convert.ToBoolean(byte) calls
\r
26 // byte.ToBoolean(). The first case is what is done here.
\r
28 // See http://lists.ximian.com/archives/public/mono-list/2001-July/000525.html
\r
30 // There are also conversion functions that are not defined in
\r
31 // the ECMA draft, such as there is no bool ToBoolean(DateTime value),
\r
32 // and placing that somewhere won't compile w/ this Convert since the
\r
33 // function doesn't exist. However calling that when using Microsoft's
\r
34 // System.Convert doesn't produce any compiler errors, it just throws
\r
35 // an InvalidCastException at runtime.
\r
37 // Whenever a decimal, double, or single is converted to an integer
\r
38 // based type, it is even rounded. This uses Math.Round which only
\r
39 // has Round(decimal) and Round(double), so in the Convert from
\r
40 // single cases the value is passed to Math as a double. This
\r
41 // may not be completely necessary.
\r
43 // The .NET Framework SDK lists DBNull as a member of this class
\r
44 // as 'public static readonly object DBNull;'.
\r
46 // It should also be decided if all the cast return values should be
\r
47 // returned as unchecked or not.
\r
49 // All the XML function comments were auto generated which is why they
\r
50 // sound someone redundant.
\r
52 // TYPE | BOOL BYTE CHAR DT DEC DBL I16 I32 I64 SBYT SNGL STR UI16 UI32 UI64
\r
53 // -----+--------------------------------------------------------------------
\r
54 // BOOL | X X X X X X X X X X X X X
\r
55 // BYTE | X X X X X X X X X X X X X X
\r
56 // CHAR | X X X X X X X X X X
\r
58 // DEC | X X X X X X X X X X X X X
\r
59 // DBL | X X X X X X X X X X X X X
\r
60 // I16 | X X X X X X X X X X X X X X
\r
61 // I32 | X X X X X X X X X X X X X X
\r
62 // I64 | X X X X X X X X X X X X X X
\r
63 // SBYT | X X X X X X X X X X X X X X
\r
64 // SNGL | X X X X X X X X X X X X X
\r
65 // STR | X X X X X X X X X X X X X X X
\r
66 // UI16 | X X X X X X X X X X X X X X
\r
67 // UI32 | X X X X X X X X X X X X X X
\r
68 // UI64 | X X X X X X X X X X X X X X
\r
71 using System.Globalization;
\r
72 using System.Security.Cryptography;
\r
77 // [CLSCompliant(false)]
\r
78 public sealed class Convert {
\r
81 public static readonly object DBNull = System.DBNull.Value;
\r
83 private Convert () {}
\r
85 // ========== BASE 64 Conversions ========== //
\r
86 // the BASE64 convert methods are using the Base64 converting methods
\r
87 // from System.Security.Cryptography.ToBase64Transform and
\r
88 // System.Security.Cryptography.FromBase64Transform
\r
90 // should be changed to a stand-alone class Base64Encoder & Base64Decoder
\r
92 public static byte[] FromBase64CharArray(char[] inArray, int offset, int length)
\r
94 if (inArray == null)
\r
95 throw new ArgumentNullException();
\r
97 if ((offset < 0) || (length < 0) || (offset + length > inArray.Length))
\r
98 throw new ArgumentOutOfRangeException();
\r
100 if (length < 4 || length % 4 != 0)
\r
101 throw new FormatException();
\r
103 byte[] inArr = new System.Text.UTF8Encoding().GetBytes(inArray, offset, length);
\r
104 FromBase64Transform t = new FromBase64Transform();
\r
106 return t.TransformFinalBlock(inArr, 0, inArr.Length);
\r
109 public static byte[] FromBase64String(string s)
\r
112 throw new ArgumentNullException();
\r
114 char[] inArr = s.ToCharArray();
\r
116 return FromBase64CharArray(inArr, 0, inArr.Length);
\r
119 public static TypeCode GetTypeCode (object value)
\r
122 return TypeCode.Empty;
\r
124 return Type.GetTypeCode (value.GetType ());
\r
127 public static bool IsDBNull (object value)
\r
129 if (value is DBNull)
\r
135 public static int ToBase64CharArray(byte[] inArray, int offsetIn, int length,
\r
136 char[] outArray, int offsetOut)
\r
138 if (inArray == null || outArray == null)
\r
139 throw new ArgumentNullException();
\r
141 if (offsetIn < 0 || length < 0 || offsetOut < 0 || (offsetIn + length) > inArray.Length)
\r
142 throw new ArgumentOutOfRangeException();
\r
144 ToBase64Transform t = new ToBase64Transform();
\r
145 byte[] outArr = t.TransformFinalBlock(inArray, offsetIn, length);
\r
147 char[] cOutArr = new System.Text.ASCIIEncoding().GetChars(outArr);
\r
149 if ((offsetOut + cOutArr.Length) > outArray.Length)
\r
150 throw new ArgumentOutOfRangeException();
\r
152 Array.Copy(cOutArr, 0, outArray, offsetOut, cOutArr.Length);
\r
154 return cOutArr.Length;
\r
157 public static string ToBase64String(byte[] inArray)
\r
159 if (inArray == null)
\r
160 throw new ArgumentNullException();
\r
162 return ToBase64String(inArray, 0, inArray.Length);
\r
165 public static string ToBase64String(byte[] inArray, int offset, int length)
\r
167 if (inArray == null)
\r
168 throw new ArgumentNullException();
\r
170 if (offset < 0 || length < 0 || (offset + length) > inArray.Length)
\r
171 throw new ArgumentOutOfRangeException();
\r
173 // FIXME: change to stand alone Base64 Encoder class
\r
174 ToBase64Transform t = new ToBase64Transform();
\r
175 byte[] outArr = t.TransformFinalBlock(inArray, offset, length);
\r
177 return (new System.Text.ASCIIEncoding().GetString(outArr));
\r
180 // ========== Boolean Conversions ========== //
\r
182 public static bool ToBoolean (bool value)
\r
187 public static bool ToBoolean (byte value)
\r
189 return (value != 0);
\r
192 public static bool ToBoolean (char value)
\r
194 throw new InvalidCastException (Locale.GetText ("Can't convert char to bool"));
\r
197 public static bool ToBoolean (DateTime value)
\r
199 throw new InvalidCastException (Locale.GetText ("Can't convert date to bool"));
\r
202 public static bool ToBoolean (decimal value)
\r
204 return (value != 0M);
\r
207 public static bool ToBoolean (double value)
\r
209 return (value != 0);
\r
212 public static bool ToBoolean (float value)
\r
214 return (value != 0f);
\r
217 public static bool ToBoolean (int value)
\r
219 return (value != 0);
\r
222 public static bool ToBoolean (long value)
\r
224 return (value != 0);
\r
227 [CLSCompliant (false)]
\r
228 public static bool ToBoolean (sbyte value)
\r
230 return (value != 0);
\r
233 public static bool ToBoolean (short value)
\r
235 return (value != 0);
\r
238 public static bool ToBoolean (string value)
\r
240 return Boolean.Parse (value);
\r
243 public static bool ToBoolean (string value, IFormatProvider provider)
\r
245 return Boolean.Parse (value); // provider is ignored.
\r
248 [CLSCompliant (false)]
\r
249 public static bool ToBoolean (uint value)
\r
251 return (value != 0);
\r
254 [CLSCompliant (false)]
\r
255 public static bool ToBoolean (ulong value)
\r
257 return (value != 0);
\r
260 [CLSCompliant (false)]
\r
261 public static bool ToBoolean (ushort value)
\r
263 return (value != 0);
\r
266 public static bool ToBoolean (object value)
\r
268 return ToBoolean (value, null);
\r
271 public static bool ToBoolean (object value, IFormatProvider provider)
\r
275 return ((IConvertible) value).ToBoolean (provider);
\r
278 // ========== Byte Conversions ========== //
\r
280 public static byte ToByte (bool value)
\r
282 return (byte)(value ? 1 : 0);
\r
285 public static byte ToByte (byte value)
\r
290 public static byte ToByte (char value)
\r
292 if (value > Byte.MaxValue)
\r
293 throw new OverflowException (Locale.GetText (
\r
294 "Value is greater than Byte.MaxValue"));
\r
296 return (byte)value;
\r
299 public static byte ToByte (DateTime value)
\r
301 throw new InvalidCastException ("This conversion is not supported.");
\r
304 public static byte ToByte (decimal value)
\r
306 if (value > Byte.MaxValue || value < Byte.MinValue)
\r
307 throw new OverflowException (Locale.GetText (
\r
308 "Value is greater than Byte.MaxValue or less than Byte.MinValue"));
\r
310 // Returned Even-Rounded
\r
311 return (byte)(Math.Round (value));
\r
314 public static byte ToByte (double value)
\r
316 if (value > Byte.MaxValue || value < Byte.MinValue)
\r
317 throw new OverflowException (Locale.GetText (
\r
318 "Value is greater than Byte.MaxValue or less than Byte.MinValue"));
\r
320 // This and the float version of ToByte are the only ones
\r
321 // the spec listed as checking for .NaN and Infinity overflow
\r
322 if (Double.IsNaN(value) || Double.IsInfinity(value))
\r
323 throw new OverflowException (Locale.GetText (
\r
324 "Value is equal to Double.NaN, Double.PositiveInfinity, or Double.NegativeInfinity"));
\r
326 // Returned Even-Rounded
\r
327 return (byte)(Math.Round (value));
\r
330 public static byte ToByte (float value)
\r
332 if (value > Byte.MaxValue || value < Byte.MinValue)
\r
333 throw new OverflowException (Locale.GetText (
\r
334 "Value is greater than Byte.MaxValue or less than Byte.Minalue"));
\r
336 // This and the double version of ToByte are the only ones
\r
337 // the spec listed as checking for .NaN and Infinity overflow
\r
338 if (Single.IsNaN(value) || Single.IsInfinity(value))
\r
339 throw new OverflowException (Locale.GetText (
\r
340 "Value is equal to Single.NaN, Single.PositiveInfinity, or Single.NegativeInfinity"));
\r
342 // Returned Even-Rounded, pass it as a double, could have this
\r
343 // method just call Convert.ToByte ( (double)value)
\r
344 return (byte)(Math.Round ( (double)value));
\r
347 public static byte ToByte (int value)
\r
349 if (value > Byte.MaxValue || value < Byte.MinValue)
\r
350 throw new OverflowException (Locale.GetText (
\r
351 "Value is greater than Byte.MaxValue or less than Byte.MinValue"));
\r
353 return (byte)value;
\r
356 public static byte ToByte (long value)
\r
358 if (value > Byte.MaxValue || value < Byte.MinValue)
\r
359 throw new OverflowException (Locale.GetText (
\r
360 "Value is greater than Byte.MaxValue or less than Byte.MinValue"));
\r
362 return (byte)value;
\r
365 [CLSCompliant (false)]
\r
366 public static byte ToByte (sbyte value)
\r
368 if (value < Byte.MinValue)
\r
369 throw new OverflowException (Locale.GetText (
\r
370 "Value is less than Byte.MinValue"));
\r
372 return (byte)value;
\r
375 public static byte ToByte (short value)
\r
377 if (value > Byte.MaxValue || value < Byte.MinValue)
\r
378 throw new OverflowException (Locale.GetText (
\r
379 "Value is greater than Byte.MaxValue or less than Byte.MinValue"));
\r
381 return (byte)value;
\r
384 public static byte ToByte (string value)
\r
386 return Byte.Parse (value);
\r
389 public static byte ToByte (string value, IFormatProvider provider)
\r
391 return Byte.Parse (value, provider);
\r
394 public static byte ToByte (string value, int fromBase)
\r
397 int retVal = ConvertFromBase (value, fromBase);
\r
399 if (retVal < (int) Byte.MinValue || retVal > (int) Byte.MaxValue)
\r
400 throw new OverflowException ();
\r
402 return (byte) retVal;
\r
405 [CLSCompliant (false)]
\r
406 public static byte ToByte (uint value)
\r
408 if (value > Byte.MaxValue)
\r
409 throw new OverflowException (Locale.GetText (
\r
410 "Value is greater than Byte.MaxValue"));
\r
412 return (byte)value;
\r
415 [CLSCompliant (false)]
\r
416 public static byte ToByte (ulong value)
\r
418 if (value > Byte.MaxValue)
\r
419 throw new OverflowException (Locale.GetText (
\r
420 "Value is greater than Byte.MaxValue"));
\r
422 return (byte)value;
\r
425 [CLSCompliant (false)]
\r
426 public static byte ToByte (ushort value)
\r
428 if (value > Byte.MaxValue)
\r
429 throw new OverflowException (Locale.GetText (
\r
430 "Value is greater than Byte.MaxValue"));
\r
432 return (byte)value;
\r
435 public static byte ToByte (object value)
\r
437 return ToByte (value, null);
\r
440 public static byte ToByte (object value, IFormatProvider provider)
\r
444 return ((IConvertible) value).ToByte (provider);
\r
447 // ========== Char Conversions ========== //
\r
449 public static char ToChar (bool value)
\r
451 throw new InvalidCastException ("This conversion is not supported.");
\r
454 public static char ToChar (byte value)
\r
456 return (char)value;
\r
459 public static char ToChar (char value)
\r
464 public static char ToChar (DateTime value)
\r
466 throw new InvalidCastException ("This conversion is not supported.");
\r
469 public static char ToChar (decimal value)
\r
471 throw new InvalidCastException ("This conversion is not supported.");
\r
474 public static char ToChar (double value)
\r
476 throw new InvalidCastException ("This conversion is not supported.");
\r
479 public static char ToChar (int value)
\r
481 if (value > Char.MaxValue || value < Char.MinValue)
\r
482 throw new OverflowException (Locale.GetText (
\r
483 "Value is greater than Char.MaxValue or less than Char.MinValue"));
\r
485 return (char)value;
\r
488 public static char ToChar (long value)
\r
490 if (value > Char.MaxValue || value < Char.MinValue)
\r
491 throw new OverflowException (Locale.GetText (
\r
492 "Value is greater than Char.MaxValue or less than Char.MinValue"));
\r
494 return (char)value;
\r
497 public static char ToChar (float value)
\r
499 throw new InvalidCastException ("This conversion is not supported.");
\r
502 [CLSCompliant (false)]
\r
503 public static char ToChar (sbyte value)
\r
505 if (value < Char.MinValue)
\r
506 throw new OverflowException (Locale.GetText (
\r
507 "Value is less than Char.MinValue"));
\r
509 return (char)value;
\r
512 public static char ToChar (short value)
\r
514 if (value < Char.MinValue)
\r
515 throw new OverflowException (Locale.GetText (
\r
516 "Value is less than Char.MinValue"));
\r
518 return (char)value;
\r
521 public static char ToChar (string value)
\r
523 return Char.Parse (value);
\r
526 public static char ToChar (string value, IFormatProvider provider)
\r
528 return Char.Parse (value); // provider is ignored.
\r
531 [CLSCompliant (false)]
\r
532 public static char ToChar (uint value)
\r
534 if (value > Char.MaxValue)
\r
535 throw new OverflowException (Locale.GetText (
\r
536 "Value is greater than Char.MaxValue"));
\r
538 return (char)value;
\r
541 [CLSCompliant (false)]
\r
542 public static char ToChar (ulong value)
\r
544 if (value > Char.MaxValue)
\r
545 throw new OverflowException (Locale.GetText (
\r
546 "Value is greater than Char.MaxValue"));
\r
548 return (char)value;
\r
551 [CLSCompliant (false)]
\r
552 public static char ToChar (ushort value)
\r
554 if (value > Char.MaxValue)
\r
555 throw new OverflowException (Locale.GetText (
\r
556 "Value is greater than Char.MaxValue"));
\r
558 return (char)value;
\r
561 public static char ToChar (object value)
\r
563 return ToChar (value, null);
\r
566 public static char ToChar (object value, IFormatProvider provider)
\r
570 return ((IConvertible) value).ToChar (provider);
\r
573 // ========== DateTime Conversions ========== //
\r
575 public static DateTime ToDateTime (string value)
\r
577 return DateTime.Parse (value);
\r
580 public static DateTime ToDateTime (string value, IFormatProvider provider)
\r
582 return DateTime.Parse (value, provider);
\r
585 public static DateTime ToDateTime (bool value)
\r
587 throw new InvalidCastException ("This conversion is not supported.");
\r
590 public static DateTime ToDateTime (byte value)
\r
592 throw new InvalidCastException ("This conversion is not supported.");
\r
595 public static DateTime ToDateTime (char value)
\r
597 throw new InvalidCastException ("This conversion is not supported.");
\r
600 public static DateTime ToDateTime (DateTime value)
\r
605 public static DateTime ToDateTime (decimal value)
\r
607 throw new InvalidCastException ("This conversion is not supported.");
\r
610 public static DateTime ToDateTime (double value)
\r
612 throw new InvalidCastException ("This conversion is not supported.");
\r
615 public static DateTime ToDateTime (short value)
\r
617 throw new InvalidCastException ("This conversion is not supported.");
\r
620 public static DateTime ToDateTime (int value)
\r
622 throw new InvalidCastException ("This conversion is not supported.");
\r
625 public static DateTime ToDateTime (long value)
\r
627 throw new InvalidCastException ("This conversion is not supported.");
\r
630 public static DateTime ToDateTime (float value)
\r
632 throw new InvalidCastException ("This conversion is not supported.");
\r
635 public static DateTime ToDateTime (object value)
\r
637 return ToDateTime (value, null);
\r
640 public static DateTime ToDateTime (object value, IFormatProvider provider)
\r
643 return DateTime.MinValue;
\r
644 return ((IConvertible) value).ToDateTime (provider);
\r
647 [CLSCompliant (false)]
\r
648 public static DateTime ToDateTime (sbyte value)
\r
650 throw new InvalidCastException ("This conversion is not supported.");
\r
652 [CLSCompliant (false)]
\r
653 public static DateTime ToDateTime (ushort value)
\r
655 throw new InvalidCastException ("This conversion is not supported.");
\r
658 [CLSCompliant (false)]
\r
659 public static DateTime ToDateTime (uint value)
\r
661 throw new InvalidCastException ("This conversion is not supported.");
\r
664 [CLSCompliant (false)]
\r
665 public static DateTime ToDateTime (ulong value)
\r
667 throw new InvalidCastException ("This conversion is not supported.");
\r
670 // ========== Decimal Conversions ========== //
\r
672 public static decimal ToDecimal (bool value)
\r
674 return value ? 1 : 0;
\r
677 public static decimal ToDecimal (byte value)
\r
679 return (decimal)value;
\r
682 public static decimal ToDecimal (char value)
\r
684 throw new InvalidCastException ("This conversion is not supported.");
\r
687 public static decimal ToDecimal (DateTime value)
\r
689 throw new InvalidCastException ("This conversion is not supported.");
\r
692 public static decimal ToDecimal (decimal value)
\r
697 public static decimal ToDecimal (double value)
\r
699 if (value > (double)Decimal.MaxValue || value < (double)Decimal.MinValue)
\r
700 throw new OverflowException (Locale.GetText (
\r
701 "Value is greater than Decimal.MaxValue or less than Decimal.MinValue"));
\r
703 return (decimal)value;
\r
706 public static decimal ToDecimal (float value)
\r
708 return (decimal) value;
\r
711 public static decimal ToDecimal (int value)
\r
713 return (decimal)value;
\r
716 public static decimal ToDecimal (long value)
\r
718 return (decimal)value;
\r
721 [CLSCompliant (false)]
\r
722 public static decimal ToDecimal (sbyte value)
\r
724 return (decimal)value;
\r
727 public static decimal ToDecimal (short value)
\r
729 return (decimal)value;
\r
732 public static decimal ToDecimal (string value)
\r
734 return Decimal.Parse (value);
\r
737 public static decimal ToDecimal (string value, IFormatProvider provider)
\r
739 return Decimal.Parse (value, provider);
\r
742 [CLSCompliant (false)]
\r
743 public static decimal ToDecimal (uint value)
\r
745 return (decimal)value;
\r
748 [CLSCompliant (false)]
\r
749 public static decimal ToDecimal (ulong value)
\r
751 return (decimal)value;
\r
754 [CLSCompliant (false)]
\r
755 public static decimal ToDecimal (ushort value)
\r
757 return (decimal)value;
\r
760 public static decimal ToDecimal (object value)
\r
762 return ToDecimal (value, null);
\r
765 public static decimal ToDecimal (object value, IFormatProvider provider)
\r
768 return new Decimal (0);
\r
769 return ((IConvertible) value).ToDecimal (provider);
\r
773 // ========== Double Conversions ========== //
\r
775 public static double ToDouble (bool value)
\r
777 return value ? 1 : 0;
\r
780 public static double ToDouble (byte value)
\r
782 return (double) value;
\r
785 public static double ToDouble (char value)
\r
787 throw new InvalidCastException ("This conversion is not supported.");
\r
790 public static double ToDouble (DateTime value)
\r
792 throw new InvalidCastException ("This conversion is not supported.");
\r
795 public static double ToDouble (decimal value)
\r
797 return (double)value;
\r
800 public static double ToDouble (double value)
\r
805 public static double ToDouble (float value)
\r
807 return (double) Math.Round (value);
\r
810 public static double ToDouble (int value)
\r
812 return (double)value;
\r
815 public static double ToDouble (long value)
\r
817 return (double)value;
\r
820 [CLSCompliant (false)]
\r
821 public static double ToDouble (sbyte value)
\r
823 return (double)value;
\r
826 public static double ToDouble (short value)
\r
828 return (double)value;
\r
831 public static double ToDouble (string value)
\r
833 return Double.Parse (value);
\r
836 public static double ToDouble (string value, IFormatProvider provider)
\r
838 return Double.Parse (value, provider);
\r
841 [CLSCompliant (false)]
\r
842 public static double ToDouble (uint value)
\r
844 return (double)value;
\r
847 [CLSCompliant (false)]
\r
848 public static double ToDouble (ulong value)
\r
850 return (double)value;
\r
853 [CLSCompliant (false)]
\r
854 public static double ToDouble (ushort value)
\r
856 return (double)value;
\r
859 public static double ToDouble (object value)
\r
861 return ToDouble (value, null);
\r
864 public static double ToDouble (object value, IFormatProvider provider)
\r
868 return ((IConvertible) value).ToDouble (provider);
\r
871 // ========== Int16 Conversions ========== //
\r
873 public static short ToInt16 (bool value)
\r
875 return (short)(value ? 1 : 0);
\r
878 public static short ToInt16 (byte value)
\r
880 return (short)value;
\r
883 public static short ToInt16 (char value)
\r
885 if (value > Int16.MaxValue)
\r
886 throw new OverflowException (Locale.GetText (
\r
887 "Value is greater than Int16.MaxValue"));
\r
889 return (short)value;
\r
892 public static short ToInt16 (DateTime value)
\r
894 throw new InvalidCastException ("This conversion is not supported.");
\r
897 public static short ToInt16 (decimal value)
\r
899 if (value > Int16.MaxValue || value < Int16.MinValue)
\r
900 throw new OverflowException (Locale.GetText (
\r
901 "Value is greater than Int16.MaxValue or less than Int16.MinValue"));
\r
903 // Returned Even-Rounded
\r
904 return (short)(Math.Round (value));
\r
907 public static short ToInt16 (double value)
\r
909 if (value > Int16.MaxValue || value < Int16.MinValue)
\r
910 throw new OverflowException (Locale.GetText (
\r
911 "Value is greater than Int16.MaxValue or less than Int16.MinValue"));
\r
913 // Returned Even-Rounded
\r
914 return (short)(Math.Round (value));
\r
917 public static short ToInt16 (float value)
\r
919 if (value > Int16.MaxValue || value < Int16.MinValue)
\r
920 throw new OverflowException (Locale.GetText (
\r
921 "Value is greater than Int16.MaxValue or less than Int16.MinValue"));
\r
923 // Returned Even-Rounded, use Math.Round pass as a double.
\r
924 return (short)Math.Round ( (double)value);
\r
927 public static short ToInt16 (int value)
\r
929 if (value > Int16.MaxValue || value < Int16.MinValue)
\r
930 throw new OverflowException (Locale.GetText (
\r
931 "Value is greater than Int16.MaxValue or less than Int16.MinValue"));
\r
933 return (short)value;
\r
936 public static short ToInt16 (long value)
\r
938 if (value > Int16.MaxValue || value < Int16.MinValue)
\r
939 throw new OverflowException (Locale.GetText (
\r
940 "Value is greater than Int16.MaxValue or less than Int16.MinValue"));
\r
942 return (short)value;
\r
945 [CLSCompliant (false)]
\r
946 public static short ToInt16 (sbyte value)
\r
948 return (short)value;
\r
951 public static short ToInt16 (short value)
\r
956 public static short ToInt16 (string value)
\r
958 return Int16.Parse (value);
\r
961 public static short ToInt16 (string value, IFormatProvider provider)
\r
963 return Int16.Parse (value, provider);
\r
967 public static short ToInt16 (string value, int fromBase)
\r
969 return Convert.ToInt16 (ConvertFromBase (value, fromBase));
\r
972 [CLSCompliant (false)]
\r
973 public static short ToInt16 (uint value)
\r
975 if (value > Int16.MaxValue)
\r
976 throw new OverflowException (Locale.GetText (
\r
977 "Value is greater than Int16.MaxValue"));
\r
979 return (short)value;
\r
982 [CLSCompliant (false)]
\r
983 public static short ToInt16 (ulong value)
\r
985 if (value > (ulong)Int16.MaxValue)
\r
986 throw new OverflowException (Locale.GetText (
\r
987 "Value is greater than Int16.MaxValue"));
\r
988 return (short)value;
\r
991 [CLSCompliant (false)]
\r
992 public static short ToInt16 (ushort value)
\r
994 if (value > Int16.MaxValue)
\r
995 throw new OverflowException (Locale.GetText (
\r
996 "Value is greater than Int16.MaxValue"));
\r
998 return (short)value;
\r
1001 public static short ToInt16 (object value)
\r
1003 return ToInt16 (value, null);
\r
1006 public static short ToInt16 (object value, IFormatProvider provider)
\r
1008 if (value == null)
\r
1010 return ((IConvertible) value).ToInt16 (provider);
\r
1013 // ========== Int32 Conversions ========== //
\r
1015 public static int ToInt32 (bool value)
\r
1017 return value ? 1 : 0;
\r
1020 public static int ToInt32 (byte value)
\r
1022 return (int)value;
\r
1025 public static int ToInt32 (char value)
\r
1027 return (int)value;
\r
1030 public static int ToInt32 (DateTime value)
\r
1032 throw new InvalidCastException ("This conversion is not supported.");
\r
1035 public static int ToInt32 (decimal value)
\r
1037 if (value > Int32.MaxValue || value < Int32.MinValue)
\r
1038 throw new OverflowException (Locale.GetText (
\r
1039 "Value is greater than Int32.MaxValue or less than Int32.MinValue"));
\r
1041 // Returned Even-Rounded
\r
1042 return (int)(Math.Round (value));
\r
1045 public static int ToInt32 (double value)
\r
1047 if (value > Int32.MaxValue || value < Int32.MinValue)
\r
1048 throw new OverflowException (Locale.GetText (
\r
1049 "Value is greater than Int32.MaxValue or less than Int32.MinValue"));
\r
1051 // Returned Even-Rounded
\r
1052 return (int)(Math.Round (value));
\r
1055 public static int ToInt32 (float value)
\r
1057 if (value > Int32.MaxValue || value < Int32.MinValue)
\r
1058 throw new OverflowException (Locale.GetText (
\r
1059 "Value is greater than Int32.MaxValue or less than Int32.MinValue"));
\r
1061 // Returned Even-Rounded, pass as a double, could just call
\r
1062 // Convert.ToInt32 ( (double)value);
\r
1063 return (int)(Math.Round ( (double)value));
\r
1066 public static int ToInt32 (int value)
\r
1071 public static int ToInt32 (long value)
\r
1073 if (value > Int32.MaxValue || value < Int32.MinValue)
\r
1074 throw new OverflowException (Locale.GetText (
\r
1075 "Value is greater than Int32.MaxValue or less than Int32.MinValue"));
\r
1077 return (int)value;
\r
1080 [CLSCompliant (false)]
\r
1081 public static int ToInt32 (sbyte value)
\r
1083 return (int)value;
\r
1086 public static int ToInt32 (short value)
\r
1088 return (int)value;
\r
1091 public static int ToInt32 (string value)
\r
1093 return Int32.Parse (value);
\r
1096 public static int ToInt32 (string value, IFormatProvider provider)
\r
1098 return Int32.Parse (value, provider);
\r
1102 public static int ToInt32 (string value, int fromBase)
\r
1104 return ConvertFromBase (value, fromBase);
\r
1107 [CLSCompliant (false)]
\r
1108 public static int ToInt32 (uint value)
\r
1110 if (value > Int32.MaxValue)
\r
1111 throw new OverflowException (Locale.GetText (
\r
1112 "Value is greater than Int32.MaxValue"));
\r
1114 return (int)value;
\r
1117 [CLSCompliant (false)]
\r
1118 public static int ToInt32 (ulong value)
\r
1120 if (value > Int32.MaxValue)
\r
1121 throw new OverflowException (Locale.GetText (
\r
1122 "Value is greater than Int32.MaxValue"));
\r
1124 return (int)value;
\r
1127 [CLSCompliant (false)]
\r
1128 public static int ToInt32 (ushort value)
\r
1130 return (int)value;
\r
1133 public static int ToInt32 (object value)
\r
1135 return ToInt32 (value, null);
\r
1138 public static int ToInt32 (object value, IFormatProvider provider)
\r
1140 if (value == null)
\r
1142 return ((IConvertible) value).ToInt32 (provider);
\r
1145 // ========== Int64 Conversions ========== //
\r
1147 public static long ToInt64 (bool value)
\r
1149 return value ? 1 : 0;
\r
1152 public static long ToInt64 (byte value)
\r
1154 return (long)(ulong)value;
\r
1157 public static long ToInt64 (char value)
\r
1159 return (long)value;
\r
1162 public static long ToInt64 (DateTime value)
\r
1164 throw new InvalidCastException ("This conversion is not supported.");
\r
1167 public static long ToInt64 (decimal value)
\r
1169 if (value > Int64.MaxValue || value < Int64.MinValue)
\r
1170 throw new OverflowException (Locale.GetText (
\r
1171 "Value is greater than Int64.MaxValue or less than Int64.MinValue"));
\r
1173 // Returned Even-Rounded
\r
1174 return (long)(Math.Round (value));
\r
1177 public static long ToInt64 (double value)
\r
1179 if (value > Int64.MaxValue || value < Int64.MinValue)
\r
1180 throw new OverflowException (Locale.GetText (
\r
1181 "Value is greater than Int64.MaxValue or less than Int64.MinValue"));
\r
1183 // Returned Even-Rounded
\r
1184 return (long)(Math.Round (value));
\r
1187 public static long ToInt64 (float value)
\r
1189 if (value > Int64.MaxValue || value < Int64.MinValue)
\r
1190 throw new OverflowException (Locale.GetText (
\r
1191 "Value is greater than Int64.MaxValue or less than Int64.MinValue"));
\r
1193 // Returned Even-Rounded, pass to Math as a double, could
\r
1194 // just call Convert.ToInt64 ( (double)value);
\r
1195 return (long)(Math.Round ( (double)value));
\r
1198 public static long ToInt64 (int value)
\r
1200 return (long)value;
\r
1203 public static long ToInt64 (long value)
\r
1208 [CLSCompliant (false)]
\r
1209 public static long ToInt64 (sbyte value)
\r
1211 return (long)value;
\r
1214 public static long ToInt64 (short value)
\r
1216 return (long)value;
\r
1219 public static long ToInt64 (string value)
\r
1221 return Int64.Parse (value);
\r
1224 public static long ToInt64 (string value, IFormatProvider provider)
\r
1226 return Int64.Parse (value, provider);
\r
1229 public static long ToInt64 (string value, int fromBase)
\r
1231 if (NotValidBase (fromBase))
\r
1232 throw new ArgumentException ("fromBase is not valid.");
\r
1234 return ConvertFromBase64 (value, fromBase);
\r
1237 [CLSCompliant (false)]
\r
1238 public static long ToInt64 (uint value)
\r
1240 return (long)(ulong)value;
\r
1243 [CLSCompliant (false)]
\r
1244 public static long ToInt64 (ulong value)
\r
1246 if (value > Int64.MaxValue)
\r
1247 throw new OverflowException (Locale.GetText (
\r
1248 "Value is greater than Int64.MaxValue"));
\r
1250 return (long)value;
\r
1253 [CLSCompliant (false)]
\r
1254 public static long ToInt64 (ushort value)
\r
1256 return (long)(ulong)value;
\r
1259 public static long ToInt64 (object value)
\r
1261 return ToInt64 (value, null);
\r
1264 public static long ToInt64 (object value, IFormatProvider provider)
\r
1266 if (value == null)
\r
1268 return ((IConvertible) value).ToInt64 (provider);
\r
1271 // ========== SByte Conversions ========== //
\r
1273 [CLSCompliant (false)]
\r
1274 public static sbyte ToSByte (bool value)
\r
1276 return (sbyte)(value ? 1 : 0);
\r
1279 [CLSCompliant (false)]
\r
1280 public static sbyte ToSByte (byte value)
\r
1282 if (value > SByte.MaxValue)
\r
1283 throw new OverflowException (Locale.GetText (
\r
1284 "Value is greater than SByte.MaxValue"));
\r
1286 return (sbyte)value;
\r
1289 [CLSCompliant (false)]
\r
1290 public static sbyte ToSByte (char value)
\r
1292 if (value > SByte.MaxValue)
\r
1293 throw new OverflowException (Locale.GetText (
\r
1294 "Value is greater than SByte.MaxValue"));
\r
1296 return (sbyte)value;
\r
1299 [CLSCompliant (false)]
\r
1300 public static sbyte ToSByte (DateTime value)
\r
1302 throw new InvalidCastException ("This conversion is not supported.");
\r
1305 [CLSCompliant (false)]
\r
1306 public static sbyte ToSByte (decimal value)
\r
1308 if (value > SByte.MaxValue || value < SByte.MinValue)
\r
1309 throw new OverflowException (Locale.GetText (
\r
1310 "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
\r
1312 // Returned Even-Rounded
\r
1313 return (sbyte)(Math.Round (value));
\r
1316 [CLSCompliant (false)]
\r
1317 public static sbyte ToSByte (double value)
\r
1319 if (value > SByte.MaxValue || value < SByte.MinValue)
\r
1320 throw new OverflowException (Locale.GetText (
\r
1321 "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
\r
1323 // Returned Even-Rounded
\r
1324 return (sbyte)(Math.Round (value));
\r
1327 [CLSCompliant (false)]
\r
1328 public static sbyte ToSByte (float value)
\r
1330 if (value > SByte.MaxValue || value < SByte.MinValue)
\r
1331 throw new OverflowException (Locale.GetText (
\r
1332 "Value is greater than SByte.MaxValue or less than SByte.Minalue"));
\r
1334 // Returned Even-Rounded, pass as double to Math
\r
1335 return (sbyte)(Math.Round ( (double)value));
\r
1338 [CLSCompliant (false)]
\r
1339 public static sbyte ToSByte (int value)
\r
1341 if (value > SByte.MaxValue || value < SByte.MinValue)
\r
1342 throw new OverflowException (Locale.GetText (
\r
1343 "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
\r
1345 return (sbyte)value;
\r
1348 [CLSCompliant (false)]
\r
1349 public static sbyte ToSByte (long value)
\r
1351 if (value > SByte.MaxValue || value < SByte.MinValue)
\r
1352 throw new OverflowException (Locale.GetText (
\r
1353 "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
\r
1355 return (sbyte)value;
\r
1358 [CLSCompliant (false)]
\r
1359 public static sbyte ToSByte (sbyte value)
\r
1364 [CLSCompliant (false)]
\r
1365 public static sbyte ToSByte (short value)
\r
1367 if (value > SByte.MaxValue || value < SByte.MinValue)
\r
1368 throw new OverflowException (Locale.GetText (
\r
1369 "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
\r
1371 return (sbyte)value;
\r
1374 [CLSCompliant (false)]
\r
1375 public static sbyte ToSByte (string value)
\r
1377 return SByte.Parse (value);
\r
1380 [CLSCompliant (false)]
\r
1381 public static sbyte ToSByte (string value, IFormatProvider provider)
\r
1383 return SByte.Parse (value, provider);
\r
1386 [CLSCompliant (false)]
\r
1387 public static sbyte ToSByte (string value, int fromBase)
\r
1389 int retVal = ConvertFromBase (value, fromBase);
\r
1391 if (retVal == 255)
\r
1394 if (retVal < (int) SByte.MinValue || retVal > (int) SByte.MaxValue)
\r
1395 throw new OverflowException ();
\r
1397 return (sbyte) retVal;
\r
1400 [CLSCompliant (false)]
\r
1401 public static sbyte ToSByte (uint value)
\r
1403 if (value > SByte.MaxValue)
\r
1404 throw new OverflowException (Locale.GetText (
\r
1405 "Value is greater than SByte.MaxValue"));
\r
1407 return (sbyte)value;
\r
1410 [CLSCompliant (false)]
\r
1411 public static sbyte ToSByte (ulong value)
\r
1413 if (value > (ulong)SByte.MaxValue)
\r
1414 throw new OverflowException (Locale.GetText (
\r
1415 "Value is greater than SByte.MaxValue"));
\r
1417 return (sbyte)value;
\r
1420 [CLSCompliant (false)]
\r
1421 public static sbyte ToSByte (ushort value)
\r
1423 if (value > SByte.MaxValue)
\r
1424 throw new OverflowException (Locale.GetText (
\r
1425 "Value is greater than SByte.MaxValue"));
\r
1427 return (sbyte)value;
\r
1430 [CLSCompliant (false)]
\r
1431 public static sbyte ToSByte (object value)
\r
1433 return ToSByte (value, null);
\r
1436 [CLSCompliant (false)]
\r
1437 public static sbyte ToSByte (object value, IFormatProvider provider)
\r
1439 if (value == null)
\r
1441 return ((IConvertible) value).ToSByte (provider);
\r
1444 // ========== Single Conversions ========== //
\r
1446 public static float ToSingle (bool value)
\r
1448 return value ? 1 : 0;
\r
1451 public static float ToSingle (byte value)
\r
1453 return (float)value;
\r
1456 public static float ToSingle (Char value)
\r
1458 throw new InvalidCastException ("This conversion is not supported.");
\r
1461 public static float ToSingle (DateTime value)
\r
1463 throw new InvalidCastException ("This conversion is not supported.");
\r
1466 public static float ToSingle (decimal value)
\r
1468 return (float)value;
\r
1471 public static float ToSingle (double value)
\r
1473 if (value > Single.MaxValue || value < Single.MinValue)
\r
1474 throw new OverflowException (Locale.GetText (
\r
1475 "Value is greater than Single.MaxValue or less than Single.MinValue"));
\r
1477 return (float)value;
\r
1480 public static float ToSingle (float value)
\r
1485 public static float ToSingle (int value)
\r
1487 return (float)value;
\r
1490 public static float ToSingle (long value)
\r
1492 return (float)value;
\r
1495 [CLSCompliant (false)]
\r
1496 public static float ToSingle (sbyte value)
\r
1498 return (float)value;
\r
1501 public static float ToSingle (short value)
\r
1503 return (float)value;
\r
1506 public static float ToSingle (string value)
\r
1508 return Single.Parse (value);
\r
1511 public static float ToSingle (string value, IFormatProvider provider)
\r
1513 return Single.Parse (value, provider);
\r
1516 [CLSCompliant (false)]
\r
1517 public static float ToSingle (uint value)
\r
1519 return (float)value;
\r
1522 [CLSCompliant (false)]
\r
1523 public static float ToSingle (ulong value)
\r
1525 return (float)value;
\r
1528 [CLSCompliant (false)]
\r
1529 public static float ToSingle (ushort value)
\r
1531 return (float)value;
\r
1534 public static float ToSingle (object value)
\r
1536 return ToSingle (value, null);
\r
1539 // [CLSCompliant (false)]
\r
1540 public static float ToSingle (object value, IFormatProvider provider)
\r
1542 if (value == null)
\r
1544 return ((IConvertible) value).ToSingle (provider);
\r
1547 // ========== String Conversions ========== //
\r
1549 public static string ToString (bool value)
\r
1551 return value.ToString ();
\r
1554 public static string ToString (bool value, IFormatProvider provider)
\r
1556 return value.ToString (); // the same as ToString (bool).
\r
1559 public static string ToString (byte value)
\r
1561 return value.ToString ();
\r
1564 public static string ToString (byte value, IFormatProvider provider)
\r
1566 return value.ToString (provider);
\r
1569 public static string ToString (byte value, int toBase)
\r
1571 if (NotValidBase (toBase))
\r
1572 throw new ArgumentException ("toBase is not valid.");
\r
1574 return ConvertToBase ((int) value, toBase);
\r
1577 public static string ToString (char value)
\r
1579 return value.ToString ();
\r
1582 public static string ToString (char value, IFormatProvider provider)
\r
1584 return value.ToString (); // the same as ToString (char)
\r
1587 public static string ToString (DateTime value)
\r
1589 return value.ToString ();
\r
1592 public static string ToString (DateTime value, IFormatProvider provider)
\r
1594 return value.ToString (provider);
\r
1597 public static string ToString (decimal value)
\r
1599 return value.ToString ();
\r
1602 public static string ToString (decimal value, IFormatProvider provider)
\r
1604 return value.ToString (provider);
\r
1607 public static string ToString (double value)
\r
1609 return value.ToString ();
\r
1612 public static string ToString (double value, IFormatProvider provider)
\r
1614 return value.ToString (provider);
\r
1617 public static string ToString (float value)
\r
1619 return value.ToString ();
\r
1622 public static string ToString (float value, IFormatProvider provider)
\r
1624 return value.ToString (provider);
\r
1627 public static string ToString (int value)
\r
1629 return value.ToString ();
\r
1632 public static string ToString (int value, int toBase)
\r
1634 if (NotValidBase (toBase))
\r
1635 throw new ArgumentException ("toBase is not valid.");
\r
1637 return ConvertToBase ((int) value, toBase);
\r
1640 public static string ToString (int value, IFormatProvider provider)
\r
1642 return value.ToString (provider);
\r
1645 public static string ToString (long value)
\r
1647 return value.ToString ();
\r
1650 public static string ToString (long value, int toBase)
\r
1652 if (NotValidBase (toBase))
\r
1653 throw new ArgumentException ("toBase is not valid.");
\r
1655 return ConvertToBase (value, toBase);
\r
1658 public static string ToString (long value, IFormatProvider provider)
\r
1660 return value.ToString (provider);
\r
1663 public static string ToString (object value)
\r
1665 return ToString (value, null);
\r
1668 public static string ToString (object value, IFormatProvider provider)
\r
1670 if (value is IConvertible)
\r
1671 return ((IConvertible) value).ToString (provider);
\r
1672 else if (value != null)
\r
1673 return value.ToString ();
\r
1674 return String.Empty;
\r
1677 [CLSCompliant (false)]
\r
1678 public static string ToString (sbyte value)
\r
1680 return value.ToString ();
\r
1683 [CLSCompliant (false)]
\r
1684 public static string ToString (sbyte value, IFormatProvider provider)
\r
1686 return value.ToString (provider);
\r
1689 public static string ToString (short value)
\r
1691 return value.ToString ();
\r
1694 public static string ToString (short value, int toBase)
\r
1696 if (NotValidBase (toBase))
\r
1697 throw new ArgumentException ("toBase is not valid.");
\r
1699 return ConvertToBase ((int) value, toBase);
\r
1702 public static string ToString (short value, IFormatProvider provider)
\r
1704 return value.ToString (provider);
\r
1707 public static string ToString (string value)
\r
1712 public static string ToString (string value, IFormatProvider provider)
\r
1714 return value; // provider is ignored.
\r
1717 [CLSCompliant (false)]
\r
1718 public static string ToString (uint value)
\r
1720 return value.ToString ();
\r
1723 [CLSCompliant (false)]
\r
1724 public static string ToString (uint value, IFormatProvider provider)
\r
1726 return value.ToString (provider);
\r
1729 [CLSCompliant (false)]
\r
1730 public static string ToString (ulong value)
\r
1732 return value.ToString ();
\r
1735 [CLSCompliant (false)]
\r
1736 public static string ToString (ulong value, IFormatProvider provider)
\r
1738 return value.ToString (provider);
\r
1741 [CLSCompliant (false)]
\r
1742 public static string ToString (ushort value)
\r
1744 return value.ToString ();
\r
1747 [CLSCompliant (false)]
\r
1748 public static string ToString (ushort value, IFormatProvider provider)
\r
1750 return value.ToString (provider);
\r
1753 // ========== UInt16 Conversions ========== //
\r
1755 [CLSCompliant (false)]
\r
1756 public static ushort ToUInt16 (bool value)
\r
1758 return (ushort)(value ? 1 : 0);
\r
1761 [CLSCompliant (false)]
\r
1762 public static ushort ToUInt16 (byte value)
\r
1764 return (ushort)value;
\r
1767 [CLSCompliant (false)]
\r
1768 public static ushort ToUInt16 (char value)
\r
1770 return (ushort)value;
\r
1773 [CLSCompliant (false)]
\r
1774 public static ushort ToUInt16 (DateTime value)
\r
1776 throw new InvalidCastException ("This conversion is not supported.");
\r
1779 [CLSCompliant (false)]
\r
1780 public static ushort ToUInt16 (decimal value)
\r
1782 if (value > UInt16.MaxValue || value < UInt16.MinValue)
\r
1783 throw new OverflowException (Locale.GetText (
\r
1784 "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
\r
1786 // Returned Even-Rounded
\r
1787 return (ushort)(Math.Round (value));
\r
1790 [CLSCompliant (false)]
\r
1791 public static ushort ToUInt16 (double value)
\r
1793 if (value > UInt16.MaxValue || value < UInt16.MinValue)
\r
1794 throw new OverflowException (Locale.GetText (
\r
1795 "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
\r
1797 // Returned Even-Rounded
\r
1798 return (ushort)(Math.Round (value));
\r
1801 [CLSCompliant (false)]
\r
1802 public static ushort ToUInt16 (float value)
\r
1804 if (value > UInt16.MaxValue || value < UInt16.MinValue)
\r
1805 throw new OverflowException (Locale.GetText (
\r
1806 "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
\r
1808 // Returned Even-Rounded, pass as double to Math
\r
1809 return (ushort)(Math.Round ( (double)value));
\r
1812 [CLSCompliant (false)]
\r
1813 public static ushort ToUInt16 (int value)
\r
1815 if (value > UInt16.MaxValue || value < UInt16.MinValue)
\r
1816 throw new OverflowException (Locale.GetText (
\r
1817 "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
\r
1819 return (ushort)value;
\r
1822 [CLSCompliant (false)]
\r
1823 public static ushort ToUInt16 (long value)
\r
1825 if (value > UInt16.MaxValue || value < UInt16.MinValue)
\r
1826 throw new OverflowException (Locale.GetText (
\r
1827 "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
\r
1829 return (ushort)value;
\r
1832 [CLSCompliant (false)]
\r
1833 public static ushort ToUInt16 (sbyte value)
\r
1835 if (value < UInt16.MinValue)
\r
1836 throw new OverflowException (Locale.GetText (
\r
1837 "Value is less than UInt16.MinValue"));
\r
1839 return (ushort)value;
\r
1842 [CLSCompliant (false)]
\r
1843 public static ushort ToUInt16 (short value)
\r
1845 if (value < UInt16.MinValue)
\r
1846 throw new OverflowException (Locale.GetText (
\r
1847 "Value is less than UInt16.MinValue"));
\r
1849 return (ushort)value;
\r
1852 [CLSCompliant (false)]
\r
1853 public static ushort ToUInt16 (string value)
\r
1855 return UInt16.Parse (value);
\r
1858 [CLSCompliant (false)]
\r
1859 public static ushort ToUInt16 (string value, IFormatProvider provider)
\r
1861 return UInt16.Parse (value, provider);
\r
1864 [CLSCompliant (false)]
\r
1865 public static ushort ToUInt16 (string value, int fromBase)
\r
1867 return (ushort) ConvertFromBase (value, fromBase);
\r
1870 [CLSCompliant (false)]
\r
1871 public static ushort ToUInt16 (uint value)
\r
1873 if (value > UInt16.MaxValue)
\r
1874 throw new OverflowException (Locale.GetText (
\r
1875 "Value is greater than UInt16.MaxValue"));
\r
1877 return (ushort)value;
\r
1880 [CLSCompliant (false)]
\r
1881 public static ushort ToUInt16 (ulong value)
\r
1883 if (value > (ulong)UInt16.MaxValue)
\r
1884 throw new OverflowException (Locale.GetText (
\r
1885 "Value is greater than UInt16.MaxValue"));
\r
1887 return (ushort)value;
\r
1890 [CLSCompliant (false)]
\r
1891 public static ushort ToUInt16 (ushort value)
\r
1896 [CLSCompliant (false)]
\r
1897 public static ushort ToUInt16 (object value)
\r
1899 return ToUInt16 (value, null);
\r
1902 [CLSCompliant (false)]
\r
1903 public static ushort ToUInt16 (object value, IFormatProvider provider)
\r
1905 if (value == null)
\r
1907 return ((IConvertible) value).ToUInt16 (provider);
\r
1910 // ========== UInt32 Conversions ========== //
\r
1912 [CLSCompliant (false)]
\r
1913 public static uint ToUInt32 (bool value)
\r
1915 return (uint)(value ? 1 : 0);
\r
1918 [CLSCompliant (false)]
\r
1919 public static uint ToUInt32 (byte value)
\r
1921 return (uint)value;
\r
1924 [CLSCompliant (false)]
\r
1925 public static uint ToUInt32 (char value)
\r
1927 return (uint)value;
\r
1930 [CLSCompliant (false)]
\r
1931 public static uint ToUInt32 (DateTime value)
\r
1933 throw new InvalidCastException ("This conversion is not supported.");
\r
1936 [CLSCompliant (false)]
\r
1937 public static uint ToUInt32 (decimal value)
\r
1939 if (value > UInt32.MaxValue || value < UInt32.MinValue)
\r
1940 throw new OverflowException (Locale.GetText (
\r
1941 "Value is greater than UInt32.MaxValue or less than UInt32.MinValue"));
\r
1943 // Returned Even-Rounded
\r
1944 return (uint)(Math.Round (value));
\r
1947 [CLSCompliant (false)]
\r
1948 public static uint ToUInt32 (double value)
\r
1950 if (value > UInt32.MaxValue || value < UInt32.MinValue)
\r
1951 throw new OverflowException (Locale.GetText (
\r
1952 "Value is greater than UInt32.MaxValue or less than UInt32.MinValue"));
\r
1954 // Returned Even-Rounded
\r
1955 return (uint)(Math.Round (value));
\r
1958 [CLSCompliant (false)]
\r
1959 public static uint ToUInt32 (float value)
\r
1961 if (value > UInt32.MaxValue || value < UInt32.MinValue)
\r
1962 throw new OverflowException (Locale.GetText (
\r
1963 "Value is greater than UInt32.MaxValue or less than UInt32.MinValue"));
\r
1965 // Returned Even-Rounded, pass as double to Math
\r
1966 return (uint)(Math.Round ( (double)value));
\r
1969 [CLSCompliant (false)]
\r
1970 public static uint ToUInt32 (int value)
\r
1972 if (value < UInt32.MinValue)
\r
1973 throw new OverflowException (Locale.GetText (
\r
1974 "Value is less than UInt32.MinValue"));
\r
1976 return (uint)value;
\r
1979 [CLSCompliant (false)]
\r
1980 public static uint ToUInt32 (long value)
\r
1982 if (value > UInt32.MaxValue || value < UInt32.MinValue)
\r
1983 throw new OverflowException (Locale.GetText (
\r
1984 "Value is greater than UInt32.MaxValue or less than UInt32.MinValue"));
\r
1986 return (uint)value;
\r
1989 [CLSCompliant (false)]
\r
1990 public static uint ToUInt32 (sbyte value)
\r
1992 if (value < UInt32.MinValue)
\r
1993 throw new OverflowException (Locale.GetText (
\r
1994 "Value is less than UInt32.MinValue"));
\r
1996 return (uint)value;
\r
1999 [CLSCompliant (false)]
\r
2000 public static uint ToUInt32 (short value)
\r
2002 if (value < UInt32.MinValue)
\r
2003 throw new OverflowException (Locale.GetText (
\r
2004 "Value is less than UInt32.MinValue"));
\r
2006 return (uint)value;
\r
2009 [CLSCompliant (false)]
\r
2010 public static uint ToUInt32 (string value)
\r
2012 return UInt32.Parse (value);
\r
2015 [CLSCompliant (false)]
\r
2016 public static uint ToUInt32 (string value, IFormatProvider provider)
\r
2018 return UInt32.Parse (value, provider);
\r
2021 [CLSCompliant (false)]
\r
2022 public static uint ToUInt32 (string value, int fromBase)
\r
2024 return (uint) ConvertFromBase (value, fromBase);
\r
2027 [CLSCompliant (false)]
\r
2028 public static uint ToUInt32 (uint value)
\r
2033 [CLSCompliant (false)]
\r
2034 public static uint ToUInt32 (ulong value)
\r
2036 if (value > UInt32.MaxValue)
\r
2037 throw new OverflowException (Locale.GetText (
\r
2038 "Value is greater than UInt32.MaxValue"));
\r
2040 return (uint)value;
\r
2043 [CLSCompliant (false)]
\r
2044 public static uint ToUInt32 (ushort value)
\r
2046 return (uint)value;
\r
2049 [CLSCompliant (false)]
\r
2050 public static uint ToUInt32 (object value)
\r
2052 return ToUInt32 (value, null);
\r
2055 [CLSCompliant (false)]
\r
2056 public static uint ToUInt32 (object value, IFormatProvider provider)
\r
2058 if (value == null)
\r
2060 return ((IConvertible) value).ToUInt32 (provider);
\r
2064 // ========== UInt64 Conversions ========== //
\r
2066 [CLSCompliant (false)]
\r
2067 public static ulong ToUInt64 (bool value)
\r
2069 return (ulong)(value ? 1 : 0);
\r
2072 [CLSCompliant (false)]
\r
2073 public static ulong ToUInt64 (byte value)
\r
2075 return (ulong)value;
\r
2078 [CLSCompliant (false)]
\r
2079 public static ulong ToUInt64 (char value)
\r
2081 return (ulong)value;
\r
2084 [CLSCompliant (false)]
\r
2085 public static ulong ToUInt64 (DateTime value)
\r
2087 throw new InvalidCastException ("The conversion is not supported.");
\r
2090 [CLSCompliant (false)]
\r
2091 public static ulong ToUInt64 (decimal value)
\r
2093 if (value > UInt64.MaxValue || value < UInt64.MinValue)
\r
2094 throw new OverflowException (Locale.GetText (
\r
2095 "Value is greater than UInt64.MaxValue or less than UInt64.MinValue"));
\r
2097 // Returned Even-Rounded
\r
2098 return (ulong)(Math.Round (value));
\r
2101 [CLSCompliant (false)]
\r
2102 public static ulong ToUInt64 (double value)
\r
2104 if (value > UInt64.MaxValue || value < UInt64.MinValue)
\r
2105 throw new OverflowException (Locale.GetText (
\r
2106 "Value is greater than UInt64.MaxValue or less than UInt64.MinValue"));
\r
2108 // Returned Even-Rounded
\r
2109 return (ulong)(Math.Round (value));
\r
2112 [CLSCompliant (false)]
\r
2113 public static ulong ToUInt64 (float value)
\r
2115 if (value > UInt64.MaxValue || value < UInt64.MinValue)
\r
2116 throw new OverflowException (Locale.GetText (
\r
2117 "Value is greater than UInt64.MaxValue or less than UInt64.MinValue"));
\r
2119 // Returned Even-Rounded, pass as a double to Math
\r
2120 return (ulong)(Math.Round ( (double)value));
\r
2123 [CLSCompliant (false)]
\r
2124 public static ulong ToUInt64 (int value)
\r
2126 if (value < (int)UInt64.MinValue)
\r
2127 throw new OverflowException (Locale.GetText (
\r
2128 "Value is less than UInt64.MinValue"));
\r
2130 return (ulong)value;
\r
2133 [CLSCompliant (false)]
\r
2134 public static ulong ToUInt64 (long value)
\r
2136 if (value < (long)UInt64.MinValue)
\r
2137 throw new OverflowException (Locale.GetText (
\r
2138 "Value is less than UInt64.MinValue"));
\r
2140 return (ulong)value;
\r
2143 [CLSCompliant (false)]
\r
2144 public static ulong ToUInt64 (sbyte value)
\r
2146 if (value < (sbyte)UInt64.MinValue)
\r
2147 throw new OverflowException
\r
2148 ("Value is less than UInt64.MinValue");
\r
2150 return (ulong)value;
\r
2153 [CLSCompliant (false)]
\r
2154 public static ulong ToUInt64 (short value)
\r
2156 if (value < (short)UInt64.MinValue)
\r
2157 throw new OverflowException (Locale.GetText (
\r
2158 "Value is less than UInt64.MinValue"));
\r
2160 return (ulong)value;
\r
2163 [CLSCompliant (false)]
\r
2164 public static ulong ToUInt64 (string value)
\r
2166 return UInt64.Parse (value);
\r
2169 [CLSCompliant (false)]
\r
2170 public static ulong ToUInt64 (string value, IFormatProvider provider)
\r
2172 return UInt64.Parse (value, provider);
\r
2175 [CLSCompliant (false)]
\r
2176 public static ulong ToUInt64 (string value, int fromBase)
\r
2178 return (ulong) ConvertFromBase (value, fromBase);
\r
2181 [CLSCompliant (false)]
\r
2182 public static ulong ToUInt64 (uint value)
\r
2184 return (ulong)value;
\r
2187 [CLSCompliant (false)]
\r
2188 public static ulong ToUInt64 (ulong value)
\r
2193 [CLSCompliant (false)]
\r
2194 public static ulong ToUInt64 (ushort value)
\r
2196 return (ulong)value;
\r
2199 [CLSCompliant (false)]
\r
2200 public static ulong ToUInt64 (object value)
\r
2202 return ToUInt64 (value, null);
\r
2205 [CLSCompliant (false)]
\r
2206 public static ulong ToUInt64 (object value, IFormatProvider provider)
\r
2208 if (value == null)
\r
2210 return ((IConvertible) value).ToUInt64 (provider);
\r
2214 // ========== Conversion / Helper Functions ========== //
\r
2216 public static object ChangeType (object value, Type conversionType)
\r
2218 CultureInfo ci = CultureInfo.CurrentCulture;
\r
2219 NumberFormatInfo number = ci.NumberFormat;
\r
2220 return ToType (value, conversionType, number);
\r
2223 public static object ChangeType (object value, TypeCode typeCode)
\r
2225 CultureInfo ci = CultureInfo.CurrentCulture;
\r
2226 Type conversionType = conversionTable [(int)typeCode];
\r
2227 NumberFormatInfo number = ci.NumberFormat;
\r
2228 return ToType (value, conversionType, number);
\r
2231 public static object ChangeType (object value, Type conversionType, IFormatProvider provider)
\r
2233 return ToType (value, conversionType, provider);
\r
2236 public static object ChangeType (object value, TypeCode typeCode, IFormatProvider provider)
\r
2238 Type conversionType = conversionTable [(int)typeCode];
\r
2239 return ToType (value, conversionType, provider);
\r
2242 private static bool NotValidBase (int value)
\r
2244 if ((value == 2) || (value == 8) ||
\r
2245 (value == 10) || (value == 16))
\r
2251 private static int ConvertFromBase (string value, int fromBase)
\r
2253 if (NotValidBase (fromBase))
\r
2254 throw new ArgumentException ("fromBase is not valid.");
\r
2260 foreach (char c in value) {
\r
2261 if (Char.IsNumber (c))
\r
2262 digitValue = c - '0';
\r
2263 else if (Char.IsLetter (c))
\r
2264 digitValue = Char.ToLower(c) - 'a' + 10;
\r
2266 throw new FormatException ("This is an invalid string: " + value);
\r
2268 if (digitValue >= fromBase)
\r
2269 throw new FormatException ("the digits are invalid.");
\r
2271 result = (fromBase) * result + digitValue;
\r
2276 throw new FormatException ("Could not find any digits.");
\r
2278 if (result > Int32.MaxValue || result < Int32.MinValue)
\r
2279 throw new OverflowException ("There is an overflow.");
\r
2284 private static long ConvertFromBase64 (string value, int fromBase)
\r
2286 if (NotValidBase (fromBase))
\r
2287 throw new ArgumentException ("fromBase is not valid.");
\r
2293 foreach (char c in value) {
\r
2294 if (Char.IsNumber (c))
\r
2295 digitValue = c - '0';
\r
2296 else if (Char.IsLetter (c))
\r
2297 digitValue = Char.ToLower(c) - 'a' + 10;
\r
2299 throw new FormatException ("This is an invalid string: " + value);
\r
2301 if (digitValue >= fromBase)
\r
2302 throw new FormatException ("the digits are invalid.");
\r
2304 result = (fromBase) * result + digitValue;
\r
2309 throw new FormatException ("Could not find any digits.");
\r
2311 if (result > Int64.MaxValue || result < Int64.MinValue)
\r
2312 throw new OverflowException ("There is an overflow.");
\r
2317 private static string ConvertToBase (int value, int toBase)
\r
2319 StringBuilder sb = new StringBuilder ();
\r
2320 BuildConvertedString (sb, value, toBase);
\r
2321 return sb.ToString ();
\r
2324 private static string ConvertToBase (long value, int toBase)
\r
2326 StringBuilder sb = new StringBuilder ();
\r
2327 BuildConvertedString64 (sb, value, toBase);
\r
2328 return sb.ToString ();
\r
2332 internal static void BuildConvertedString (StringBuilder sb, int value, int toBase)
\r
2334 int divided = value / toBase;
\r
2335 int reminder = value % toBase;
\r
2338 BuildConvertedString (sb, divided, toBase);
\r
2340 if (reminder >= 10)
\r
2341 sb.Append ((char) (reminder + 'a' - 10));
\r
2343 sb.Append ((char) (reminder + '0'));
\r
2346 internal static void BuildConvertedString64 (StringBuilder sb, long value, int toBase)
\r
2348 long divided = value / toBase;
\r
2349 long reminder = value % toBase;
\r
2352 BuildConvertedString64 (sb, divided, toBase);
\r
2354 if (reminder >= 10)
\r
2355 sb.Append ((char) (reminder + 'a' - 10));
\r
2357 sb.Append ((char) (reminder + '0'));
\r
2360 // Lookup table for the conversion ToType method. Order
\r
2361 // is important! Used by ToType for comparing the target
\r
2362 // type, and uses hardcoded array indexes.
\r
2363 private static Type[] conversionTable = {
\r
2364 // Valid ICovnertible Types
\r
2366 typeof (object), // 1 TypeCode.Object
\r
2367 typeof (DBNull), // 2 TypeCode.DBNull
\r
2368 typeof (Boolean), // 3 TypeCode.Boolean
\r
2369 typeof (Char), // 4 TypeCode.Char
\r
2370 typeof (SByte), // 5 TypeCode.SByte
\r
2371 typeof (Byte), // 6 TypeCode.Byte
\r
2372 typeof (Int16), // 7 TypeCode.Int16
\r
2373 typeof (UInt16), // 8 TypeCode.UInt16
\r
2374 typeof (Int32), // 9 TypeCode.Int32
\r
2375 typeof (UInt32), // 10 TypeCode.UInt32
\r
2376 typeof (Int64), // 11 TypeCode.Int64
\r
2377 typeof (UInt64), // 12 TypeCode.UInt64
\r
2378 typeof (Single), // 13 TypeCode.Single
\r
2379 typeof (Double), // 14 TypeCode.Double
\r
2380 typeof (Decimal), // 15 TypeCode.Decimal
\r
2381 typeof (DateTime), // 16 TypeCode.DateTime
\r
2382 typeof (String), // 18 TypeCode.String
\r
2385 // Function to convert an object to another type and return
\r
2386 // it as an object. In place for the core data types to use
\r
2387 // when implementing IConvertible. Uses hardcoded indexes in
\r
2388 // the conversionTypes array, so if modify carefully.
\r
2389 internal static object ToType (object value, Type conversionType,
\r
2390 IFormatProvider provider)
\r
2392 if (value == null)
\r
2393 throw new ArgumentException (Locale.GetText (
\r
2394 "Invalid conversion from null value"));
\r
2396 if (value is IConvertible) {
\r
2397 IConvertible convertValue = (IConvertible) value;
\r
2399 if (conversionType == conversionTable[0]) // 0 Empty
\r
2402 else if (conversionType == conversionTable[1]) // 1 TypeCode.Object
\r
2403 return (object) value;
\r
2405 // else if (conversionType == conversionTable[2]) // 2 TypeCode.DBNull
\r
2406 // return null; // It's not IConvertible
\r
2408 else if (conversionType == conversionTable[3]) // 3 TypeCode.Boolean
\r
2409 return (object) convertValue.ToBoolean (provider);
\r
2411 else if (conversionType == conversionTable[4]) // 4 TypeCode.Char
\r
2412 return (object) convertValue.ToChar (provider);
\r
2414 else if (conversionType == conversionTable[5]) // 5 TypeCode.SByte
\r
2415 return (object) convertValue.ToSByte (provider);
\r
2417 else if (conversionType == conversionTable[6]) // 6 TypeCode.Byte
\r
2418 return (object) convertValue.ToByte (provider);
\r
2420 else if (conversionType == conversionTable[7]) // 7 TypeCode.Int16
\r
2421 return (object) convertValue.ToInt16 (provider);
\r
2423 else if (conversionType == conversionTable[8]) // 8 TypeCode.UInt16
\r
2424 return (object) convertValue.ToUInt16 (provider);
\r
2426 else if (conversionType == conversionTable[9]) // 9 TypeCode.Int32
\r
2427 return (object) convertValue.ToInt32 (provider);
\r
2429 else if (conversionType == conversionTable[10]) // 10 TypeCode.UInt32
\r
2430 return (object) convertValue.ToUInt32 (provider);
\r
2432 else if (conversionType == conversionTable[11]) // 11 TypeCode.Int64
\r
2433 return (object) convertValue.ToInt64 (provider);
\r
2435 else if (conversionType == conversionTable[12]) // 12 TypeCode.UInt64
\r
2436 return (object) convertValue.ToUInt64 (provider);
\r
2438 else if (conversionType == conversionTable[13]) // 13 TypeCode.Single
\r
2439 return (object) convertValue.ToSingle (provider);
\r
2441 else if (conversionType == conversionTable[14]) // 14 TypeCode.Double
\r
2442 return (object) convertValue.ToDouble (provider);
\r
2444 else if (conversionType == conversionTable[15]) // 15 TypeCode.Decimal
\r
2445 return (object) convertValue.ToDecimal (provider);
\r
2447 else if (conversionType == conversionTable[16]) // 16 TypeCode.DateTime
\r
2448 return (object) convertValue.ToDateTime (provider);
\r
2450 else if (conversionType == conversionTable[18]) // 18 TypeCode.String
\r
2451 return (object) convertValue.ToString (provider);
\r
2453 throw new InvalidCastException (Locale.GetText ("Unknown target conversion type"));
\r
2455 // Not in the conversion table
\r
2456 throw new ArgumentException ((Locale.GetText (
\r
2457 "Value is not a convertible object: " + value.GetType().ToString() + " to " + conversionType.ToString())));
\r