2010-03-12 Jb Evain <jbevain@novell.com>
[mono.git] / mcs / class / corlib / System / Convert.cs
1 //
2 // System.Convert.cs
3 //
4 // Author:
5 //   Derek Holden (dholden@draper.com)
6 //   Duncan Mak (duncan@ximian.com)
7 //
8 // (C) Ximian, Inc.  http://www.ximian.com
9 //
10 //
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.
15 //
16 // This header and the one above it can be formatted however, just trying
17 // to keep it consistent w/ the existing mcs headers.
18 //
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.
26 //
27 // See http://lists.ximian.com/archives/public/mono-list/2001-July/000525.html
28 //
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.
35 //
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.
41 //
42 // The .NET Framework SDK lists DBNull as a member of this class
43 // as 'public static readonly object DBNull;'. 
44 //
45 // It should also be decided if all the cast return values should be
46 // returned as unchecked or not.
47 //
48 // All the XML function comments were auto generated which is why they
49 // sound someone redundant.
50 //
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
56 // DT   |                 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
68 //
69
70 //
71 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
72 //
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:
80 // 
81 // The above copyright notice and this permission notice shall be
82 // included in all copies or substantial portions of the Software.
83 // 
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.
91 //
92
93 using System.Globalization;
94 using System.IO;
95 using System.Security.Cryptography;
96 using System.Text;
97 using System.Runtime.CompilerServices;
98 using System.Runtime.InteropServices;
99
100 namespace System {
101   
102 //      [CLSCompliant(false)]
103         public static class Convert {
104
105                 // Fields
106                 public static readonly object DBNull = System.DBNull.Value;
107         
108                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
109                 extern static byte [] InternalFromBase64String (string str, bool allowWhitespaceOnly);
110
111                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
112                 extern static byte [] InternalFromBase64CharArray (char [] arr, int offset, int length);
113
114                 public static byte[] FromBase64CharArray (char[] inArray, int offset, int length)
115                 {
116                         if (inArray == null)
117                                 throw new ArgumentNullException ("inArray");
118                         if (offset < 0)
119                                 throw new ArgumentOutOfRangeException ("offset < 0");
120                         if (length < 0)
121                                 throw new ArgumentOutOfRangeException ("length < 0");
122                         // avoid integer overflow
123                         if (offset > inArray.Length - length)
124                                 throw new ArgumentOutOfRangeException ("offset + length > array.Length");
125
126                         return InternalFromBase64CharArray (inArray, offset, length);
127                 }
128
129                 public static byte[] FromBase64String (string s)
130                 {
131                         if (s == null)
132                                 throw new ArgumentNullException ("s");
133
134                         if (s.Length == 0) {
135                                 return new byte[0];
136                         }
137
138                         return InternalFromBase64String (s, true);
139                 }
140
141                 public static TypeCode GetTypeCode (object value)
142                 {
143                         if (value == null)
144                                 return TypeCode.Empty;
145                         else 
146                                 return Type.GetTypeCode (value.GetType ());
147                 }
148
149                 public static bool IsDBNull (object value)
150                 {
151                         if (value is DBNull)
152                                 return true;
153                         else
154                                 return false;
155                 }
156                 
157                 public static int ToBase64CharArray (byte[] inArray, int offsetIn, int length, 
158                                                     char[] outArray, int offsetOut)
159                 {
160                         if (inArray == null)
161                                 throw new ArgumentNullException ("inArray");
162                         if (outArray == null)
163                                 throw new ArgumentNullException ("outArray");
164                         if (offsetIn < 0 || length < 0 || offsetOut < 0)
165                                 throw new ArgumentOutOfRangeException ("offsetIn, length, offsetOut < 0");
166                         // avoid integer overflow
167                         if (offsetIn > inArray.Length - length)
168                                 throw new ArgumentOutOfRangeException ("offsetIn + length > array.Length");
169
170                         // note: normally ToBase64Transform doesn't support multiple block processing
171                         byte[] outArr = ToBase64Transform.InternalTransformFinalBlock (inArray, offsetIn, length);
172                         
173                         char[] cOutArr = new ASCIIEncoding ().GetChars (outArr);
174                         
175                         // avoid integer overflow
176                         if (offsetOut > outArray.Length - cOutArr.Length)
177                                 throw new ArgumentOutOfRangeException ("offsetOut + cOutArr.Length > outArray.Length");
178                         
179                         Array.Copy (cOutArr, 0, outArray, offsetOut, cOutArr.Length);
180                         
181                         return cOutArr.Length;
182                 }
183                 
184                 public static string ToBase64String (byte[] inArray)
185                 {
186                         if (inArray == null)
187                                 throw new ArgumentNullException ("inArray");
188
189                         return ToBase64String (inArray, 0, inArray.Length);
190                 }
191                 
192                 public static string ToBase64String (byte[] inArray, int offset, int length)
193                 {
194                         if (inArray == null)
195                                 throw new ArgumentNullException ("inArray");
196                         if (offset < 0 || length < 0)
197                                 throw new ArgumentOutOfRangeException ("offset < 0 || length < 0");
198                         // avoid integer overflow
199                         if (offset > inArray.Length - length)
200                                 throw new ArgumentOutOfRangeException ("offset + length > array.Length");
201                         
202                         // note: normally ToBase64Transform doesn't support multiple block processing
203                         byte[] outArr = ToBase64Transform.InternalTransformFinalBlock (inArray, offset, length);
204                         
205                         return (new ASCIIEncoding ().GetString (outArr));
206                 }
207
208                 [ComVisible (false)]
209                 public static string ToBase64String (byte[] inArray, Base64FormattingOptions options)
210                 {
211                         if (inArray == null)
212                                 throw new ArgumentNullException ("inArray");
213                         return ToBase64String (inArray, 0, inArray.Length, options);
214                 }
215
216                 [ComVisible (false)]
217                 public static string ToBase64String (byte[] inArray, int offset, int length, Base64FormattingOptions options)
218                 {
219                         if (inArray == null)
220                                 throw new ArgumentNullException ("inArray");
221                         if (offset < 0 || length < 0)
222                                 throw new ArgumentOutOfRangeException ("offset < 0 || length < 0");
223                         // avoid integer overflow
224                         if (offset > inArray.Length - length)
225                                 throw new ArgumentOutOfRangeException ("offset + length > array.Length");
226
227                         if (length == 0)
228                                 return String.Empty;
229
230                         if (options == Base64FormattingOptions.InsertLineBreaks)
231                                 return ToBase64StringBuilderWithLine (inArray, offset, length).ToString ();
232                         else
233                                 return Encoding.ASCII.GetString (ToBase64Transform.InternalTransformFinalBlock (inArray, offset, length));
234                 }
235
236                 [ComVisible (false)]
237                 public static int ToBase64CharArray (byte[] inArray, int offsetIn, int length, 
238                                                     char[] outArray, int offsetOut, Base64FormattingOptions options)
239                 {
240                         if (inArray == null)
241                                 throw new ArgumentNullException ("inArray");
242                         if (outArray == null)
243                                 throw new ArgumentNullException ("outArray");
244                         if (offsetIn < 0 || length < 0 || offsetOut < 0)
245                                 throw new ArgumentOutOfRangeException ("offsetIn, length, offsetOut < 0");
246                         // avoid integer overflow
247                         if (offsetIn > inArray.Length - length)
248                                 throw new ArgumentOutOfRangeException ("offsetIn + length > array.Length");
249
250                         if (length == 0)
251                                 return 0;
252
253                         // note: normally ToBase64Transform doesn't support multiple block processing
254                         if (options == Base64FormattingOptions.InsertLineBreaks) {
255                                 StringBuilder sb = ToBase64StringBuilderWithLine (inArray, offsetIn, length);
256                                 sb.CopyTo (0, outArray, offsetOut, sb.Length);
257                                 return sb.Length;
258                         } else {
259                                 byte[] outArr = ToBase64Transform.InternalTransformFinalBlock (inArray, offsetIn, length);
260                         
261                                 char[] cOutArr = Encoding.ASCII.GetChars (outArr);
262                         
263                                 // avoid integer overflow
264                                 if (offsetOut > outArray.Length - cOutArr.Length)
265                                         throw new ArgumentOutOfRangeException ("offsetOut + cOutArr.Length > outArray.Length");
266                         
267                                 Array.Copy (cOutArr, 0, outArray, offsetOut, cOutArr.Length);
268                                 return cOutArr.Length;
269                         }
270                 }
271
272                 private const int MaxBytesPerLine = 57;
273
274                 static StringBuilder ToBase64StringBuilderWithLine (byte [] inArray, int offset, int length)
275                 {
276                         StringBuilder sb = new StringBuilder ();
277
278                         int remainder;
279                         int full = Math.DivRem (length, MaxBytesPerLine, out remainder);
280                         for (int i = 0; i < full; i ++) {
281                                 byte[] data = ToBase64Transform.InternalTransformFinalBlock (inArray, offset, MaxBytesPerLine);
282                                 sb.AppendLine (Encoding.ASCII.GetString (data));
283                                 offset += MaxBytesPerLine;
284                         }
285                         // we never complete (i.e. the last line) with a new line
286                         if (remainder == 0) {
287                                 int nll = Environment.NewLine.Length;
288                                 sb.Remove (sb.Length - nll, nll);
289                         } else {
290                                 byte[] data = ToBase64Transform.InternalTransformFinalBlock (inArray, offset, remainder);
291                                 sb.Append (Encoding.ASCII.GetString (data));
292                         }
293                         return sb;
294                 }
295                 
296                 // ========== Boolean Conversions ========== //
297         
298                 public static bool ToBoolean (bool value) 
299                 { 
300                         return value; 
301                 }
302
303                 public static bool ToBoolean (byte value) 
304                 { 
305                         return (value != 0); 
306                 }
307  
308                 public static bool ToBoolean (char value)
309                 {
310                         throw new InvalidCastException (Locale.GetText ("Can't convert char to bool"));
311                 }
312                 
313                 public static bool ToBoolean (DateTime value)
314                 {
315                         throw new InvalidCastException (Locale.GetText ("Can't convert date to bool"));
316                 }
317                 
318                 public static bool ToBoolean (decimal value) 
319                 { 
320                         return (value != 0M); 
321                 }
322
323                 public static bool ToBoolean (double value) 
324                 { 
325                         return (value != 0); 
326                 }
327
328                 public static bool ToBoolean (float value) 
329                 { 
330                         return (value != 0f); 
331                 }
332
333                 public static bool ToBoolean (int value) 
334                 { 
335                         return (value != 0); 
336                 }
337
338                 public static bool ToBoolean (long value) 
339                 { 
340                         return (value != 0); 
341                 }
342
343                 [CLSCompliant (false)]
344                 public static bool ToBoolean (sbyte value) 
345                 { 
346                         return (value != 0); 
347                 }
348         
349                 public static bool ToBoolean (short value) 
350                 { 
351                         return (value != 0); 
352                 }
353
354                 public static bool ToBoolean (string value) 
355                 {
356                         if (value == null)
357                                 return false; // LAMESPEC: Spec says throw ArgumentNullException
358                         return Boolean.Parse (value);
359                 }
360
361                 public static bool ToBoolean (string value, IFormatProvider provider)
362                 {
363                         if (value == null)
364                                 return false; // LAMESPEC: Spec says throw ArgumentNullException
365                         return Boolean.Parse (value); // provider is ignored.
366                 }
367
368                 [CLSCompliant (false)]
369                 public static bool ToBoolean (uint value) 
370                 { 
371                         return (value != 0);
372                 }
373
374                 [CLSCompliant (false)]
375                 public static bool ToBoolean (ulong value) 
376                 { 
377                         return (value != 0); 
378                 }
379
380                 [CLSCompliant (false)]
381                 public static bool ToBoolean (ushort value) 
382                 { 
383                         //if (value == null)
384                         //      return false;
385                         return (value != 0); 
386                 }
387
388                 public static bool ToBoolean (object value)
389                 {
390                         if (value == null)
391                                 return false;
392                         return ToBoolean (value, null);
393                 }
394
395                 public static bool ToBoolean (object value, IFormatProvider provider)
396                 {
397                         if (value == null)
398                                 return false;
399                         return ((IConvertible) value).ToBoolean (provider);
400                 }
401
402                 // ========== Byte Conversions ========== //
403         
404                 public static byte ToByte (bool value) 
405                 { 
406                         return (byte)(value ? 1 : 0); 
407                 }
408         
409                 public static byte ToByte (byte value) 
410                 { 
411                         return value; 
412                 }
413
414                 public static byte ToByte (char value) 
415                 { 
416                         if (value > Byte.MaxValue)
417                                 throw new OverflowException (Locale.GetText (
418                                         "Value is greater than Byte.MaxValue"));
419
420                         return (byte)value;
421                 }
422
423                 public static byte ToByte (DateTime value)
424                 {
425                         throw new InvalidCastException ("This conversion is not supported.");
426                 }
427         
428                 public static byte ToByte (decimal value)
429                 { 
430                         if (value > Byte.MaxValue || value < Byte.MinValue)
431                                 throw new OverflowException (Locale.GetText (
432                                         "Value is greater than Byte.MaxValue or less than Byte.MinValue"));
433           
434                         // Returned Even-Rounded
435                         return (byte)(Math.Round (value));
436                 }
437         
438                 public static byte ToByte (double value) 
439                 { 
440                         if (value > Byte.MaxValue || value < Byte.MinValue)
441                                 throw new OverflowException (Locale.GetText (
442                                         "Value is greater than Byte.MaxValue or less than Byte.MinValue"));
443           
444                         // This and the float version of ToByte are the only ones
445                         // the spec listed as checking for .NaN and Infinity overflow
446                         if (Double.IsNaN(value) || Double.IsInfinity(value))
447                                 throw new OverflowException (Locale.GetText (
448                                         "Value is equal to Double.NaN, Double.PositiveInfinity, or Double.NegativeInfinity"));
449
450                         // Returned Even-Rounded
451                         return (byte)(Math.Round (value));
452                 }
453
454                 public static byte ToByte (float value) 
455                 { 
456                         if (value > Byte.MaxValue || value < Byte.MinValue)
457                                 throw new OverflowException (Locale.GetText (
458                                         "Value is greater than Byte.MaxValue or less than Byte.Minalue"));
459
460                         // This and the double version of ToByte are the only ones
461                         // the spec listed as checking for .NaN and Infinity overflow
462                         if (Single.IsNaN(value) || Single.IsInfinity(value))
463                                 throw new OverflowException (Locale.GetText (
464                                         "Value is equal to Single.NaN, Single.PositiveInfinity, or Single.NegativeInfinity"));
465           
466                         // Returned Even-Rounded, pass it as a double, could have this
467                         // method just call Convert.ToByte ( (double)value)
468                         return (byte)(Math.Round ( (double)value));
469                 }
470
471                 public static byte ToByte (int value) 
472                 { 
473                         if (value > Byte.MaxValue || value < Byte.MinValue)
474                                 throw new OverflowException (Locale.GetText (
475                                         "Value is greater than Byte.MaxValue or less than Byte.MinValue"));
476           
477                         return (byte)value; 
478                 }
479
480                 public static byte ToByte (long value) 
481                 { 
482                         if (value > Byte.MaxValue || value < Byte.MinValue)
483                                 throw new OverflowException (Locale.GetText (
484                                         "Value is greater than Byte.MaxValue or less than Byte.MinValue"));
485           
486                         return (byte)value;
487                 }
488
489                 [CLSCompliant (false)]
490                 public static byte ToByte (sbyte value) 
491                 { 
492                         if (value < Byte.MinValue)
493                                 throw new OverflowException (Locale.GetText (
494                                         "Value is less than Byte.MinValue"));
495           
496                         return (byte)value;
497                 }
498         
499                 public static byte ToByte (short value) 
500                 { 
501                         if (value > Byte.MaxValue || value < Byte.MinValue)
502                                 throw new OverflowException (Locale.GetText (
503                                         "Value is greater than Byte.MaxValue or less than Byte.MinValue"));
504           
505                         return (byte)value; 
506                 }
507
508                 public static byte ToByte (string value) 
509                 {
510                         if (value == null)
511                                 return 0; // LAMESPEC: Spec says throw ArgumentNullException
512                         return Byte.Parse (value);
513                 }
514
515                 public static byte ToByte (string value, IFormatProvider provider) 
516                 {
517                         if (value == null)
518                                 return 0; // LAMESPEC: Spec says throw ArgumentNullException
519                         return Byte.Parse (value, provider);
520                 }
521
522                 public static byte ToByte (string value, int fromBase)
523                 {
524                         int retVal = ConvertFromBase (value, fromBase, true);
525
526                         if (retVal < (int) Byte.MinValue || retVal > (int) Byte.MaxValue)
527                                 throw new OverflowException ();
528                         else
529                                 return (byte) retVal;
530                 }
531
532                 [CLSCompliant (false)]
533                 public static byte ToByte (uint value) 
534                 { 
535                         if (value > Byte.MaxValue)
536                                 throw new OverflowException (Locale.GetText (
537                                         "Value is greater than Byte.MaxValue"));
538
539                         return (byte)value;
540                 }
541
542                 [CLSCompliant (false)]
543                 public static byte ToByte (ulong value) 
544                 { 
545                         if (value > Byte.MaxValue)
546                                 throw new OverflowException (Locale.GetText (
547                                         "Value is greater than Byte.MaxValue"));
548
549                         return (byte)value;
550                 }
551
552                 [CLSCompliant (false)]
553                 public static byte ToByte (ushort value) 
554                 { 
555                         if (value > Byte.MaxValue)
556                                 throw new OverflowException (Locale.GetText (
557                                         "Value is greater than Byte.MaxValue"));
558
559                         return (byte)value;
560                 }
561
562                 public static byte ToByte (object value)
563                 {
564                         if (value == null)
565                                 return 0;
566                         return ToByte (value, null);
567                 }
568
569                 public static byte ToByte (object value, IFormatProvider provider)
570                 {
571                         if (value == null)
572                                 return 0;
573                         return ((IConvertible) value).ToByte (provider);
574                 }
575
576                 // ========== Char Conversions ========== //
577
578                 public static char ToChar (bool value)
579                 {
580                         throw new InvalidCastException ("This conversion is not supported.");
581                 }
582                 
583                 public static char ToChar (byte value) 
584                 { 
585                         return (char)value;
586                 }
587
588                 public static char ToChar (char value) 
589                 { 
590                         return value;
591                 }
592
593                 public static char ToChar (DateTime value)
594                 {
595                         throw new InvalidCastException ("This conversion is not supported.");
596                 }
597
598                 public static char ToChar (decimal value)
599                 {
600                         throw new InvalidCastException ("This conversion is not supported.");
601                 }
602
603                 public static char ToChar (double value)
604                 {
605                         throw new InvalidCastException ("This conversion is not supported.");
606                 }
607                 
608                 public static char ToChar (int value) 
609                 { 
610                         if (value > Char.MaxValue || value < Char.MinValue)
611                                 throw new OverflowException (Locale.GetText (
612                                         "Value is greater than Char.MaxValue or less than Char.MinValue"));
613           
614                         return (char)value; 
615                 }
616
617                 public static char ToChar (long value) 
618                 { 
619                         if (value > Char.MaxValue || value < Char.MinValue)
620                                 throw new OverflowException (Locale.GetText (
621                                         "Value is greater than Char.MaxValue or less than Char.MinValue"));
622           
623                         return (char)value; 
624                 }
625
626                 public static char ToChar (float value)
627                 {
628                         throw new InvalidCastException ("This conversion is not supported.");
629                 }
630
631                 [CLSCompliant (false)]
632                 public static char ToChar (sbyte value) 
633                 { 
634                         if (value < Char.MinValue)
635                                 throw new OverflowException (Locale.GetText (
636                                         "Value is less than Char.MinValue"));
637           
638                         return (char)value; 
639                 }
640         
641                 public static char ToChar (short value) 
642                 { 
643                         if (value < Char.MinValue)
644                                 throw new OverflowException (Locale.GetText (
645                                         "Value is less than Char.MinValue"));
646           
647                         return (char)value; 
648                 }
649
650                 public static char ToChar (string value) 
651                 {
652                         return Char.Parse (value);
653                 }
654
655                 public static char ToChar (string value, IFormatProvider provider)
656                 {
657                         return Char.Parse (value); // provider is ignored.
658                 }
659
660                 [CLSCompliant (false)]
661                 public static char ToChar (uint value) 
662                 { 
663                         if (value > Char.MaxValue)
664                                 throw new OverflowException (Locale.GetText (
665                                         "Value is greater than Char.MaxValue"));
666           
667                         return (char)value; 
668                 }
669
670                 [CLSCompliant (false)]
671                 public static char ToChar (ulong value) 
672                 { 
673                         if (value > Char.MaxValue)
674                                 throw new OverflowException (Locale.GetText (
675                                         "Value is greater than Char.MaxValue"));
676           
677                         return (char)value; 
678                 }
679
680                 [CLSCompliant (false)]
681                 public static char ToChar (ushort value) 
682                 { 
683                         return (char)value; 
684                 }
685
686                 public static char ToChar (object value)
687                 {
688                         if (value == null)
689                                 return '\0';
690                         return ToChar (value, null);
691                 }
692
693                 public static char ToChar (object value, IFormatProvider provider)
694                 {
695                         if (value == null)
696                                 return '\0';
697                         return ((IConvertible) value).ToChar (provider);
698                 }
699
700                 // ========== DateTime Conversions ========== //
701         
702                 public static DateTime ToDateTime (string value) 
703                 { 
704                         if (value == null)
705                                 return DateTime.MinValue; // LAMESPEC: Spec says throw ArgumentNullException
706                         return DateTime.Parse (value);
707                 }
708         
709                 public static DateTime ToDateTime (string value, IFormatProvider provider) 
710                 {
711                         if (value == null)
712                                 return DateTime.MinValue; // LAMESPEC: Spec says throw ArgumentNullException
713                         return DateTime.Parse (value, provider);
714                 }
715
716                 public static DateTime ToDateTime (bool value)
717                 {
718                         throw new InvalidCastException ("This conversion is not supported.");
719                 }
720
721                 public static DateTime ToDateTime (byte value)
722                 {
723                         throw new InvalidCastException ("This conversion is not supported.");
724                 }
725
726                 public static DateTime ToDateTime (char value)
727                 {
728                         throw new InvalidCastException ("This conversion is not supported.");
729                 }
730
731                 public static DateTime ToDateTime (DateTime value)
732                 {
733                         return value;
734                 }
735
736                 public static DateTime ToDateTime (decimal value)
737                 {
738                         throw new InvalidCastException ("This conversion is not supported.");
739                 }
740
741                 public static DateTime ToDateTime (double value)
742                 {
743                         throw new InvalidCastException ("This conversion is not supported.");
744                 }
745
746                 public static DateTime ToDateTime (short value)
747                 {
748                         throw new InvalidCastException ("This conversion is not supported.");
749                 }
750
751                 public static DateTime ToDateTime (int value)
752                 {
753                         throw new InvalidCastException ("This conversion is not supported.");
754                 }
755
756                 public static DateTime ToDateTime (long value)
757                 {
758                         throw new InvalidCastException ("This conversion is not supported.");
759                 }
760
761                 public static DateTime ToDateTime (float value)
762                 {
763                         throw new InvalidCastException ("This conversion is not supported.");
764                 }
765
766                 public static DateTime ToDateTime (object value)
767                 {
768                         if (value == null)
769                                 return DateTime.MinValue;
770                         return ToDateTime (value, null);
771                 }
772
773                 public static DateTime ToDateTime (object value, IFormatProvider provider)
774                 {
775                         if (value == null)
776                                 return DateTime.MinValue;
777                         return ((IConvertible) value).ToDateTime (provider);
778                 }
779
780                 [CLSCompliant (false)]
781                 public static DateTime ToDateTime (sbyte value)
782                 {
783                         throw new InvalidCastException ("This conversion is not supported.");
784                 }
785                 [CLSCompliant (false)]
786                 public static DateTime ToDateTime (ushort value)
787                 {
788                         throw new InvalidCastException ("This conversion is not supported.");
789                 }
790
791                 [CLSCompliant (false)]
792                 public static DateTime ToDateTime (uint value)
793                 {
794                         throw new InvalidCastException ("This conversion is not supported.");
795                 }
796
797                 [CLSCompliant (false)]
798                 public static DateTime ToDateTime (ulong value)
799                 {
800                         throw new InvalidCastException ("This conversion is not supported.");
801                 }
802
803                 // ========== Decimal Conversions ========== //
804         
805                 public static decimal ToDecimal (bool value) 
806                 { 
807                         return value ? 1 : 0; 
808                 }
809         
810                 public static decimal ToDecimal (byte value) 
811                 { 
812                         return (decimal)value; 
813                 }
814
815                 public static decimal ToDecimal (char value)
816                 {
817                         throw new InvalidCastException ("This conversion is not supported.");
818                 }
819
820                 public static decimal ToDecimal (DateTime value)
821                 {
822                         throw new InvalidCastException ("This conversion is not supported.");
823                 }
824                                 
825                 public static decimal ToDecimal (decimal value) 
826                 { 
827                         return value; 
828                 }
829
830                 public static decimal ToDecimal (double value) 
831                 { 
832                         return (decimal) value; 
833                 }
834
835                 public static decimal ToDecimal (float value) 
836                 {
837                         return (decimal) value;
838                 }
839
840                 public static decimal ToDecimal (int value) 
841                 { 
842                         return (decimal)value; 
843                 }
844         
845                 public static decimal ToDecimal (long value) 
846                 { 
847                         return (decimal)value; 
848                 }
849
850                 [CLSCompliant (false)]
851                 public static decimal ToDecimal (sbyte value) 
852                 { 
853                         return (decimal)value; 
854                 }
855         
856                 public static decimal ToDecimal (short value) 
857                 { 
858                         return (decimal)value; 
859                 }
860
861                 public static decimal ToDecimal (string value) 
862                 {
863                         if (value == null)
864                                 return new Decimal (0); // LAMESPEC: Spec says throw ArgumentNullException
865                         return Decimal.Parse (value);
866                 }
867
868                 public static decimal ToDecimal (string value, IFormatProvider provider) 
869                 {
870                         if (value == null)
871                                 return new Decimal (0); // LAMESPEC: Spec says throw ArgumentNullException
872                         return Decimal.Parse (value, provider);
873                 }
874
875                 [CLSCompliant (false)]
876                 public static decimal ToDecimal (uint value) 
877                 { 
878                         return (decimal)value; 
879                 }
880
881                 [CLSCompliant (false)]
882                 public static decimal ToDecimal (ulong value) 
883                 { 
884                         return (decimal)value; 
885                 }
886
887                 [CLSCompliant (false)]
888                 public static decimal ToDecimal (ushort value) 
889                 { 
890                         return (decimal)value; 
891                 }
892
893                 public static decimal ToDecimal (object value)
894                 {
895                         if (value == null)
896                                 return new Decimal (0);
897                         return ToDecimal (value, null);
898                 }
899
900                 public static decimal ToDecimal (object value, IFormatProvider provider)
901                 {
902                         if (value == null)
903                                 return new Decimal (0);
904                         return ((IConvertible) value).ToDecimal (provider);
905                 }
906                                                  
907
908                 // ========== Double Conversions ========== //
909         
910                 public static double ToDouble (bool value) 
911                 { 
912                         return value ? 1 : 0; 
913                 }
914         
915                 public static double ToDouble (byte value) 
916                 { 
917                         return (double) value;
918                 }
919
920                 public static double ToDouble (char value)
921                 {
922                         throw new InvalidCastException ("This conversion is not supported.");
923                 }
924
925                 public static double ToDouble (DateTime value)
926                 {
927                         throw new InvalidCastException ("This conversion is not supported.");
928                 }
929         
930                 public static double ToDouble (decimal value) 
931                 { 
932                         return (double)value; 
933                 }
934
935                 public static double ToDouble (double value) 
936                 { 
937                         return value; 
938                 }
939
940                 public static double ToDouble (float value) 
941                 { 
942                         return (double) value;
943                 }
944
945                 public static double ToDouble (int value) 
946                 { 
947                         return (double)value; 
948                 }
949         
950                 public static double ToDouble (long value) 
951                 { 
952                         return (double)value; 
953                 }
954
955                 [CLSCompliant (false)]
956                 public static double ToDouble (sbyte value) 
957                 { 
958                         return (double)value; 
959                 }
960         
961                 public static double ToDouble (short value) 
962                 { 
963                         return (double)value; 
964                 }
965
966                 public static double ToDouble (string value) 
967                 {
968                         if (value == null)
969                                 return 0.0; // LAMESPEC: Spec says throw ArgumentNullException
970                         return Double.Parse (value);
971                 }
972
973                 public static double ToDouble (string value, IFormatProvider provider) 
974                 {
975                         if (value == null)
976                                 return 0.0; // LAMESPEC: Spec says throw ArgumentNullException
977                         return Double.Parse (value, provider);
978                 }
979
980                 [CLSCompliant (false)]
981                 public static double ToDouble (uint value) 
982                 { 
983                         return (double)value; 
984                 }
985
986                 [CLSCompliant (false)]
987                 public static double ToDouble (ulong value) 
988                 { 
989                         return (double)value; 
990                 }
991
992                 [CLSCompliant (false)]
993                 public static double ToDouble (ushort value) 
994                 { 
995                         return (double)value; 
996                 }
997
998                 public static double ToDouble (object value)
999                 {
1000                         if (value == null)
1001                                 return 0.0;
1002                         return ToDouble (value, null);
1003                 }
1004
1005                 public static double ToDouble (object value, IFormatProvider provider)
1006                 {
1007                         if (value == null)
1008                                 return 0.0;
1009                         return ((IConvertible) value).ToDouble (provider);
1010                 }
1011
1012                 // ========== Int16 Conversions ========== //
1013
1014                 public static short ToInt16 (bool value) 
1015                 { 
1016                         return (short)(value ? 1 : 0); 
1017                 }
1018         
1019                 public static short ToInt16 (byte value) 
1020                 { 
1021                         return (short)value; 
1022                 }
1023
1024                 public static short ToInt16 (char value) 
1025                 {
1026                         if (value > Int16.MaxValue) 
1027                                 throw new OverflowException (Locale.GetText (
1028                                         "Value is greater than Int16.MaxValue"));
1029
1030                         return (short)value;
1031                 }
1032
1033                 public static short ToInt16 (DateTime value) 
1034                 {
1035                         throw new InvalidCastException ("This conversion is not supported.");
1036                 }
1037         
1038                 public static short ToInt16 (decimal value) 
1039                 { 
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"));
1043           
1044                         // Returned Even-Rounded
1045                         return (short)(Math.Round (value));       
1046                 }
1047
1048                 public static short ToInt16 (double value) 
1049                 { 
1050                         if (value > Int16.MaxValue || value < Int16.MinValue) 
1051                                 throw new OverflowException (Locale.GetText (
1052                                         "Value is greater than Int16.MaxValue or less than Int16.MinValue"));
1053           
1054                         // Returned Even-Rounded
1055                         return (short)(Math.Round (value));       
1056                 }
1057  
1058                 public static short ToInt16 (float value) 
1059                 { 
1060                         if (value > Int16.MaxValue || value < Int16.MinValue) 
1061                                 throw new OverflowException (Locale.GetText (
1062                                         "Value is greater than Int16.MaxValue or less than Int16.MinValue"));
1063           
1064                         // Returned Even-Rounded, use Math.Round pass as a double.
1065                         return (short)Math.Round ( (double)value);
1066                 }
1067
1068                 public static short ToInt16 (int value) 
1069                 { 
1070                         if (value > Int16.MaxValue || value < Int16.MinValue) 
1071                                 throw new OverflowException (Locale.GetText (
1072                                         "Value is greater than Int16.MaxValue or less than Int16.MinValue"));
1073
1074                         return (short)value; 
1075                 }
1076         
1077                 public static short ToInt16 (long value) 
1078                 { 
1079                         if (value > Int16.MaxValue || value < Int16.MinValue) 
1080                                 throw new OverflowException (Locale.GetText (
1081                                         "Value is greater than Int16.MaxValue or less than Int16.MinValue"));
1082
1083                         return (short)value; 
1084                 }
1085
1086                 [CLSCompliant (false)]
1087                 public static short ToInt16 (sbyte value) 
1088                 { 
1089                         return (short)value; 
1090                 }
1091         
1092                 public static short ToInt16 (short value) 
1093                 { 
1094                         return value; 
1095                 }
1096
1097                 public static short ToInt16 (string value) 
1098                 {
1099                         if (value == null)
1100                                 return 0; // LAMESPEC: Spec says throw ArgumentNullException
1101                         return Int16.Parse (value);
1102                 }
1103
1104                 public static short ToInt16 (string value, IFormatProvider provider) 
1105                 {
1106                         if (value == null)
1107                                 return 0; // LAMESPEC: Spec says throw ArgumentNullException
1108                         return Int16.Parse (value, provider);
1109                 }
1110
1111                 public static short ToInt16 (string value, int fromBase)
1112                 {
1113                         int result = ConvertFromBase (value, fromBase, false);
1114                         if (fromBase != 10) {
1115                                 if (result > ushort.MaxValue) {
1116                                         throw new OverflowException ("Value was either too large or too small for an Int16.");
1117                                 }
1118
1119                                 // note: no sign are available to detect negatives
1120                                 if (result > Int16.MaxValue) {
1121                                         // return negative 2's complement
1122                                         return Convert.ToInt16 (-(65536 - result));
1123                                 }
1124                         }
1125                         return Convert.ToInt16 (result);
1126                 }
1127
1128                 [CLSCompliant (false)]
1129                 public static short ToInt16 (uint value) 
1130                 { 
1131                         if (value > Int16.MaxValue) 
1132                                 throw new OverflowException (Locale.GetText (
1133                                         "Value is greater than Int16.MaxValue"));
1134
1135                         return (short)value; 
1136                 }
1137
1138                 [CLSCompliant (false)]
1139                 public static short ToInt16 (ulong value) 
1140                 { 
1141                         if (value > (ulong)Int16.MaxValue) 
1142                                 throw new OverflowException (Locale.GetText (
1143                                         "Value is greater than Int16.MaxValue"));
1144                         return (short)value; 
1145                 }
1146
1147                 [CLSCompliant (false)]
1148                 public static short ToInt16 (ushort value) 
1149                 { 
1150                         if (value > Int16.MaxValue) 
1151                                 throw new OverflowException (Locale.GetText (
1152                                         "Value is greater than Int16.MaxValue"));
1153
1154                         return (short)value; 
1155                 }
1156
1157                 public static short ToInt16 (object value)
1158                 {
1159                         if (value == null)
1160                                 return 0;
1161                         return ToInt16 (value, null);
1162                 }
1163
1164                 public static short ToInt16 (object value, IFormatProvider provider)
1165                 {
1166                         if (value == null)
1167                                 return 0;
1168                         return ((IConvertible) value).ToInt16 (provider);
1169                 }
1170         
1171                 // ========== Int32 Conversions ========== //
1172
1173                 public static int ToInt32 (bool value) 
1174                 { 
1175                         return value ? 1 : 0; 
1176                 }
1177         
1178                 public static int ToInt32 (byte value) 
1179                 { 
1180                         return (int)value; 
1181                 }
1182
1183                 public static int ToInt32 (char value) 
1184                 { 
1185                         return (int)value; 
1186                 }
1187
1188                 public static int ToInt32 (DateTime value)
1189                 {
1190                         throw new InvalidCastException ("This conversion is not supported.");
1191                 }
1192         
1193                 public static int ToInt32 (decimal value) 
1194                 { 
1195                         if (value > Int32.MaxValue || value < Int32.MinValue) 
1196                                 throw new OverflowException (Locale.GetText (
1197                                         "Value is greater than Int32.MaxValue or less than Int32.MinValue"));
1198
1199                         // Returned Even-Rounded
1200                         return (int)(Math.Round (value));         
1201                 }
1202
1203                 public static int ToInt32 (double value) 
1204                 { 
1205                         if (value > Int32.MaxValue || value < Int32.MinValue) 
1206                                 throw new OverflowException (Locale.GetText (
1207                                         "Value is greater than Int32.MaxValue or less than Int32.MinValue"));
1208           
1209                         // Returned Even-Rounded
1210                         return (int)(Math.Round (value));         
1211                 }
1212  
1213                 public static int ToInt32 (float value) 
1214                 { 
1215                         if (value > Int32.MaxValue || value < Int32.MinValue) 
1216                                 throw new OverflowException (Locale.GetText (
1217                                         "Value is greater than Int32.MaxValue or less than Int32.MinValue"));
1218           
1219                         // Returned Even-Rounded, pass as a double, could just call
1220                         // Convert.ToInt32 ( (double)value);
1221                         return (int)(Math.Round ( (double)value));
1222                 }
1223
1224                 public static int ToInt32 (int value) 
1225                 { 
1226                         return value; 
1227                 }
1228         
1229                 public static int ToInt32 (long value) 
1230                 { 
1231                         if (value > Int32.MaxValue || value < Int32.MinValue) 
1232                                 throw new OverflowException (Locale.GetText (
1233                                         "Value is greater than Int32.MaxValue or less than Int32.MinValue"));
1234
1235                         return (int)value; 
1236                 }
1237
1238                 [CLSCompliant (false)]
1239                 public static int ToInt32 (sbyte value) 
1240                 { 
1241                         return (int)value; 
1242                 }
1243         
1244                 public static int ToInt32 (short value) 
1245                 { 
1246                         return (int)value; 
1247                 }
1248
1249                 public static int ToInt32 (string value) 
1250                 {
1251                         if (value == null)
1252                                 return 0; // LAMESPEC: Spec says throw ArgumentNullException
1253                         return Int32.Parse (value);
1254                 }
1255
1256                 public static int ToInt32 (string value, IFormatProvider provider) 
1257                 {
1258                         if (value == null)
1259                                 return 0; // LAMESPEC: Spec says throw ArgumentNullException
1260                         return Int32.Parse (value, provider);
1261                 }
1262
1263                 
1264                 public static int ToInt32 (string value, int fromBase)
1265                 {
1266                         return ConvertFromBase (value, fromBase, false);
1267                 }
1268                 
1269                 [CLSCompliant (false)]
1270                 public static int ToInt32 (uint value) 
1271                 { 
1272                         if (value > Int32.MaxValue) 
1273                                 throw new OverflowException (Locale.GetText (
1274                                         "Value is greater than Int32.MaxValue"));
1275
1276                         return (int)value; 
1277                 }
1278
1279                 [CLSCompliant (false)]
1280                 public static int ToInt32 (ulong value) 
1281                 { 
1282                         if (value > Int32.MaxValue) 
1283                                 throw new OverflowException (Locale.GetText (
1284                                         "Value is greater than Int32.MaxValue"));
1285
1286                         return (int)value; 
1287                 }
1288
1289                 [CLSCompliant (false)]
1290                 public static int ToInt32 (ushort value) 
1291                 { 
1292                         return (int)value; 
1293                 }
1294
1295                 public static int ToInt32 (object value)
1296                 {
1297                         if (value == null)
1298                                 return 0;
1299                         return ToInt32 (value, null);
1300                 }
1301
1302                 public static int ToInt32 (object value, IFormatProvider provider)
1303                 {
1304                         if (value == null)
1305                                 return 0;
1306                         return ((IConvertible) value).ToInt32 (provider);
1307                 }
1308
1309                 // ========== Int64 Conversions ========== //
1310
1311                 public static long ToInt64 (bool value) 
1312                 { 
1313                         return value ? 1 : 0; 
1314                 }
1315         
1316                 public static long ToInt64 (byte value) 
1317                 { 
1318                         return (long)(ulong)value; 
1319                 }
1320
1321                 public static long ToInt64 (char value) 
1322                 { 
1323                         return (long)value; 
1324                 }
1325
1326                 public static long ToInt64 (DateTime value)
1327                 {
1328                         throw new InvalidCastException ("This conversion is not supported.");
1329                 }
1330         
1331                 public static long ToInt64 (decimal value) 
1332                 { 
1333                         if (value > Int64.MaxValue || value < Int64.MinValue) 
1334                                 throw new OverflowException (Locale.GetText (
1335                                         "Value is greater than Int64.MaxValue or less than Int64.MinValue"));
1336           
1337                         // Returned Even-Rounded
1338                         return (long)(Math.Round (value));        
1339                 }
1340
1341                 public static long ToInt64 (double value) 
1342                 { 
1343                         if (value > Int64.MaxValue || value < Int64.MinValue) 
1344                                 throw new OverflowException (Locale.GetText (
1345                                         "Value is greater than Int64.MaxValue or less than Int64.MinValue"));
1346           
1347                         // Returned Even-Rounded
1348                         return (long)(Math.Round (value));
1349                 }
1350  
1351                 public static long ToInt64 (float value) 
1352                 { 
1353                         if (value > Int64.MaxValue || value < Int64.MinValue) 
1354                                 throw new OverflowException (Locale.GetText (
1355                                         "Value is greater than Int64.MaxValue or less than Int64.MinValue"));
1356           
1357                         // Returned Even-Rounded, pass to Math as a double, could
1358                         // just call Convert.ToInt64 ( (double)value);
1359                         return (long)(Math.Round ( (double)value));
1360                 }
1361
1362                 public static long ToInt64 (int value) 
1363                 { 
1364                         return (long)value; 
1365                 }
1366         
1367                 public static long ToInt64 (long value) 
1368                 { 
1369                         return value; 
1370                 }
1371
1372                 [CLSCompliant (false)]
1373                 public static long ToInt64 (sbyte value) 
1374                 { 
1375                         return (long)value; 
1376                 }
1377         
1378                 public static long ToInt64 (short value) 
1379                 { 
1380                         return (long)value; 
1381                 }
1382
1383                 public static long ToInt64 (string value) 
1384                 {
1385                         if (value == null)
1386                                 return 0; // LAMESPEC: Spec says throw ArgumentNullException
1387                         return Int64.Parse (value);
1388                 }
1389
1390                 public static long ToInt64 (string value, IFormatProvider provider) 
1391                 {
1392                         if (value == null)
1393                                 return 0; // LAMESPEC: Spec says throw ArgumentNullException
1394                         return Int64.Parse (value, provider);
1395                 }
1396
1397                 public static long ToInt64 (string value, int fromBase)
1398                 {
1399                         return ConvertFromBase64 (value, fromBase, false);
1400                 }
1401
1402                 [CLSCompliant (false)]
1403                 public static long ToInt64 (uint value) 
1404                 { 
1405                         return (long)(ulong)value; 
1406                 }
1407
1408                 [CLSCompliant (false)]
1409                 public static long ToInt64 (ulong value) 
1410                 { 
1411                         if (value > Int64.MaxValue) 
1412                                 throw new OverflowException (Locale.GetText (
1413                                         "Value is greater than Int64.MaxValue"));
1414
1415                         return (long)value; 
1416                 }
1417
1418                 [CLSCompliant (false)]
1419                 public static long ToInt64 (ushort value) 
1420                 { 
1421                         return (long)(ulong)value; 
1422                 }
1423
1424                 public static long ToInt64 (object value)
1425                 {
1426                         if (value == null)
1427                                 return 0;
1428                         return ToInt64 (value, null);
1429                 }
1430
1431                 public static long ToInt64 (object value, IFormatProvider provider)
1432                 {
1433                         if (value == null)
1434                                 return 0;
1435                         return ((IConvertible) value).ToInt64 (provider);
1436                 }
1437                 
1438                 // ========== SByte Conversions ========== //
1439
1440                 [CLSCompliant (false)]
1441                 public static sbyte ToSByte (bool value) 
1442                 { 
1443                         return (sbyte)(value ? 1 : 0); 
1444                 }
1445
1446                 [CLSCompliant (false)]
1447                 public static sbyte ToSByte (byte value) 
1448                 { 
1449                         if (value > SByte.MaxValue)
1450                                 throw new OverflowException (Locale.GetText (
1451                                         "Value is greater than SByte.MaxValue"));
1452
1453                         return (sbyte)value; 
1454                 }
1455
1456                 [CLSCompliant (false)]
1457                 public static sbyte ToSByte (char value) 
1458                 { 
1459                         if (value > SByte.MaxValue)
1460                                 throw new OverflowException (Locale.GetText (
1461                                         "Value is greater than SByte.MaxValue"));
1462
1463                         return (sbyte)value;
1464                 }
1465
1466                 [CLSCompliant (false)]
1467                 public static sbyte ToSByte (DateTime value)
1468                 {
1469                         throw new InvalidCastException ("This conversion is not supported.");
1470                 }
1471                 
1472                 [CLSCompliant (false)]  
1473                 public static sbyte ToSByte (decimal value) 
1474                 { 
1475                         if (value > SByte.MaxValue || value < SByte.MinValue)
1476                                 throw new OverflowException (Locale.GetText (
1477                                         "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
1478           
1479                         // Returned Even-Rounded
1480                         return (sbyte)(Math.Round (value));
1481                 }
1482
1483                 [CLSCompliant (false)]
1484                 public static sbyte ToSByte (double value) 
1485                 { 
1486                         if (value > SByte.MaxValue || value < SByte.MinValue)
1487                                 throw new OverflowException (Locale.GetText (
1488                                         "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
1489
1490                         // Returned Even-Rounded
1491                         return (sbyte)(Math.Round (value));
1492                 }
1493
1494                 [CLSCompliant (false)]
1495                 public static sbyte ToSByte (float value) 
1496                 { 
1497                         if (value > SByte.MaxValue || value < SByte.MinValue)
1498                                 throw new OverflowException (Locale.GetText (
1499                                         "Value is greater than SByte.MaxValue or less than SByte.Minalue"));
1500
1501                         // Returned Even-Rounded, pass as double to Math
1502                         return (sbyte)(Math.Round ( (double)value));
1503                 }
1504
1505                 [CLSCompliant (false)]
1506                 public static sbyte ToSByte (int value) 
1507                 { 
1508                         if (value > SByte.MaxValue || value < SByte.MinValue)
1509                                 throw new OverflowException (Locale.GetText (
1510                                         "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
1511           
1512                         return (sbyte)value; 
1513                 }
1514
1515                 [CLSCompliant (false)]
1516                 public static sbyte ToSByte (long value) 
1517                 { 
1518                         if (value > SByte.MaxValue || value < SByte.MinValue)
1519                                 throw new OverflowException (Locale.GetText (
1520                                         "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
1521           
1522                         return (sbyte)value;
1523                 }
1524
1525                 [CLSCompliant (false)]
1526                 public static sbyte ToSByte (sbyte value) 
1527                 { 
1528                         return value;
1529                 }
1530
1531                 [CLSCompliant (false)]
1532                 public static sbyte ToSByte (short value) 
1533                 { 
1534                         if (value > SByte.MaxValue || value < SByte.MinValue)
1535                                 throw new OverflowException (Locale.GetText (
1536                                         "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
1537           
1538                         return (sbyte)value; 
1539                 }
1540
1541                 [CLSCompliant (false)]
1542                 public static sbyte ToSByte (string value) 
1543                 {
1544                         if (value == null)
1545                                 return 0; // LAMESPEC: Spec says throw ArgumentNullException
1546                         return SByte.Parse (value);
1547                 }
1548                 
1549                 [CLSCompliant (false)]
1550                 public static sbyte ToSByte (string value, IFormatProvider provider) 
1551                 {
1552                         if (value == null)
1553                                 throw new ArgumentNullException ("value");
1554                         return SByte.Parse (value, provider);
1555                 }
1556
1557                 [CLSCompliant (false)]
1558                 public static sbyte ToSByte (string value, int fromBase)
1559                 {
1560                         int result = ConvertFromBase (value, fromBase, false);
1561                         if (fromBase != 10) {
1562                                 // note: no sign are available to detect negatives
1563                                 if (result > SByte.MaxValue) {
1564                                         // return negative 2's complement
1565                                         return Convert.ToSByte (-(256 - result));
1566                                 }
1567                         }
1568                         return Convert.ToSByte (result);
1569                 }
1570                 
1571                 [CLSCompliant (false)]
1572                 public static sbyte ToSByte (uint value) 
1573                 { 
1574                         if (value > SByte.MaxValue)
1575                                 throw new OverflowException (Locale.GetText (
1576                                         "Value is greater than SByte.MaxValue"));
1577
1578                         return (sbyte)value;
1579                 }
1580
1581                 [CLSCompliant (false)]
1582                 public static sbyte ToSByte (ulong value) 
1583                 { 
1584                         if (value > (ulong)SByte.MaxValue)
1585                                 throw new OverflowException (Locale.GetText (
1586                                         "Value is greater than SByte.MaxValue"));
1587
1588                         return (sbyte)value;
1589                 }
1590
1591                 [CLSCompliant (false)]
1592                 public static sbyte ToSByte (ushort value) 
1593                 { 
1594                         if (value > SByte.MaxValue)
1595                                 throw new OverflowException (Locale.GetText (
1596                                         "Value is greater than SByte.MaxValue"));
1597
1598                         return (sbyte)value;
1599                 }
1600
1601                 [CLSCompliant (false)]
1602                 public static sbyte ToSByte (object value)
1603                 {
1604                         if (value == null)
1605                                 return 0;
1606                         return ToSByte (value, null);
1607                 }
1608
1609                 [CLSCompliant (false)]
1610                 public static sbyte ToSByte (object value, IFormatProvider provider)
1611                 {
1612                         if (value == null)
1613                                 return 0;
1614                         return ((IConvertible) value).ToSByte (provider);
1615                 }
1616
1617                 // ========== Single Conversions ========== //
1618         
1619                 public static float ToSingle (bool value) 
1620                 { 
1621                         return value ? 1 : 0; 
1622                 }
1623         
1624                 public static float ToSingle (byte value) 
1625                 { 
1626                         return (float)value; 
1627                 }
1628
1629                 public static float ToSingle (Char value)
1630                 {
1631                         throw new InvalidCastException ("This conversion is not supported.");
1632                 }
1633
1634                 public static float ToSingle (DateTime value)
1635                 {
1636                         throw new InvalidCastException ("This conversion is not supported.");
1637                 }
1638         
1639                 public static float ToSingle (decimal value) 
1640                 { 
1641                         return (float)value; 
1642                 }
1643
1644                 public static float ToSingle (double value) 
1645                 { 
1646                         return (float)value; 
1647                 }
1648         
1649                 public static float ToSingle (float value) 
1650                 { 
1651                         return value; 
1652                 }
1653
1654                 public static float ToSingle (int value) 
1655                 { 
1656                         return (float)value; 
1657                 }
1658         
1659                 public static float ToSingle (long value) 
1660                 { 
1661                         return (float)value; 
1662                 }
1663
1664                 [CLSCompliant (false)]
1665                 public static float ToSingle (sbyte value) 
1666                 { 
1667                         return (float)value; 
1668                 }
1669         
1670                 public static float ToSingle (short value) 
1671                 { 
1672                         return (float)value; 
1673                 }
1674
1675                 public static float ToSingle (string value) 
1676                 {
1677                         if (value == null)
1678                                 return 0.0f; // LAMESPEC: Spec says throw ArgumentNullException
1679                         return Single.Parse (value);
1680                 }
1681
1682                 public static float ToSingle (string value, IFormatProvider provider) 
1683                 {
1684                         if (value == null)
1685                                 return 0.0f; // LAMESPEC: Spec says throw ArgumentNullException
1686                         return Single.Parse (value, provider);
1687                 }              
1688
1689                 [CLSCompliant (false)]
1690                 public static float ToSingle (uint value) 
1691                 { 
1692                         return (float)value; 
1693                 }
1694
1695                 [CLSCompliant (false)]
1696                 public static float ToSingle (ulong value) 
1697                 { 
1698                         return (float)value; 
1699                 }
1700
1701                 [CLSCompliant (false)]
1702                 public static float ToSingle (ushort value) 
1703                 { 
1704                         return (float)value; 
1705                 }
1706
1707                 public static float ToSingle (object value)
1708                 {
1709                         if (value == null)
1710                                 return 0.0f;
1711                         return ToSingle (value, null);
1712                 }
1713
1714 //              [CLSCompliant (false)]
1715                 public static float ToSingle (object value, IFormatProvider provider)
1716                 {
1717                         if (value == null)
1718                                 return 0.0f;
1719                         return ((IConvertible) value).ToSingle (provider);
1720                 }
1721
1722                 // ========== String Conversions ========== //
1723         
1724                 public static string ToString (bool value) 
1725                 { 
1726                         return value.ToString (); 
1727                 }
1728
1729                 public static string ToString (bool value, IFormatProvider provider)
1730                 {
1731                         return value.ToString (); // the same as ToString (bool).
1732                 }
1733         
1734                 public static string ToString (byte value) 
1735                 { 
1736                         return value.ToString (); 
1737                 }
1738         
1739                 public static string ToString (byte value, IFormatProvider provider) 
1740                 {
1741                         return value.ToString (provider); 
1742                 }
1743
1744                 public static string ToString (byte value, int toBase)
1745                 {
1746                         if (value == 0)
1747                                 return "0";
1748                         if (toBase == 10)
1749                                 return value.ToString ();
1750                         
1751                         byte[] val = BitConverter.GetBytes (value);
1752
1753                         switch (toBase) {
1754                         case 2:
1755                                 return ConvertToBase2 (val);
1756                         case 8:
1757                                 return ConvertToBase8 (val);
1758                         case 16:
1759                                 return ConvertToBase16 (val);
1760                         default:
1761                                 throw new ArgumentException (Locale.GetText ("toBase is not valid."));
1762                         }
1763                 }
1764
1765                 public static string ToString (char value) 
1766                 { 
1767                         return value.ToString (); 
1768                 }
1769
1770                 public static string ToString (char value, IFormatProvider provider)
1771                 {
1772                         return value.ToString (); // the same as ToString (char)
1773                 }
1774
1775                 public static string ToString (DateTime value) 
1776                 { 
1777                         return value.ToString (); 
1778                 }
1779
1780                 public static string ToString (DateTime value, IFormatProvider provider) 
1781                 { 
1782                         return value.ToString (provider); 
1783                 }
1784
1785                 public static string ToString (decimal value) 
1786                 {
1787                         return value.ToString ();
1788                 }
1789
1790                 public static string ToString (decimal value, IFormatProvider provider) 
1791                 { 
1792                         return value.ToString (provider); 
1793                 }
1794         
1795                 public static string ToString (double value) 
1796                 { 
1797                         return value.ToString (); 
1798                 }
1799
1800                 public static string ToString (double value, IFormatProvider provider) 
1801                 { 
1802                         return value.ToString (provider);
1803                 }
1804         
1805                 public static string ToString (float value) 
1806                 { 
1807                         return value.ToString (); 
1808                 }
1809
1810                 public static string ToString (float value, IFormatProvider provider) 
1811                 { 
1812                         return value.ToString (provider); 
1813                 }
1814
1815                 public static string ToString (int value) 
1816                 { 
1817                         return value.ToString (); 
1818                 }
1819
1820                 public static string ToString (int value, int toBase)
1821                 {
1822                         if (value == 0)
1823                                 return "0";
1824                         if (toBase == 10)
1825                                 return value.ToString ();
1826                         
1827                         byte[] val = BitConverter.GetBytes (value);
1828
1829                         switch (toBase) {
1830                         case 2:
1831                                 return ConvertToBase2 (val);
1832                         case 8:
1833                                 return ConvertToBase8 (val);
1834                         case 16:
1835                                 return ConvertToBase16 (val);
1836                         default:
1837                                 throw new ArgumentException (Locale.GetText ("toBase is not valid."));
1838                         }
1839                 }
1840
1841                 public static string ToString (int value, IFormatProvider provider) 
1842                 { 
1843                         return value.ToString (provider); 
1844                 }
1845         
1846                 public static string ToString (long value) 
1847                 { 
1848                         return value.ToString (); 
1849                 }
1850
1851                 public static string ToString (long value, int toBase)
1852                 {
1853                         if (value == 0)
1854                                 return "0";
1855                         if (toBase == 10)
1856                                 return value.ToString ();
1857                         
1858                         byte[] val = BitConverter.GetBytes (value);
1859
1860                         switch (toBase) {
1861                         case 2:
1862                                 return ConvertToBase2 (val);
1863                         case 8:
1864                                 return ConvertToBase8 (val);
1865                         case 16:
1866                                 return ConvertToBase16 (val);
1867                         default:
1868                                 throw new ArgumentException (Locale.GetText ("toBase is not valid."));
1869                         }
1870                 }
1871
1872                 public static string ToString (long value, IFormatProvider provider) 
1873                 { 
1874                         return value.ToString (provider); 
1875                 }
1876
1877                 public static string ToString (object value)
1878                 {
1879                         return ToString (value, null);
1880                 }               
1881
1882                 public static string ToString (object value, IFormatProvider provider)
1883                 {
1884                         if (value is IConvertible)
1885                                 return ((IConvertible) value).ToString (provider);
1886                         else if (value != null)
1887                                 return value.ToString ();
1888                         return String.Empty;
1889                 }                               
1890
1891                 [CLSCompliant (false)]
1892                 public static string ToString (sbyte value) 
1893                 { 
1894                         return value.ToString (); 
1895                 }
1896
1897                 [CLSCompliant (false)]                          
1898                 public static string ToString (sbyte value, IFormatProvider provider) 
1899                 { 
1900                         return value.ToString (provider); 
1901                 }
1902         
1903                 public static string ToString (short value) 
1904                 { 
1905                         return value.ToString (); 
1906                 }
1907
1908                 public static string ToString (short value, int toBase)
1909                 {
1910                         if (value == 0)
1911                                 return "0";
1912                         if (toBase == 10)
1913                                 return value.ToString ();
1914                         
1915                         byte[] val = BitConverter.GetBytes (value);
1916
1917                         switch (toBase) {
1918                         case 2:
1919                                 return ConvertToBase2 (val);
1920                         case 8:
1921                                 return ConvertToBase8 (val);
1922                         case 16:
1923                                 return ConvertToBase16 (val);
1924                         default:
1925                                 throw new ArgumentException (Locale.GetText ("toBase is not valid."));
1926                         }
1927                 }
1928
1929                 public static string ToString (short value, IFormatProvider provider) 
1930                 { 
1931                         return value.ToString (provider); 
1932                 }
1933
1934                 public static string ToString (string value) 
1935                 {
1936                         return value;
1937                 }
1938
1939                 public static string ToString (string value, IFormatProvider provider)
1940                 {
1941                         return value; // provider is ignored.
1942                 }
1943
1944                 [CLSCompliant (false)]
1945                 public static string ToString (uint value) 
1946                 { 
1947                         return value.ToString (); 
1948                 }
1949
1950                 [CLSCompliant (false)]
1951                 public static string ToString (uint value, IFormatProvider provider) 
1952                 { 
1953                         return value.ToString (provider); 
1954                 }
1955
1956                 [CLSCompliant (false)]
1957                 public static string ToString (ulong value) 
1958                 { 
1959                         return value.ToString (); 
1960                 }
1961
1962                 [CLSCompliant (false)]
1963                 public static string ToString (ulong value, IFormatProvider provider) 
1964                 { 
1965                         return value.ToString (provider); 
1966                 }
1967
1968                 [CLSCompliant (false)]
1969                 public static string ToString (ushort value) 
1970                 { 
1971                         return value.ToString (); 
1972                 }
1973
1974                 [CLSCompliant (false)]
1975                 public static string ToString (ushort value, IFormatProvider provider) 
1976                 { 
1977                         return value.ToString (provider); 
1978                 }
1979                 
1980                 // ========== UInt16 Conversions ========== //
1981
1982                 [CLSCompliant (false)]
1983                 public static ushort ToUInt16 (bool value) 
1984                 { 
1985                         return (ushort)(value ? 1 : 0); 
1986                 }
1987
1988                 [CLSCompliant (false)]
1989                 public static ushort ToUInt16 (byte value) 
1990                 { 
1991                         return (ushort)value; 
1992                 }
1993
1994                 [CLSCompliant (false)]
1995                 public static ushort ToUInt16 (char value) 
1996                 { 
1997                         return (ushort)value; 
1998                 }
1999
2000                 [CLSCompliant (false)]
2001                 public static ushort ToUInt16 (DateTime value)
2002                 {
2003                         throw new InvalidCastException ("This conversion is not supported.");
2004                 }
2005
2006                 [CLSCompliant (false)]
2007                 public static ushort ToUInt16 (decimal value) 
2008                 { 
2009                         if (value > UInt16.MaxValue || value < UInt16.MinValue) 
2010                                 throw new OverflowException (Locale.GetText (
2011                                         "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
2012           
2013                         // Returned Even-Rounded
2014                         return (ushort)(Math.Round (value));      
2015                 }
2016
2017                 [CLSCompliant (false)]
2018                 public static ushort ToUInt16 (double value) 
2019                 { 
2020                         if (value > UInt16.MaxValue || value < UInt16.MinValue) 
2021                                 throw new OverflowException (Locale.GetText (
2022                                         "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
2023           
2024                         // Returned Even-Rounded
2025                         return (ushort)(Math.Round (value));
2026                 }
2027
2028                 [CLSCompliant (false)]
2029                 public static ushort ToUInt16 (float value) 
2030                 { 
2031                         if (value > UInt16.MaxValue || value < UInt16.MinValue) 
2032                                 throw new OverflowException (Locale.GetText (
2033                                         "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
2034           
2035                         // Returned Even-Rounded, pass as double to Math
2036                         return (ushort)(Math.Round ( (double)value));
2037                 }
2038
2039                 [CLSCompliant (false)]
2040                 public static ushort ToUInt16 (int value) 
2041                 { 
2042                         if (value > UInt16.MaxValue || value < UInt16.MinValue) 
2043                                 throw new OverflowException (Locale.GetText (
2044                                         "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
2045
2046                         return (ushort)value; 
2047                 }
2048
2049                 [CLSCompliant (false)]
2050                 public static ushort ToUInt16 (long value) 
2051                 { 
2052                         if (value > UInt16.MaxValue || value < UInt16.MinValue) 
2053                                 throw new OverflowException (Locale.GetText (
2054                                         "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
2055
2056                         return (ushort)value; 
2057                 }
2058
2059                 [CLSCompliant (false)]
2060                 public static ushort ToUInt16 (sbyte value) 
2061                 { 
2062                         if (value < UInt16.MinValue) 
2063                                 throw new OverflowException (Locale.GetText (
2064                                         "Value is less than UInt16.MinValue"));
2065
2066                         return (ushort)value; 
2067                 }
2068
2069                 [CLSCompliant (false)]
2070                 public static ushort ToUInt16 (short value) 
2071                 { 
2072                         if (value < UInt16.MinValue) 
2073                                 throw new OverflowException (Locale.GetText (
2074                                         "Value is less than UInt16.MinValue"));
2075
2076                         return (ushort)value; 
2077                 }
2078                 
2079                 [CLSCompliant (false)]
2080                 public static ushort ToUInt16 (string value) 
2081                 {
2082                         if (value == null)
2083                                 return 0; // LAMESPEC: Spec says throw ArgumentNullException
2084                         return UInt16.Parse (value);
2085                 }
2086
2087                 [CLSCompliant (false)]
2088                 public static ushort ToUInt16 (string value, IFormatProvider provider) 
2089                 {
2090                         if (value == null)
2091                                 return 0; // LAMESPEC: Spec says throw ArgumentNullException
2092                         return UInt16.Parse (value, provider);
2093                 }
2094
2095                 [CLSCompliant (false)]
2096                 public static ushort ToUInt16 (string value, int fromBase) 
2097                 {
2098                         return ToUInt16 (ConvertFromBase (value, fromBase, true));
2099                 } 
2100
2101                 [CLSCompliant (false)]
2102                 public static ushort ToUInt16 (uint value) 
2103                 { 
2104                         if (value > UInt16.MaxValue) 
2105                                 throw new OverflowException (Locale.GetText (
2106                                         "Value is greater than UInt16.MaxValue"));
2107
2108                         return (ushort)value; 
2109                 }
2110
2111                 [CLSCompliant (false)]
2112                 public static ushort ToUInt16 (ulong value) 
2113                 { 
2114                         if (value > (ulong)UInt16.MaxValue) 
2115                                 throw new OverflowException (Locale.GetText (
2116                                         "Value is greater than UInt16.MaxValue"));
2117
2118                         return (ushort)value; 
2119                 }
2120
2121                 [CLSCompliant (false)]
2122                 public static ushort ToUInt16 (ushort value) 
2123                 { 
2124                         return value; 
2125                 }
2126
2127                 [CLSCompliant (false)]
2128                 public static ushort ToUInt16 (object value)
2129                 {
2130                         if (value == null)
2131                                 return 0;
2132                         return ToUInt16 (value, null);
2133                 }
2134
2135                 [CLSCompliant (false)]
2136                 public static ushort ToUInt16 (object value, IFormatProvider provider)
2137                 {
2138                         if (value == null)
2139                                 return 0;
2140                         return ((IConvertible) value).ToUInt16 (provider);
2141                 }
2142
2143                 // ========== UInt32 Conversions ========== //
2144
2145                 [CLSCompliant (false)]
2146                 public static uint ToUInt32 (bool value) 
2147                 { 
2148                         return (uint)(value ? 1 : 0); 
2149                 }
2150
2151                 [CLSCompliant (false)]
2152                 public static uint ToUInt32 (byte value) 
2153                 { 
2154                         return (uint)value; 
2155                 }
2156
2157                 [CLSCompliant (false)]
2158                 public static uint ToUInt32 (char value) 
2159                 { 
2160                         return (uint)value; 
2161                 }
2162
2163                 [CLSCompliant (false)]
2164                 public static uint ToUInt32 (DateTime value)
2165                 {
2166                         throw new InvalidCastException ("This conversion is not supported.");
2167                 }
2168                 
2169                 [CLSCompliant (false)]
2170                 public static uint ToUInt32 (decimal value) 
2171                 { 
2172                         if (value > UInt32.MaxValue || value < UInt32.MinValue) 
2173                                 throw new OverflowException (Locale.GetText (
2174                                         "Value is greater than UInt32.MaxValue or less than UInt32.MinValue"));
2175           
2176                         // Returned Even-Rounded
2177                         return (uint)(Math.Round (value));        
2178                 }
2179
2180                 [CLSCompliant (false)]
2181                 public static uint ToUInt32 (double value) 
2182                 { 
2183                         if (value > UInt32.MaxValue || value < UInt32.MinValue) 
2184                                 throw new OverflowException (Locale.GetText (
2185                                         "Value is greater than UInt32.MaxValue or less than UInt32.MinValue"));
2186           
2187                         // Returned Even-Rounded
2188                         return (uint)(Math.Round (value));        
2189                 }
2190
2191                 [CLSCompliant (false)]
2192                 public static uint ToUInt32 (float value) 
2193                 { 
2194                         if (value > UInt32.MaxValue || value < UInt32.MinValue) 
2195                                 throw new OverflowException (Locale.GetText (
2196                                         "Value is greater than UInt32.MaxValue or less than UInt32.MinValue"));
2197           
2198                         // Returned Even-Rounded, pass as double to Math
2199                         return (uint)(Math.Round ( (double)value));
2200                 }
2201
2202                 [CLSCompliant (false)]
2203                 public static uint ToUInt32 (int value) 
2204                 { 
2205                         if (value < UInt32.MinValue) 
2206                                 throw new OverflowException (Locale.GetText (
2207                                         "Value is less than UInt32.MinValue"));
2208
2209                         return (uint)value; 
2210                 }
2211
2212                 [CLSCompliant (false)]
2213                 public static uint ToUInt32 (long value) 
2214                 { 
2215                         if (value > UInt32.MaxValue || value < UInt32.MinValue) 
2216                                 throw new OverflowException (Locale.GetText (
2217                                         "Value is greater than UInt32.MaxValue or less than UInt32.MinValue"));
2218
2219                         return (uint)value; 
2220                 }
2221
2222                 [CLSCompliant (false)]
2223                 public static uint ToUInt32 (sbyte value) 
2224                 { 
2225                         if (value < UInt32.MinValue) 
2226                                 throw new OverflowException (Locale.GetText (
2227                                         "Value is less than UInt32.MinValue"));
2228
2229                         return (uint)value; 
2230                 }
2231
2232                 [CLSCompliant (false)]
2233                 public static uint ToUInt32 (short value) 
2234                 { 
2235                         if (value < UInt32.MinValue) 
2236                                 throw new OverflowException (Locale.GetText (
2237                                         "Value is less than UInt32.MinValue"));
2238
2239                         return (uint)value; 
2240                 }
2241
2242                 [CLSCompliant (false)]
2243                 public static uint ToUInt32 (string value) 
2244                 {
2245                         if (value == null)
2246                                 return 0; // LAMESPEC: Spec says throw ArgumentNullException
2247                         return UInt32.Parse (value);
2248                 }
2249
2250                 [CLSCompliant (false)]
2251                 public static uint ToUInt32 (string value, IFormatProvider provider) 
2252                 {
2253                         if (value == null)
2254                                 return 0; // LAMESPEC: Spec says throw ArgumentNullException
2255                         return UInt32.Parse (value, provider);
2256                 }
2257
2258                 [CLSCompliant (false)]
2259                 public static uint ToUInt32 (string value, int fromBase)
2260                 {
2261                         return (uint) ConvertFromBase (value, fromBase, true);
2262                 }
2263
2264                 [CLSCompliant (false)]
2265                 public static uint ToUInt32 (uint value) 
2266                 { 
2267                         return value; 
2268                 }
2269
2270                 [CLSCompliant (false)]
2271                 public static uint ToUInt32 (ulong value) 
2272                 { 
2273                         if (value > UInt32.MaxValue) 
2274                                 throw new OverflowException (Locale.GetText (
2275                                         "Value is greater than UInt32.MaxValue"));
2276
2277                         return (uint)value; 
2278                 }
2279
2280                 [CLSCompliant (false)]
2281                 public static uint ToUInt32 (ushort value) 
2282                 { 
2283                         return (uint)value; 
2284                 }
2285
2286                 [CLSCompliant (false)]
2287                 public static uint ToUInt32 (object value)
2288                 {
2289                         if (value == null)
2290                                 return 0;
2291                         return ToUInt32 (value, null);
2292                 }               
2293
2294                 [CLSCompliant (false)]
2295                 public static uint ToUInt32 (object value, IFormatProvider provider)
2296                 {
2297                         if (value == null)
2298                                 return 0;
2299                         return ((IConvertible) value).ToUInt32 (provider);
2300                 }               
2301                 
2302
2303                 // ========== UInt64 Conversions ========== //
2304
2305                 [CLSCompliant (false)]
2306                 public static ulong ToUInt64 (bool value) 
2307                 { 
2308                         return (ulong)(value ? 1 : 0); 
2309                 }
2310
2311                 [CLSCompliant (false)]
2312                 public static ulong ToUInt64 (byte value) 
2313                 { 
2314                         return (ulong)value; 
2315                 }
2316
2317                 [CLSCompliant (false)]
2318                 public static ulong ToUInt64 (char value) 
2319                 { 
2320                         return (ulong)value; 
2321                 }
2322
2323                 [CLSCompliant (false)]
2324                 public static ulong ToUInt64 (DateTime value)
2325                 {
2326                         throw new InvalidCastException ("The conversion is not supported.");
2327                 }
2328
2329                 [CLSCompliant (false)]
2330                 public static ulong ToUInt64 (decimal value) 
2331                 { 
2332                         if (value > UInt64.MaxValue || value < UInt64.MinValue) 
2333                                 throw new OverflowException (Locale.GetText (
2334                                         "Value is greater than UInt64.MaxValue or less than UInt64.MinValue"));
2335           
2336                         // Returned Even-Rounded
2337                         return (ulong)(Math.Round (value));       
2338                 }
2339
2340                 [CLSCompliant (false)]
2341                 public static ulong ToUInt64 (double value) 
2342                 { 
2343                         if (value > UInt64.MaxValue || value < UInt64.MinValue) 
2344                                 throw new OverflowException (Locale.GetText (
2345                                         "Value is greater than UInt64.MaxValue or less than UInt64.MinValue"));
2346           
2347                         // Returned Even-Rounded
2348                         return (ulong)(Math.Round (value));       
2349                 }
2350                 
2351                 [CLSCompliant (false)] 
2352                 public static ulong ToUInt64 (float value) 
2353                 { 
2354                         if (value > UInt64.MaxValue || value < UInt64.MinValue) 
2355                                 throw new OverflowException (Locale.GetText (
2356                                         "Value is greater than UInt64.MaxValue or less than UInt64.MinValue"));
2357           
2358                         // Returned Even-Rounded, pass as a double to Math
2359                         return (ulong)(Math.Round ( (double)value));
2360                 }
2361                 
2362                 [CLSCompliant (false)]
2363                 public static ulong ToUInt64 (int value) 
2364                 { 
2365                         if (value < (int)UInt64.MinValue) 
2366                                 throw new OverflowException (Locale.GetText (
2367                                         "Value is less than UInt64.MinValue"));
2368
2369                         return (ulong)value; 
2370                 }
2371                 
2372                 [CLSCompliant (false)]
2373                 public static ulong ToUInt64 (long value) 
2374                 { 
2375                         if (value < (long)UInt64.MinValue) 
2376                                 throw new OverflowException (Locale.GetText (
2377                                         "Value is less than UInt64.MinValue"));
2378
2379                         return (ulong)value; 
2380                 }
2381
2382                 [CLSCompliant (false)]
2383                 public static ulong ToUInt64 (sbyte value) 
2384                 { 
2385                         if (value < (sbyte)UInt64.MinValue) 
2386                                 throw new OverflowException
2387                                 ("Value is less than UInt64.MinValue");
2388
2389                         return (ulong)value; 
2390                 }
2391                 
2392                 [CLSCompliant (false)]  
2393                 public static ulong ToUInt64 (short value) 
2394                 { 
2395                         if (value < (short)UInt64.MinValue) 
2396                                 throw new OverflowException (Locale.GetText (
2397                                         "Value is less than UInt64.MinValue"));
2398
2399                         return (ulong)value; 
2400                 }
2401
2402                 [CLSCompliant (false)]
2403                 public static ulong ToUInt64 (string value) 
2404                 {
2405                         if (value == null)
2406                                 return 0; // LAMESPEC: Spec says throw ArgumentNullException
2407                         return UInt64.Parse (value);
2408                 }
2409
2410                 [CLSCompliant (false)]
2411                 public static ulong ToUInt64 (string value, IFormatProvider provider) 
2412                 {
2413                         if (value == null)
2414                                 return 0; // LAMESPEC: Spec says throw ArgumentNullException
2415                         return UInt64.Parse (value, provider);
2416                 }
2417
2418                 [CLSCompliant (false)]
2419                 public static ulong ToUInt64 (string value, int fromBase)
2420                 {
2421                         return (ulong) ConvertFromBase64 (value, fromBase, true);
2422                 }
2423
2424                 [CLSCompliant (false)]
2425                 public static ulong ToUInt64 (uint value) 
2426                 { 
2427                         return (ulong)value; 
2428                 }
2429
2430                 [CLSCompliant (false)]
2431                 public static ulong ToUInt64 (ulong value) 
2432                 { 
2433                         return value; 
2434                 }
2435                 
2436                 [CLSCompliant (false)]
2437                 public static ulong ToUInt64 (ushort value) 
2438                 { 
2439                         return (ulong)value; 
2440                 }
2441
2442                 [CLSCompliant (false)]
2443                 public static ulong ToUInt64 (object value)
2444                 {
2445                         if (value == null)
2446                                 return 0;
2447                         return ToUInt64 (value, null);
2448                 }               
2449
2450                 [CLSCompliant (false)]
2451                 public static ulong ToUInt64 (object value, IFormatProvider provider)
2452                 {
2453                         if (value == null)
2454                                 return 0;
2455                         return ((IConvertible) value).ToUInt64 (provider);
2456                 }               
2457                 
2458
2459                 // ========== Conversion / Helper Functions ========== //
2460
2461                 public static object ChangeType (object value, Type conversionType)
2462                 {
2463                         if ((value != null) && (conversionType == null))
2464                                 throw new ArgumentNullException ("conversionType");
2465                         CultureInfo ci = CultureInfo.CurrentCulture;
2466                         IFormatProvider provider;
2467                         if (conversionType == typeof(DateTime)) {
2468                                 provider = ci.DateTimeFormat;
2469                         }
2470                         else {
2471                                 provider = ci.NumberFormat;
2472                         }
2473                         return ToType (value, conversionType, provider, true);
2474                 }
2475                 
2476                 public static object ChangeType (object value, TypeCode typeCode)
2477                 {
2478                         CultureInfo ci = CultureInfo.CurrentCulture;
2479                         Type conversionType = conversionTable [(int) typeCode];
2480                         IFormatProvider provider;
2481                         if (conversionType == typeof(DateTime)) {
2482                                 provider = ci.DateTimeFormat;
2483                         }
2484                         else {
2485                                 provider = ci.NumberFormat;
2486                         }
2487                         return ToType (value, conversionType, provider, true);
2488                 }
2489
2490                 public static object ChangeType (object value, Type conversionType, IFormatProvider provider)
2491                 {
2492                         if ((value != null) && (conversionType == null))
2493                                 throw new ArgumentNullException ("conversionType");
2494                         return ToType (value, conversionType, provider, true);
2495                 }
2496                 
2497                 public static object ChangeType (object value, TypeCode typeCode, IFormatProvider provider)
2498                 {
2499                         Type conversionType = conversionTable [(int)typeCode];
2500                         return ToType (value, conversionType, provider, true);
2501                 }
2502
2503                 private static bool NotValidBase (int value)
2504                 {
2505                         if ((value == 2) || (value == 8) ||
2506                            (value == 10) || (value == 16))
2507                                 return false;
2508                         
2509                         return true;
2510                 }
2511
2512                 private static int ConvertFromBase (string value, int fromBase, bool unsigned)
2513                 {
2514                         if (NotValidBase (fromBase))
2515                                 throw new ArgumentException ("fromBase is not valid.");
2516                         if (value == null)
2517                                 return 0;
2518
2519                         int chars = 0;
2520                         int result = 0;
2521                         int digitValue;
2522
2523                         int i=0; 
2524                         int len = value.Length;
2525                         bool negative = false;
2526
2527                         // special processing for some bases
2528                         switch (fromBase) {
2529                                 case 10:
2530                                         if (value.Substring (i, 1) == "-") {
2531                                                 if (unsigned) {
2532                                                         throw new OverflowException (
2533                                                                 Locale.GetText ("The string was being parsed as"
2534                                                                 + " an unsigned number and could not have a"
2535                                                                 + " negative sign."));
2536                                                 }
2537                                                 negative = true;
2538                                                 i++;
2539                                         }
2540                                         break;
2541                                 case 16:
2542                                         if (value.Substring (i, 1) == "-") {
2543                                                 throw new ArgumentException ("String cannot contain a "
2544                                                         + "minus sign if the base is not 10.");
2545                                         }
2546                                         if (len >= i + 2) {
2547                                                 // 0x00 or 0X00
2548                                                 if ((value[i] == '0') && ((value [i+1] == 'x') || (value [i+1] == 'X'))) {
2549                                                         i+=2;
2550                                                 }
2551                                         }
2552                                         break;
2553                                 default:
2554                                         if (value.Substring (i, 1) == "-") {
2555                                                 throw new ArgumentException ("String cannot contain a "
2556                                                         + "minus sign if the base is not 10.");
2557                                         }
2558                                         break;
2559                         }
2560
2561                         if (len == i) {
2562                                 throw new FormatException ("Could not find any parsable digits.");
2563                         }
2564
2565                         if (value[i] == '+') {
2566                                 i++;
2567                         }
2568
2569                         while (i < len) {
2570                                 char c = value[i++];
2571                                 if (Char.IsNumber (c)) {
2572                                         digitValue = c - '0';
2573                                 } else if (Char.IsLetter (c)) {
2574                                         digitValue = Char.ToLowerInvariant (c) - 'a' + 10;
2575                                 } else {
2576                                         if (chars > 0) {
2577                                                 throw new FormatException ("Additional unparsable "
2578                                                         + "characters are at the end of the string.");
2579                                         } else {
2580                                                 throw new FormatException ("Could not find any parsable"
2581                                                         + " digits.");
2582                                         }
2583                                 }
2584
2585                                 if (digitValue >= fromBase) {
2586                                         if (chars > 0) {
2587                                                 throw new FormatException ("Additional unparsable "
2588                                                         + "characters are at the end of the string.");
2589                                         } else {
2590                                                 throw new FormatException ("Could not find any parsable"
2591                                                         + " digits.");
2592                                         }
2593                                 }
2594
2595                                 result = (fromBase) * result + digitValue;
2596                                 chars ++;
2597                         }
2598
2599                         if (chars == 0)
2600                                 throw new FormatException ("Could not find any parsable digits.");
2601
2602                         if (negative)
2603                                 return -result;
2604                         else
2605                                 return result;
2606                 }
2607
2608                 // note: this has nothing to do with base64 encoding (just base and Int64)
2609                 private static long ConvertFromBase64 (string value, int fromBase, bool unsigned)
2610                 {
2611                         if (NotValidBase (fromBase))
2612                                 throw new ArgumentException ("fromBase is not valid.");
2613                         if (value == null)
2614                                 return 0;
2615
2616                         int chars = 0;
2617                         int digitValue = -1;
2618                         long result = 0;
2619                         bool negative = false;
2620
2621                         int i = 0;
2622                         int len = value.Length;
2623
2624                         // special processing for some bases
2625                         switch (fromBase) {
2626                                 case 10:
2627                                         if (value.Substring(i, 1) == "-") {
2628                                                 if (unsigned) {
2629                                                         throw new OverflowException (
2630                                                                 Locale.GetText ("The string was being parsed as"
2631                                                                 + " an unsigned number and could not have a"
2632                                                                 + " negative sign."));
2633                                                 }
2634                                                 negative = true;
2635                                                 i++;
2636                                         }
2637                                         break;
2638                                 case 16:
2639                                         if (value.Substring (i, 1) == "-") {
2640                                                 throw new ArgumentException ("String cannot contain a "
2641                                                         + "minus sign if the base is not 10.");
2642                                         }
2643                                         if (len >= i + 2) {
2644                                                 // 0x00 or 0X00
2645                                                 if ((value[i] == '0') && ((value[i + 1] == 'x') || (value[i + 1] == 'X'))) {
2646                                                         i += 2;
2647                                                 }
2648                                         }
2649                                         break;
2650                                 default:
2651                                         if (value.Substring (i, 1) == "-") {
2652                                                 throw new ArgumentException ("String cannot contain a "
2653                                                         + "minus sign if the base is not 10.");
2654                                         }
2655                                         break;
2656                         }
2657
2658                         if (len == i) {
2659                                 throw new FormatException ("Could not find any parsable digits.");
2660                         }
2661
2662                         if (value[i] == '+') {
2663                                 i++;
2664                         }
2665
2666                         while (i < len) {
2667                                 char c = value[i++];
2668                                 if (Char.IsNumber (c)) {
2669                                         digitValue = c - '0';
2670                                 } else if (Char.IsLetter (c)) {
2671                                         digitValue = Char.ToLowerInvariant (c) - 'a' + 10;
2672                                 } else {
2673                                         if (chars > 0) {
2674                                                 throw new FormatException ("Additional unparsable "
2675                                                         + "characters are at the end of the string.");
2676                                         } else {
2677                                                 throw new FormatException ("Could not find any parsable"
2678                                                         + " digits.");
2679                                         }
2680                                 }
2681
2682                                 if (digitValue >= fromBase) {
2683                                         if (chars > 0) {
2684                                                 throw new FormatException ("Additional unparsable "
2685                                                         + "characters are at the end of the string.");
2686                                         } else {
2687                                                 throw new FormatException ("Could not find any parsable"
2688                                                         + " digits.");
2689                                         }
2690                                 }
2691
2692                                 result = (fromBase * result + digitValue);
2693                                 chars ++;
2694                         }
2695
2696                         if (chars == 0)
2697                                 throw new FormatException ("Could not find any parsable digits.");
2698
2699                         if (negative)
2700                                 return -1 * result;
2701                         else
2702                                 return result;
2703                 }
2704
2705                 private static void EndianSwap (ref byte[] value)
2706                 {
2707                         byte[] buf = new byte[value.Length];
2708                         for (int i = 0; i < value.Length; i++)
2709                                 buf[i] = value[value.Length-1-i];
2710                         value = buf;
2711                 }
2712
2713                 private static string ConvertToBase2 (byte[] value)
2714                 {
2715                         if (!BitConverter.IsLittleEndian)
2716                                 EndianSwap (ref value);
2717                         StringBuilder sb = new StringBuilder ();
2718                         for (int i = value.Length - 1; i >= 0; i--) {
2719                                 byte b = value [i];
2720                                 for (int j = 0; j < 8; j++) {
2721                                         if ((b & 0x80) == 0x80) {
2722                                                 sb.Append ('1');
2723                                         }
2724                                         else {
2725                                                 if (sb.Length > 0)
2726                                                         sb.Append ('0');
2727                                         }
2728                                         b <<= 1;
2729                                 }
2730                         }
2731                         return sb.ToString ();
2732                 }
2733
2734                 private static string ConvertToBase8 (byte[] value)
2735                 {
2736                         ulong l = 0;
2737                         switch (value.Length) {
2738                         case 1:
2739                                 l = (ulong) value [0];
2740                                 break;
2741                         case 2:
2742                                 l = (ulong) BitConverter.ToUInt16 (value, 0);
2743                                 break;
2744                         case 4:
2745                                 l = (ulong) BitConverter.ToUInt32 (value, 0);
2746                                 break;
2747                         case 8:
2748                                 l = BitConverter.ToUInt64 (value, 0);
2749                                 break;
2750                         default:
2751                                 throw new ArgumentException ("value");
2752                         }
2753
2754                         StringBuilder sb = new StringBuilder ();
2755                         for (int i = 21; i >= 0; i--) {
2756                                 // 3 bits at the time
2757                                 char val = (char) ((l >> i * 3) & 0x7);
2758                                 if ((val != 0) || (sb.Length > 0)) {
2759                                         val += '0';
2760                                         sb.Append (val);
2761                                 }
2762                         }
2763                         return sb.ToString ();
2764                 }
2765
2766                 private static string ConvertToBase16 (byte[] value)
2767                 {
2768                         if (!BitConverter.IsLittleEndian)
2769                                 EndianSwap (ref value);
2770                         StringBuilder sb = new StringBuilder ();
2771                         for (int i = value.Length - 1; i >= 0; i--) {
2772                                 char high = (char)((value[i] >> 4) & 0x0f);
2773                                 if ((high != 0) || (sb.Length > 0)) {
2774                                         if (high < 10) 
2775                                                 high += '0';
2776                                         else {
2777                                                 high -= (char) 10;
2778                                                 high += 'a';
2779                                         }
2780                                         sb.Append (high);
2781                                 }
2782
2783                                 char low = (char)(value[i] & 0x0f);
2784                                 if ((low != 0) || (sb.Length > 0)) {
2785                                         if (low < 10)
2786                                                 low += '0';
2787                                         else {
2788                                                 low -= (char) 10;
2789                                                 low += 'a';
2790                                         }
2791                                         sb.Append (low);
2792                                 }
2793                         }
2794                         return sb.ToString ();
2795                 }
2796
2797                 // Lookup table for the conversion ToType method. Order
2798                 // is important! Used by ToType for comparing the target
2799                 // type, and uses hardcoded array indexes.
2800                 private static readonly Type[] conversionTable = {
2801                         // Valid ICovnertible Types
2802                         null,               //  0 empty
2803                         typeof (object),   //  1 TypeCode.Object
2804                         typeof (DBNull),   //  2 TypeCode.DBNull
2805                         typeof (Boolean),  //  3 TypeCode.Boolean
2806                         typeof (Char),     //  4 TypeCode.Char
2807                         typeof (SByte),    //  5 TypeCode.SByte
2808                         typeof (Byte),     //  6 TypeCode.Byte
2809                         typeof (Int16),    //  7 TypeCode.Int16
2810                         typeof (UInt16),   //  8 TypeCode.UInt16
2811                         typeof (Int32),    //  9 TypeCode.Int32
2812                         typeof (UInt32),   // 10 TypeCode.UInt32
2813                         typeof (Int64),    // 11 TypeCode.Int64
2814                         typeof (UInt64),   // 12 TypeCode.UInt64
2815                         typeof (Single),   // 13 TypeCode.Single
2816                         typeof (Double),   // 14 TypeCode.Double
2817                         typeof (Decimal),  // 15 TypeCode.Decimal
2818                         typeof (DateTime), // 16 TypeCode.DateTime
2819                         null,               // 17 null.
2820                         typeof (String),   // 18 TypeCode.String
2821                 };
2822
2823                 // Function to convert an object to another type and return
2824                 // it as an object. In place for the core data types to use
2825                 // when implementing IConvertible. Uses hardcoded indexes in 
2826                 // the conversionTypes array, so if modify carefully.
2827         
2828                 //
2829                 // The `try_target_to_type' boolean indicates if the code
2830                 // should try to call the IConvertible.ToType method if everything
2831                 // else fails.
2832                 //
2833                 // This should be true for invocations from Convert.cs, and
2834                 // false from the mscorlib types that implement IConvertible that 
2835                 // all into this internal function.
2836                 //
2837                 // This was added to keep the fix for #481687 working and to avoid
2838                 // the regression that the simple fix introduced (485377)
2839                 internal static object ToType (object value, Type conversionType, IFormatProvider provider, bool try_target_to_type) 
2840                 {
2841                         if (value == null) {
2842                                 if ((conversionType != null) && conversionType.IsValueType){
2843                                         throw new InvalidCastException ("Null object can not be converted to a value type.");
2844                                 } else
2845                                         return null;
2846                         }
2847
2848                         if (conversionType == null)
2849                                 throw new InvalidCastException ("Cannot cast to destination type.");
2850
2851                         if (value.GetType () == conversionType)
2852                                 return value;
2853                         
2854                         if (value is IConvertible) {
2855                                 IConvertible convertValue = (IConvertible) value;
2856
2857                                 if (conversionType == conversionTable[0]) // 0 Empty
2858                                         throw new ArgumentNullException ();
2859                                 
2860                                 else if (conversionType == conversionTable[1]) // 1 TypeCode.Object
2861                                         return (object) value;
2862                                         
2863                                 else if (conversionType == conversionTable[2]) // 2 TypeCode.DBNull
2864                                         throw new InvalidCastException (
2865                                                 "Cannot cast to DBNull, it's not IConvertible");
2866                   
2867                                 else if (conversionType == conversionTable[3]) // 3 TypeCode.Boolean
2868                                         return (object) convertValue.ToBoolean (provider);
2869                                         
2870                                 else if (conversionType == conversionTable[4]) // 4 TypeCode.Char
2871                                         return (object) convertValue.ToChar (provider);
2872                   
2873                                 else if (conversionType == conversionTable[5]) // 5 TypeCode.SByte
2874                                         return (object) convertValue.ToSByte (provider);
2875
2876                                 else if (conversionType == conversionTable[6]) // 6 TypeCode.Byte
2877                                         return (object) convertValue.ToByte (provider);
2878                                 
2879                                 else if (conversionType == conversionTable[7]) // 7 TypeCode.Int16
2880                                         return (object) convertValue.ToInt16 (provider);
2881                                         
2882                                 else if (conversionType == conversionTable[8]) // 8 TypeCode.UInt16
2883                                         return (object) convertValue.ToUInt16 (provider);
2884                   
2885                                 else if (conversionType == conversionTable[9]) // 9 TypeCode.Int32
2886                                         return (object) convertValue.ToInt32 (provider);
2887                         
2888                                 else if (conversionType == conversionTable[10]) // 10 TypeCode.UInt32
2889                                         return (object) convertValue.ToUInt32 (provider);
2890                   
2891                                 else if (conversionType == conversionTable[11]) // 11 TypeCode.Int64
2892                                         return (object) convertValue.ToInt64 (provider);
2893                   
2894                                 else if (conversionType == conversionTable[12]) // 12 TypeCode.UInt64
2895                                         return (object) convertValue.ToUInt64 (provider);
2896                   
2897                                 else if (conversionType == conversionTable[13]) // 13 TypeCode.Single
2898                                         return (object) convertValue.ToSingle (provider);
2899                   
2900                                 else if (conversionType == conversionTable[14]) // 14 TypeCode.Double
2901                                         return (object) convertValue.ToDouble (provider);
2902
2903                                 else if (conversionType == conversionTable[15]) // 15 TypeCode.Decimal
2904                                         return (object) convertValue.ToDecimal (provider);
2905
2906                                 else if (conversionType == conversionTable[16]) // 16 TypeCode.DateTime
2907                                         return (object) convertValue.ToDateTime (provider);
2908                                 
2909                                 else if (conversionType == conversionTable[18]) // 18 TypeCode.String
2910                                         return (object) convertValue.ToString (provider);
2911                                 else {
2912                                         if (try_target_to_type)
2913                                                 return convertValue.ToType (conversionType, provider);
2914                                 }
2915                         } 
2916                         // Not in the conversion table
2917                         throw new InvalidCastException ((Locale.GetText (
2918                                                                  "Value is not a convertible object: " + value.GetType().ToString() + " to " + conversionType.FullName)));
2919                 }
2920         }
2921 }