New tests.
[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                         checked {
1211                                 return (int)(Math.Round (value));
1212                         }
1213                 }
1214  
1215                 public static int ToInt32 (float value) 
1216                 { 
1217                         if (value > Int32.MaxValue || value < Int32.MinValue) 
1218                                 throw new OverflowException (Locale.GetText (
1219                                         "Value is greater than Int32.MaxValue or less than Int32.MinValue"));
1220           
1221                         // Returned Even-Rounded, pass as a double, could just call
1222                         // Convert.ToInt32 ( (double)value);
1223                         checked {
1224                                 return (int)(Math.Round ( (double)value));
1225                         }
1226                 }
1227
1228                 public static int ToInt32 (int value) 
1229                 { 
1230                         return value; 
1231                 }
1232         
1233                 public static int ToInt32 (long value) 
1234                 { 
1235                         if (value > Int32.MaxValue || value < Int32.MinValue) 
1236                                 throw new OverflowException (Locale.GetText (
1237                                         "Value is greater than Int32.MaxValue or less than Int32.MinValue"));
1238
1239                         return (int)value; 
1240                 }
1241
1242                 [CLSCompliant (false)]
1243                 public static int ToInt32 (sbyte value) 
1244                 { 
1245                         return (int)value; 
1246                 }
1247         
1248                 public static int ToInt32 (short value) 
1249                 { 
1250                         return (int)value; 
1251                 }
1252
1253                 public static int ToInt32 (string value) 
1254                 {
1255                         if (value == null)
1256                                 return 0; // LAMESPEC: Spec says throw ArgumentNullException
1257                         return Int32.Parse (value);
1258                 }
1259
1260                 public static int ToInt32 (string value, IFormatProvider provider) 
1261                 {
1262                         if (value == null)
1263                                 return 0; // LAMESPEC: Spec says throw ArgumentNullException
1264                         return Int32.Parse (value, provider);
1265                 }
1266
1267                 
1268                 public static int ToInt32 (string value, int fromBase)
1269                 {
1270                         return ConvertFromBase (value, fromBase, false);
1271                 }
1272                 
1273                 [CLSCompliant (false)]
1274                 public static int ToInt32 (uint value) 
1275                 { 
1276                         if (value > Int32.MaxValue) 
1277                                 throw new OverflowException (Locale.GetText (
1278                                         "Value is greater than Int32.MaxValue"));
1279
1280                         return (int)value; 
1281                 }
1282
1283                 [CLSCompliant (false)]
1284                 public static int ToInt32 (ulong value) 
1285                 { 
1286                         if (value > Int32.MaxValue) 
1287                                 throw new OverflowException (Locale.GetText (
1288                                         "Value is greater than Int32.MaxValue"));
1289
1290                         return (int)value; 
1291                 }
1292
1293                 [CLSCompliant (false)]
1294                 public static int ToInt32 (ushort value) 
1295                 { 
1296                         return (int)value; 
1297                 }
1298
1299                 public static int ToInt32 (object value)
1300                 {
1301                         if (value == null)
1302                                 return 0;
1303                         return ToInt32 (value, null);
1304                 }
1305
1306                 public static int ToInt32 (object value, IFormatProvider provider)
1307                 {
1308                         if (value == null)
1309                                 return 0;
1310                         return ((IConvertible) value).ToInt32 (provider);
1311                 }
1312
1313                 // ========== Int64 Conversions ========== //
1314
1315                 public static long ToInt64 (bool value) 
1316                 { 
1317                         return value ? 1 : 0; 
1318                 }
1319         
1320                 public static long ToInt64 (byte value) 
1321                 { 
1322                         return (long)(ulong)value; 
1323                 }
1324
1325                 public static long ToInt64 (char value) 
1326                 { 
1327                         return (long)value; 
1328                 }
1329
1330                 public static long ToInt64 (DateTime value)
1331                 {
1332                         throw new InvalidCastException ("This conversion is not supported.");
1333                 }
1334         
1335                 public static long ToInt64 (decimal value) 
1336                 { 
1337                         if (value > Int64.MaxValue || value < Int64.MinValue) 
1338                                 throw new OverflowException (Locale.GetText (
1339                                         "Value is greater than Int64.MaxValue or less than Int64.MinValue"));
1340           
1341                         // Returned Even-Rounded
1342                         return (long)(Math.Round (value));        
1343                 }
1344
1345                 public static long ToInt64 (double value) 
1346                 { 
1347                         if (value > Int64.MaxValue || value < Int64.MinValue) 
1348                                 throw new OverflowException (Locale.GetText (
1349                                         "Value is greater than Int64.MaxValue or less than Int64.MinValue"));
1350           
1351                         // Returned Even-Rounded
1352                         return (long)(Math.Round (value));
1353                 }
1354  
1355                 public static long ToInt64 (float value) 
1356                 { 
1357                         if (value > Int64.MaxValue || value < Int64.MinValue) 
1358                                 throw new OverflowException (Locale.GetText (
1359                                         "Value is greater than Int64.MaxValue or less than Int64.MinValue"));
1360           
1361                         // Returned Even-Rounded, pass to Math as a double, could
1362                         // just call Convert.ToInt64 ( (double)value);
1363                         return (long)(Math.Round ( (double)value));
1364                 }
1365
1366                 public static long ToInt64 (int value) 
1367                 { 
1368                         return (long)value; 
1369                 }
1370         
1371                 public static long ToInt64 (long value) 
1372                 { 
1373                         return value; 
1374                 }
1375
1376                 [CLSCompliant (false)]
1377                 public static long ToInt64 (sbyte value) 
1378                 { 
1379                         return (long)value; 
1380                 }
1381         
1382                 public static long ToInt64 (short value) 
1383                 { 
1384                         return (long)value; 
1385                 }
1386
1387                 public static long ToInt64 (string value) 
1388                 {
1389                         if (value == null)
1390                                 return 0; // LAMESPEC: Spec says throw ArgumentNullException
1391                         return Int64.Parse (value);
1392                 }
1393
1394                 public static long ToInt64 (string value, IFormatProvider provider) 
1395                 {
1396                         if (value == null)
1397                                 return 0; // LAMESPEC: Spec says throw ArgumentNullException
1398                         return Int64.Parse (value, provider);
1399                 }
1400
1401                 public static long ToInt64 (string value, int fromBase)
1402                 {
1403                         return ConvertFromBase64 (value, fromBase, false);
1404                 }
1405
1406                 [CLSCompliant (false)]
1407                 public static long ToInt64 (uint value) 
1408                 { 
1409                         return (long)(ulong)value; 
1410                 }
1411
1412                 [CLSCompliant (false)]
1413                 public static long ToInt64 (ulong value) 
1414                 { 
1415                         if (value > Int64.MaxValue) 
1416                                 throw new OverflowException (Locale.GetText (
1417                                         "Value is greater than Int64.MaxValue"));
1418
1419                         return (long)value; 
1420                 }
1421
1422                 [CLSCompliant (false)]
1423                 public static long ToInt64 (ushort value) 
1424                 { 
1425                         return (long)(ulong)value; 
1426                 }
1427
1428                 public static long ToInt64 (object value)
1429                 {
1430                         if (value == null)
1431                                 return 0;
1432                         return ToInt64 (value, null);
1433                 }
1434
1435                 public static long ToInt64 (object value, IFormatProvider provider)
1436                 {
1437                         if (value == null)
1438                                 return 0;
1439                         return ((IConvertible) value).ToInt64 (provider);
1440                 }
1441                 
1442                 // ========== SByte Conversions ========== //
1443
1444                 [CLSCompliant (false)]
1445                 public static sbyte ToSByte (bool value) 
1446                 { 
1447                         return (sbyte)(value ? 1 : 0); 
1448                 }
1449
1450                 [CLSCompliant (false)]
1451                 public static sbyte ToSByte (byte value) 
1452                 { 
1453                         if (value > SByte.MaxValue)
1454                                 throw new OverflowException (Locale.GetText (
1455                                         "Value is greater than SByte.MaxValue"));
1456
1457                         return (sbyte)value; 
1458                 }
1459
1460                 [CLSCompliant (false)]
1461                 public static sbyte ToSByte (char value) 
1462                 { 
1463                         if (value > SByte.MaxValue)
1464                                 throw new OverflowException (Locale.GetText (
1465                                         "Value is greater than SByte.MaxValue"));
1466
1467                         return (sbyte)value;
1468                 }
1469
1470                 [CLSCompliant (false)]
1471                 public static sbyte ToSByte (DateTime value)
1472                 {
1473                         throw new InvalidCastException ("This conversion is not supported.");
1474                 }
1475                 
1476                 [CLSCompliant (false)]  
1477                 public static sbyte ToSByte (decimal value) 
1478                 { 
1479                         if (value > SByte.MaxValue || value < SByte.MinValue)
1480                                 throw new OverflowException (Locale.GetText (
1481                                         "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
1482           
1483                         // Returned Even-Rounded
1484                         return (sbyte)(Math.Round (value));
1485                 }
1486
1487                 [CLSCompliant (false)]
1488                 public static sbyte ToSByte (double value) 
1489                 { 
1490                         if (value > SByte.MaxValue || value < SByte.MinValue)
1491                                 throw new OverflowException (Locale.GetText (
1492                                         "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
1493
1494                         // Returned Even-Rounded
1495                         return (sbyte)(Math.Round (value));
1496                 }
1497
1498                 [CLSCompliant (false)]
1499                 public static sbyte ToSByte (float value) 
1500                 { 
1501                         if (value > SByte.MaxValue || value < SByte.MinValue)
1502                                 throw new OverflowException (Locale.GetText (
1503                                         "Value is greater than SByte.MaxValue or less than SByte.Minalue"));
1504
1505                         // Returned Even-Rounded, pass as double to Math
1506                         return (sbyte)(Math.Round ( (double)value));
1507                 }
1508
1509                 [CLSCompliant (false)]
1510                 public static sbyte ToSByte (int value) 
1511                 { 
1512                         if (value > SByte.MaxValue || value < SByte.MinValue)
1513                                 throw new OverflowException (Locale.GetText (
1514                                         "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
1515           
1516                         return (sbyte)value; 
1517                 }
1518
1519                 [CLSCompliant (false)]
1520                 public static sbyte ToSByte (long value) 
1521                 { 
1522                         if (value > SByte.MaxValue || value < SByte.MinValue)
1523                                 throw new OverflowException (Locale.GetText (
1524                                         "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
1525           
1526                         return (sbyte)value;
1527                 }
1528
1529                 [CLSCompliant (false)]
1530                 public static sbyte ToSByte (sbyte value) 
1531                 { 
1532                         return value;
1533                 }
1534
1535                 [CLSCompliant (false)]
1536                 public static sbyte ToSByte (short value) 
1537                 { 
1538                         if (value > SByte.MaxValue || value < SByte.MinValue)
1539                                 throw new OverflowException (Locale.GetText (
1540                                         "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
1541           
1542                         return (sbyte)value; 
1543                 }
1544
1545                 [CLSCompliant (false)]
1546                 public static sbyte ToSByte (string value) 
1547                 {
1548                         if (value == null)
1549                                 return 0; // LAMESPEC: Spec says throw ArgumentNullException
1550                         return SByte.Parse (value);
1551                 }
1552                 
1553                 [CLSCompliant (false)]
1554                 public static sbyte ToSByte (string value, IFormatProvider provider) 
1555                 {
1556                         if (value == null)
1557                                 throw new ArgumentNullException ("value");
1558                         return SByte.Parse (value, provider);
1559                 }
1560
1561                 [CLSCompliant (false)]
1562                 public static sbyte ToSByte (string value, int fromBase)
1563                 {
1564                         int result = ConvertFromBase (value, fromBase, false);
1565                         if (fromBase != 10) {
1566                                 // note: no sign are available to detect negatives
1567                                 if (result > SByte.MaxValue) {
1568                                         // return negative 2's complement
1569                                         return Convert.ToSByte (-(256 - result));
1570                                 }
1571                         }
1572                         return Convert.ToSByte (result);
1573                 }
1574                 
1575                 [CLSCompliant (false)]
1576                 public static sbyte ToSByte (uint value) 
1577                 { 
1578                         if (value > SByte.MaxValue)
1579                                 throw new OverflowException (Locale.GetText (
1580                                         "Value is greater than SByte.MaxValue"));
1581
1582                         return (sbyte)value;
1583                 }
1584
1585                 [CLSCompliant (false)]
1586                 public static sbyte ToSByte (ulong value) 
1587                 { 
1588                         if (value > (ulong)SByte.MaxValue)
1589                                 throw new OverflowException (Locale.GetText (
1590                                         "Value is greater than SByte.MaxValue"));
1591
1592                         return (sbyte)value;
1593                 }
1594
1595                 [CLSCompliant (false)]
1596                 public static sbyte ToSByte (ushort value) 
1597                 { 
1598                         if (value > SByte.MaxValue)
1599                                 throw new OverflowException (Locale.GetText (
1600                                         "Value is greater than SByte.MaxValue"));
1601
1602                         return (sbyte)value;
1603                 }
1604
1605                 [CLSCompliant (false)]
1606                 public static sbyte ToSByte (object value)
1607                 {
1608                         if (value == null)
1609                                 return 0;
1610                         return ToSByte (value, null);
1611                 }
1612
1613                 [CLSCompliant (false)]
1614                 public static sbyte ToSByte (object value, IFormatProvider provider)
1615                 {
1616                         if (value == null)
1617                                 return 0;
1618                         return ((IConvertible) value).ToSByte (provider);
1619                 }
1620
1621                 // ========== Single Conversions ========== //
1622         
1623                 public static float ToSingle (bool value) 
1624                 { 
1625                         return value ? 1 : 0; 
1626                 }
1627         
1628                 public static float ToSingle (byte value) 
1629                 { 
1630                         return (float)value; 
1631                 }
1632
1633                 public static float ToSingle (Char value)
1634                 {
1635                         throw new InvalidCastException ("This conversion is not supported.");
1636                 }
1637
1638                 public static float ToSingle (DateTime value)
1639                 {
1640                         throw new InvalidCastException ("This conversion is not supported.");
1641                 }
1642         
1643                 public static float ToSingle (decimal value) 
1644                 { 
1645                         return (float)value; 
1646                 }
1647
1648                 public static float ToSingle (double value) 
1649                 { 
1650                         return (float)value; 
1651                 }
1652         
1653                 public static float ToSingle (float value) 
1654                 { 
1655                         return value; 
1656                 }
1657
1658                 public static float ToSingle (int value) 
1659                 { 
1660                         return (float)value; 
1661                 }
1662         
1663                 public static float ToSingle (long value) 
1664                 { 
1665                         return (float)value; 
1666                 }
1667
1668                 [CLSCompliant (false)]
1669                 public static float ToSingle (sbyte value) 
1670                 { 
1671                         return (float)value; 
1672                 }
1673         
1674                 public static float ToSingle (short value) 
1675                 { 
1676                         return (float)value; 
1677                 }
1678
1679                 public static float ToSingle (string value) 
1680                 {
1681                         if (value == null)
1682                                 return 0.0f; // LAMESPEC: Spec says throw ArgumentNullException
1683                         return Single.Parse (value);
1684                 }
1685
1686                 public static float ToSingle (string value, IFormatProvider provider) 
1687                 {
1688                         if (value == null)
1689                                 return 0.0f; // LAMESPEC: Spec says throw ArgumentNullException
1690                         return Single.Parse (value, provider);
1691                 }              
1692
1693                 [CLSCompliant (false)]
1694                 public static float ToSingle (uint value) 
1695                 { 
1696                         return (float)value; 
1697                 }
1698
1699                 [CLSCompliant (false)]
1700                 public static float ToSingle (ulong value) 
1701                 { 
1702                         return (float)value; 
1703                 }
1704
1705                 [CLSCompliant (false)]
1706                 public static float ToSingle (ushort value) 
1707                 { 
1708                         return (float)value; 
1709                 }
1710
1711                 public static float ToSingle (object value)
1712                 {
1713                         if (value == null)
1714                                 return 0.0f;
1715                         return ToSingle (value, null);
1716                 }
1717
1718 //              [CLSCompliant (false)]
1719                 public static float ToSingle (object value, IFormatProvider provider)
1720                 {
1721                         if (value == null)
1722                                 return 0.0f;
1723                         return ((IConvertible) value).ToSingle (provider);
1724                 }
1725
1726                 // ========== String Conversions ========== //
1727         
1728                 public static string ToString (bool value) 
1729                 { 
1730                         return value.ToString (); 
1731                 }
1732
1733                 public static string ToString (bool value, IFormatProvider provider)
1734                 {
1735                         return value.ToString (); // the same as ToString (bool).
1736                 }
1737         
1738                 public static string ToString (byte value) 
1739                 { 
1740                         return value.ToString (); 
1741                 }
1742         
1743                 public static string ToString (byte value, IFormatProvider provider) 
1744                 {
1745                         return value.ToString (provider); 
1746                 }
1747
1748                 public static string ToString (byte value, int toBase)
1749                 {
1750                         if (value == 0)
1751                                 return "0";
1752                         if (toBase == 10)
1753                                 return value.ToString ();
1754                         
1755                         byte[] val = BitConverter.GetBytes (value);
1756
1757                         switch (toBase) {
1758                         case 2:
1759                                 return ConvertToBase2 (val);
1760                         case 8:
1761                                 return ConvertToBase8 (val);
1762                         case 16:
1763                                 return ConvertToBase16 (val);
1764                         default:
1765                                 throw new ArgumentException (Locale.GetText ("toBase is not valid."));
1766                         }
1767                 }
1768
1769                 public static string ToString (char value) 
1770                 { 
1771                         return value.ToString (); 
1772                 }
1773
1774                 public static string ToString (char value, IFormatProvider provider)
1775                 {
1776                         return value.ToString (); // the same as ToString (char)
1777                 }
1778
1779                 public static string ToString (DateTime value) 
1780                 { 
1781                         return value.ToString (); 
1782                 }
1783
1784                 public static string ToString (DateTime value, IFormatProvider provider) 
1785                 { 
1786                         return value.ToString (provider); 
1787                 }
1788
1789                 public static string ToString (decimal value) 
1790                 {
1791                         return value.ToString ();
1792                 }
1793
1794                 public static string ToString (decimal value, IFormatProvider provider) 
1795                 { 
1796                         return value.ToString (provider); 
1797                 }
1798         
1799                 public static string ToString (double value) 
1800                 { 
1801                         return value.ToString (); 
1802                 }
1803
1804                 public static string ToString (double value, IFormatProvider provider) 
1805                 { 
1806                         return value.ToString (provider);
1807                 }
1808         
1809                 public static string ToString (float value) 
1810                 { 
1811                         return value.ToString (); 
1812                 }
1813
1814                 public static string ToString (float value, IFormatProvider provider) 
1815                 { 
1816                         return value.ToString (provider); 
1817                 }
1818
1819                 public static string ToString (int value) 
1820                 { 
1821                         return value.ToString (); 
1822                 }
1823
1824                 public static string ToString (int value, int toBase)
1825                 {
1826                         if (value == 0)
1827                                 return "0";
1828                         if (toBase == 10)
1829                                 return value.ToString ();
1830                         
1831                         byte[] val = BitConverter.GetBytes (value);
1832
1833                         switch (toBase) {
1834                         case 2:
1835                                 return ConvertToBase2 (val);
1836                         case 8:
1837                                 return ConvertToBase8 (val);
1838                         case 16:
1839                                 return ConvertToBase16 (val);
1840                         default:
1841                                 throw new ArgumentException (Locale.GetText ("toBase is not valid."));
1842                         }
1843                 }
1844
1845                 public static string ToString (int value, IFormatProvider provider) 
1846                 { 
1847                         return value.ToString (provider); 
1848                 }
1849         
1850                 public static string ToString (long value) 
1851                 { 
1852                         return value.ToString (); 
1853                 }
1854
1855                 public static string ToString (long value, int toBase)
1856                 {
1857                         if (value == 0)
1858                                 return "0";
1859                         if (toBase == 10)
1860                                 return value.ToString ();
1861                         
1862                         byte[] val = BitConverter.GetBytes (value);
1863
1864                         switch (toBase) {
1865                         case 2:
1866                                 return ConvertToBase2 (val);
1867                         case 8:
1868                                 return ConvertToBase8 (val);
1869                         case 16:
1870                                 return ConvertToBase16 (val);
1871                         default:
1872                                 throw new ArgumentException (Locale.GetText ("toBase is not valid."));
1873                         }
1874                 }
1875
1876                 public static string ToString (long value, IFormatProvider provider) 
1877                 { 
1878                         return value.ToString (provider); 
1879                 }
1880
1881                 public static string ToString (object value)
1882                 {
1883                         return ToString (value, null);
1884                 }               
1885
1886                 public static string ToString (object value, IFormatProvider provider)
1887                 {
1888                         if (value is IConvertible)
1889                                 return ((IConvertible) value).ToString (provider);
1890                         else if (value != null)
1891                                 return value.ToString ();
1892                         return String.Empty;
1893                 }                               
1894
1895                 [CLSCompliant (false)]
1896                 public static string ToString (sbyte value) 
1897                 { 
1898                         return value.ToString (); 
1899                 }
1900
1901                 [CLSCompliant (false)]                          
1902                 public static string ToString (sbyte value, IFormatProvider provider) 
1903                 { 
1904                         return value.ToString (provider); 
1905                 }
1906         
1907                 public static string ToString (short value) 
1908                 { 
1909                         return value.ToString (); 
1910                 }
1911
1912                 public static string ToString (short value, int toBase)
1913                 {
1914                         if (value == 0)
1915                                 return "0";
1916                         if (toBase == 10)
1917                                 return value.ToString ();
1918                         
1919                         byte[] val = BitConverter.GetBytes (value);
1920
1921                         switch (toBase) {
1922                         case 2:
1923                                 return ConvertToBase2 (val);
1924                         case 8:
1925                                 return ConvertToBase8 (val);
1926                         case 16:
1927                                 return ConvertToBase16 (val);
1928                         default:
1929                                 throw new ArgumentException (Locale.GetText ("toBase is not valid."));
1930                         }
1931                 }
1932
1933                 public static string ToString (short value, IFormatProvider provider) 
1934                 { 
1935                         return value.ToString (provider); 
1936                 }
1937
1938                 public static string ToString (string value) 
1939                 {
1940                         return value;
1941                 }
1942
1943                 public static string ToString (string value, IFormatProvider provider)
1944                 {
1945                         return value; // provider is ignored.
1946                 }
1947
1948                 [CLSCompliant (false)]
1949                 public static string ToString (uint value) 
1950                 { 
1951                         return value.ToString (); 
1952                 }
1953
1954                 [CLSCompliant (false)]
1955                 public static string ToString (uint value, IFormatProvider provider) 
1956                 { 
1957                         return value.ToString (provider); 
1958                 }
1959
1960                 [CLSCompliant (false)]
1961                 public static string ToString (ulong value) 
1962                 { 
1963                         return value.ToString (); 
1964                 }
1965
1966                 [CLSCompliant (false)]
1967                 public static string ToString (ulong value, IFormatProvider provider) 
1968                 { 
1969                         return value.ToString (provider); 
1970                 }
1971
1972                 [CLSCompliant (false)]
1973                 public static string ToString (ushort value) 
1974                 { 
1975                         return value.ToString (); 
1976                 }
1977
1978                 [CLSCompliant (false)]
1979                 public static string ToString (ushort value, IFormatProvider provider) 
1980                 { 
1981                         return value.ToString (provider); 
1982                 }
1983                 
1984                 // ========== UInt16 Conversions ========== //
1985
1986                 [CLSCompliant (false)]
1987                 public static ushort ToUInt16 (bool value) 
1988                 { 
1989                         return (ushort)(value ? 1 : 0); 
1990                 }
1991
1992                 [CLSCompliant (false)]
1993                 public static ushort ToUInt16 (byte value) 
1994                 { 
1995                         return (ushort)value; 
1996                 }
1997
1998                 [CLSCompliant (false)]
1999                 public static ushort ToUInt16 (char value) 
2000                 { 
2001                         return (ushort)value; 
2002                 }
2003
2004                 [CLSCompliant (false)]
2005                 public static ushort ToUInt16 (DateTime value)
2006                 {
2007                         throw new InvalidCastException ("This conversion is not supported.");
2008                 }
2009
2010                 [CLSCompliant (false)]
2011                 public static ushort ToUInt16 (decimal value) 
2012                 { 
2013                         if (value > UInt16.MaxValue || value < UInt16.MinValue) 
2014                                 throw new OverflowException (Locale.GetText (
2015                                         "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
2016           
2017                         // Returned Even-Rounded
2018                         return (ushort)(Math.Round (value));      
2019                 }
2020
2021                 [CLSCompliant (false)]
2022                 public static ushort ToUInt16 (double value) 
2023                 { 
2024                         if (value > UInt16.MaxValue || value < UInt16.MinValue) 
2025                                 throw new OverflowException (Locale.GetText (
2026                                         "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
2027           
2028                         // Returned Even-Rounded
2029                         return (ushort)(Math.Round (value));
2030                 }
2031
2032                 [CLSCompliant (false)]
2033                 public static ushort ToUInt16 (float value) 
2034                 { 
2035                         if (value > UInt16.MaxValue || value < UInt16.MinValue) 
2036                                 throw new OverflowException (Locale.GetText (
2037                                         "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
2038           
2039                         // Returned Even-Rounded, pass as double to Math
2040                         return (ushort)(Math.Round ( (double)value));
2041                 }
2042
2043                 [CLSCompliant (false)]
2044                 public static ushort ToUInt16 (int value) 
2045                 { 
2046                         if (value > UInt16.MaxValue || value < UInt16.MinValue) 
2047                                 throw new OverflowException (Locale.GetText (
2048                                         "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
2049
2050                         return (ushort)value; 
2051                 }
2052
2053                 [CLSCompliant (false)]
2054                 public static ushort ToUInt16 (long value) 
2055                 { 
2056                         if (value > UInt16.MaxValue || value < UInt16.MinValue) 
2057                                 throw new OverflowException (Locale.GetText (
2058                                         "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
2059
2060                         return (ushort)value; 
2061                 }
2062
2063                 [CLSCompliant (false)]
2064                 public static ushort ToUInt16 (sbyte value) 
2065                 { 
2066                         if (value < UInt16.MinValue) 
2067                                 throw new OverflowException (Locale.GetText (
2068                                         "Value is less than UInt16.MinValue"));
2069
2070                         return (ushort)value; 
2071                 }
2072
2073                 [CLSCompliant (false)]
2074                 public static ushort ToUInt16 (short value) 
2075                 { 
2076                         if (value < UInt16.MinValue) 
2077                                 throw new OverflowException (Locale.GetText (
2078                                         "Value is less than UInt16.MinValue"));
2079
2080                         return (ushort)value; 
2081                 }
2082                 
2083                 [CLSCompliant (false)]
2084                 public static ushort ToUInt16 (string value) 
2085                 {
2086                         if (value == null)
2087                                 return 0; // LAMESPEC: Spec says throw ArgumentNullException
2088                         return UInt16.Parse (value);
2089                 }
2090
2091                 [CLSCompliant (false)]
2092                 public static ushort ToUInt16 (string value, IFormatProvider provider) 
2093                 {
2094                         if (value == null)
2095                                 return 0; // LAMESPEC: Spec says throw ArgumentNullException
2096                         return UInt16.Parse (value, provider);
2097                 }
2098
2099                 [CLSCompliant (false)]
2100                 public static ushort ToUInt16 (string value, int fromBase) 
2101                 {
2102                         return ToUInt16 (ConvertFromBase (value, fromBase, true));
2103                 } 
2104
2105                 [CLSCompliant (false)]
2106                 public static ushort ToUInt16 (uint value) 
2107                 { 
2108                         if (value > UInt16.MaxValue) 
2109                                 throw new OverflowException (Locale.GetText (
2110                                         "Value is greater than UInt16.MaxValue"));
2111
2112                         return (ushort)value; 
2113                 }
2114
2115                 [CLSCompliant (false)]
2116                 public static ushort ToUInt16 (ulong value) 
2117                 { 
2118                         if (value > (ulong)UInt16.MaxValue) 
2119                                 throw new OverflowException (Locale.GetText (
2120                                         "Value is greater than UInt16.MaxValue"));
2121
2122                         return (ushort)value; 
2123                 }
2124
2125                 [CLSCompliant (false)]
2126                 public static ushort ToUInt16 (ushort value) 
2127                 { 
2128                         return value; 
2129                 }
2130
2131                 [CLSCompliant (false)]
2132                 public static ushort ToUInt16 (object value)
2133                 {
2134                         if (value == null)
2135                                 return 0;
2136                         return ToUInt16 (value, null);
2137                 }
2138
2139                 [CLSCompliant (false)]
2140                 public static ushort ToUInt16 (object value, IFormatProvider provider)
2141                 {
2142                         if (value == null)
2143                                 return 0;
2144                         return ((IConvertible) value).ToUInt16 (provider);
2145                 }
2146
2147                 // ========== UInt32 Conversions ========== //
2148
2149                 [CLSCompliant (false)]
2150                 public static uint ToUInt32 (bool value) 
2151                 { 
2152                         return (uint)(value ? 1 : 0); 
2153                 }
2154
2155                 [CLSCompliant (false)]
2156                 public static uint ToUInt32 (byte value) 
2157                 { 
2158                         return (uint)value; 
2159                 }
2160
2161                 [CLSCompliant (false)]
2162                 public static uint ToUInt32 (char value) 
2163                 { 
2164                         return (uint)value; 
2165                 }
2166
2167                 [CLSCompliant (false)]
2168                 public static uint ToUInt32 (DateTime value)
2169                 {
2170                         throw new InvalidCastException ("This conversion is not supported.");
2171                 }
2172                 
2173                 [CLSCompliant (false)]
2174                 public static uint ToUInt32 (decimal value) 
2175                 { 
2176                         if (value > UInt32.MaxValue || value < UInt32.MinValue) 
2177                                 throw new OverflowException (Locale.GetText (
2178                                         "Value is greater than UInt32.MaxValue or less than UInt32.MinValue"));
2179           
2180                         // Returned Even-Rounded
2181                         return (uint)(Math.Round (value));        
2182                 }
2183
2184                 [CLSCompliant (false)]
2185                 public static uint ToUInt32 (double value) 
2186                 { 
2187                         if (value > UInt32.MaxValue || value < UInt32.MinValue) 
2188                                 throw new OverflowException (Locale.GetText (
2189                                         "Value is greater than UInt32.MaxValue or less than UInt32.MinValue"));
2190           
2191                         // Returned Even-Rounded
2192                         return (uint)(Math.Round (value));        
2193                 }
2194
2195                 [CLSCompliant (false)]
2196                 public static uint ToUInt32 (float value) 
2197                 { 
2198                         if (value > UInt32.MaxValue || value < UInt32.MinValue) 
2199                                 throw new OverflowException (Locale.GetText (
2200                                         "Value is greater than UInt32.MaxValue or less than UInt32.MinValue"));
2201           
2202                         // Returned Even-Rounded, pass as double to Math
2203                         return (uint)(Math.Round ( (double)value));
2204                 }
2205
2206                 [CLSCompliant (false)]
2207                 public static uint ToUInt32 (int value) 
2208                 { 
2209                         if (value < UInt32.MinValue) 
2210                                 throw new OverflowException (Locale.GetText (
2211                                         "Value is less than UInt32.MinValue"));
2212
2213                         return (uint)value; 
2214                 }
2215
2216                 [CLSCompliant (false)]
2217                 public static uint ToUInt32 (long value) 
2218                 { 
2219                         if (value > UInt32.MaxValue || value < UInt32.MinValue) 
2220                                 throw new OverflowException (Locale.GetText (
2221                                         "Value is greater than UInt32.MaxValue or less than UInt32.MinValue"));
2222
2223                         return (uint)value; 
2224                 }
2225
2226                 [CLSCompliant (false)]
2227                 public static uint ToUInt32 (sbyte value) 
2228                 { 
2229                         if (value < UInt32.MinValue) 
2230                                 throw new OverflowException (Locale.GetText (
2231                                         "Value is less than UInt32.MinValue"));
2232
2233                         return (uint)value; 
2234                 }
2235
2236                 [CLSCompliant (false)]
2237                 public static uint ToUInt32 (short value) 
2238                 { 
2239                         if (value < UInt32.MinValue) 
2240                                 throw new OverflowException (Locale.GetText (
2241                                         "Value is less than UInt32.MinValue"));
2242
2243                         return (uint)value; 
2244                 }
2245
2246                 [CLSCompliant (false)]
2247                 public static uint ToUInt32 (string value) 
2248                 {
2249                         if (value == null)
2250                                 return 0; // LAMESPEC: Spec says throw ArgumentNullException
2251                         return UInt32.Parse (value);
2252                 }
2253
2254                 [CLSCompliant (false)]
2255                 public static uint ToUInt32 (string value, IFormatProvider provider) 
2256                 {
2257                         if (value == null)
2258                                 return 0; // LAMESPEC: Spec says throw ArgumentNullException
2259                         return UInt32.Parse (value, provider);
2260                 }
2261
2262                 [CLSCompliant (false)]
2263                 public static uint ToUInt32 (string value, int fromBase)
2264                 {
2265                         return (uint) ConvertFromBase (value, fromBase, true);
2266                 }
2267
2268                 [CLSCompliant (false)]
2269                 public static uint ToUInt32 (uint value) 
2270                 { 
2271                         return value; 
2272                 }
2273
2274                 [CLSCompliant (false)]
2275                 public static uint ToUInt32 (ulong value) 
2276                 { 
2277                         if (value > UInt32.MaxValue) 
2278                                 throw new OverflowException (Locale.GetText (
2279                                         "Value is greater than UInt32.MaxValue"));
2280
2281                         return (uint)value; 
2282                 }
2283
2284                 [CLSCompliant (false)]
2285                 public static uint ToUInt32 (ushort value) 
2286                 { 
2287                         return (uint)value; 
2288                 }
2289
2290                 [CLSCompliant (false)]
2291                 public static uint ToUInt32 (object value)
2292                 {
2293                         if (value == null)
2294                                 return 0;
2295                         return ToUInt32 (value, null);
2296                 }               
2297
2298                 [CLSCompliant (false)]
2299                 public static uint ToUInt32 (object value, IFormatProvider provider)
2300                 {
2301                         if (value == null)
2302                                 return 0;
2303                         return ((IConvertible) value).ToUInt32 (provider);
2304                 }               
2305                 
2306
2307                 // ========== UInt64 Conversions ========== //
2308
2309                 [CLSCompliant (false)]
2310                 public static ulong ToUInt64 (bool value) 
2311                 { 
2312                         return (ulong)(value ? 1 : 0); 
2313                 }
2314
2315                 [CLSCompliant (false)]
2316                 public static ulong ToUInt64 (byte value) 
2317                 { 
2318                         return (ulong)value; 
2319                 }
2320
2321                 [CLSCompliant (false)]
2322                 public static ulong ToUInt64 (char value) 
2323                 { 
2324                         return (ulong)value; 
2325                 }
2326
2327                 [CLSCompliant (false)]
2328                 public static ulong ToUInt64 (DateTime value)
2329                 {
2330                         throw new InvalidCastException ("The conversion is not supported.");
2331                 }
2332
2333                 [CLSCompliant (false)]
2334                 public static ulong ToUInt64 (decimal value) 
2335                 { 
2336                         if (value > UInt64.MaxValue || value < UInt64.MinValue) 
2337                                 throw new OverflowException (Locale.GetText (
2338                                         "Value is greater than UInt64.MaxValue or less than UInt64.MinValue"));
2339           
2340                         // Returned Even-Rounded
2341                         return (ulong)(Math.Round (value));       
2342                 }
2343
2344                 [CLSCompliant (false)]
2345                 public static ulong ToUInt64 (double value) 
2346                 { 
2347                         if (value > UInt64.MaxValue || value < UInt64.MinValue) 
2348                                 throw new OverflowException (Locale.GetText (
2349                                         "Value is greater than UInt64.MaxValue or less than UInt64.MinValue"));
2350           
2351                         // Returned Even-Rounded
2352                         return (ulong)(Math.Round (value));       
2353                 }
2354                 
2355                 [CLSCompliant (false)] 
2356                 public static ulong ToUInt64 (float value) 
2357                 { 
2358                         if (value > UInt64.MaxValue || value < UInt64.MinValue) 
2359                                 throw new OverflowException (Locale.GetText (
2360                                         "Value is greater than UInt64.MaxValue or less than UInt64.MinValue"));
2361           
2362                         // Returned Even-Rounded, pass as a double to Math
2363                         return (ulong)(Math.Round ( (double)value));
2364                 }
2365                 
2366                 [CLSCompliant (false)]
2367                 public static ulong ToUInt64 (int value) 
2368                 { 
2369                         if (value < (int)UInt64.MinValue) 
2370                                 throw new OverflowException (Locale.GetText (
2371                                         "Value is less than UInt64.MinValue"));
2372
2373                         return (ulong)value; 
2374                 }
2375                 
2376                 [CLSCompliant (false)]
2377                 public static ulong ToUInt64 (long value) 
2378                 { 
2379                         if (value < (long)UInt64.MinValue) 
2380                                 throw new OverflowException (Locale.GetText (
2381                                         "Value is less than UInt64.MinValue"));
2382
2383                         return (ulong)value; 
2384                 }
2385
2386                 [CLSCompliant (false)]
2387                 public static ulong ToUInt64 (sbyte value) 
2388                 { 
2389                         if (value < (sbyte)UInt64.MinValue) 
2390                                 throw new OverflowException
2391                                 ("Value is less than UInt64.MinValue");
2392
2393                         return (ulong)value; 
2394                 }
2395                 
2396                 [CLSCompliant (false)]  
2397                 public static ulong ToUInt64 (short value) 
2398                 { 
2399                         if (value < (short)UInt64.MinValue) 
2400                                 throw new OverflowException (Locale.GetText (
2401                                         "Value is less than UInt64.MinValue"));
2402
2403                         return (ulong)value; 
2404                 }
2405
2406                 [CLSCompliant (false)]
2407                 public static ulong ToUInt64 (string value) 
2408                 {
2409                         if (value == null)
2410                                 return 0; // LAMESPEC: Spec says throw ArgumentNullException
2411                         return UInt64.Parse (value);
2412                 }
2413
2414                 [CLSCompliant (false)]
2415                 public static ulong ToUInt64 (string value, IFormatProvider provider) 
2416                 {
2417                         if (value == null)
2418                                 return 0; // LAMESPEC: Spec says throw ArgumentNullException
2419                         return UInt64.Parse (value, provider);
2420                 }
2421
2422                 [CLSCompliant (false)]
2423                 public static ulong ToUInt64 (string value, int fromBase)
2424                 {
2425                         return (ulong) ConvertFromBase64 (value, fromBase, true);
2426                 }
2427
2428                 [CLSCompliant (false)]
2429                 public static ulong ToUInt64 (uint value) 
2430                 { 
2431                         return (ulong)value; 
2432                 }
2433
2434                 [CLSCompliant (false)]
2435                 public static ulong ToUInt64 (ulong value) 
2436                 { 
2437                         return value; 
2438                 }
2439                 
2440                 [CLSCompliant (false)]
2441                 public static ulong ToUInt64 (ushort value) 
2442                 { 
2443                         return (ulong)value; 
2444                 }
2445
2446                 [CLSCompliant (false)]
2447                 public static ulong ToUInt64 (object value)
2448                 {
2449                         if (value == null)
2450                                 return 0;
2451                         return ToUInt64 (value, null);
2452                 }               
2453
2454                 [CLSCompliant (false)]
2455                 public static ulong ToUInt64 (object value, IFormatProvider provider)
2456                 {
2457                         if (value == null)
2458                                 return 0;
2459                         return ((IConvertible) value).ToUInt64 (provider);
2460                 }               
2461                 
2462
2463                 // ========== Conversion / Helper Functions ========== //
2464
2465                 public static object ChangeType (object value, Type conversionType)
2466                 {
2467                         if ((value != null) && (conversionType == null))
2468                                 throw new ArgumentNullException ("conversionType");
2469                         CultureInfo ci = CultureInfo.CurrentCulture;
2470                         IFormatProvider provider;
2471                         if (conversionType == typeof(DateTime)) {
2472                                 provider = ci.DateTimeFormat;
2473                         }
2474                         else {
2475                                 provider = ci.NumberFormat;
2476                         }
2477                         return ToType (value, conversionType, provider, true);
2478                 }
2479                 
2480                 public static object ChangeType (object value, TypeCode typeCode)
2481                 {
2482                         CultureInfo ci = CultureInfo.CurrentCulture;
2483                         Type conversionType = conversionTable [(int) typeCode];
2484                         IFormatProvider provider;
2485                         if (conversionType == typeof(DateTime)) {
2486                                 provider = ci.DateTimeFormat;
2487                         }
2488                         else {
2489                                 provider = ci.NumberFormat;
2490                         }
2491                         return ToType (value, conversionType, provider, true);
2492                 }
2493
2494                 public static object ChangeType (object value, Type conversionType, IFormatProvider provider)
2495                 {
2496                         if ((value != null) && (conversionType == null))
2497                                 throw new ArgumentNullException ("conversionType");
2498                         return ToType (value, conversionType, provider, true);
2499                 }
2500                 
2501                 public static object ChangeType (object value, TypeCode typeCode, IFormatProvider provider)
2502                 {
2503                         Type conversionType = conversionTable [(int)typeCode];
2504                         return ToType (value, conversionType, provider, true);
2505                 }
2506
2507                 private static bool NotValidBase (int value)
2508                 {
2509                         if ((value == 2) || (value == 8) ||
2510                            (value == 10) || (value == 16))
2511                                 return false;
2512                         
2513                         return true;
2514                 }
2515
2516                 private static int ConvertFromBase (string value, int fromBase, bool unsigned)
2517                 {
2518                         if (NotValidBase (fromBase))
2519                                 throw new ArgumentException ("fromBase is not valid.");
2520                         if (value == null)
2521                                 return 0;
2522
2523                         int chars = 0;
2524                         int result = 0;
2525                         int digitValue;
2526
2527                         int i=0; 
2528                         int len = value.Length;
2529                         bool negative = false;
2530
2531                         // special processing for some bases
2532                         switch (fromBase) {
2533                                 case 10:
2534                                         if (value.Substring (i, 1) == "-") {
2535                                                 if (unsigned) {
2536                                                         throw new OverflowException (
2537                                                                 Locale.GetText ("The string was being parsed as"
2538                                                                 + " an unsigned number and could not have a"
2539                                                                 + " negative sign."));
2540                                                 }
2541                                                 negative = true;
2542                                                 i++;
2543                                         }
2544                                         break;
2545                                 case 16:
2546                                         if (value.Substring (i, 1) == "-") {
2547                                                 throw new ArgumentException ("String cannot contain a "
2548                                                         + "minus sign if the base is not 10.");
2549                                         }
2550                                         if (len >= i + 2) {
2551                                                 // 0x00 or 0X00
2552                                                 if ((value[i] == '0') && ((value [i+1] == 'x') || (value [i+1] == 'X'))) {
2553                                                         i+=2;
2554                                                 }
2555                                         }
2556                                         break;
2557                                 default:
2558                                         if (value.Substring (i, 1) == "-") {
2559                                                 throw new ArgumentException ("String cannot contain a "
2560                                                         + "minus sign if the base is not 10.");
2561                                         }
2562                                         break;
2563                         }
2564
2565                         if (len == i) {
2566                                 throw new FormatException ("Could not find any parsable digits.");
2567                         }
2568
2569                         if (value[i] == '+') {
2570                                 i++;
2571                         }
2572
2573                         while (i < len) {
2574                                 char c = value[i++];
2575                                 if (Char.IsNumber (c)) {
2576                                         digitValue = c - '0';
2577                                 } else if (Char.IsLetter (c)) {
2578                                         digitValue = Char.ToLowerInvariant (c) - 'a' + 10;
2579                                 } else {
2580                                         if (chars > 0) {
2581                                                 throw new FormatException ("Additional unparsable "
2582                                                         + "characters are at the end of the string.");
2583                                         } else {
2584                                                 throw new FormatException ("Could not find any parsable"
2585                                                         + " digits.");
2586                                         }
2587                                 }
2588
2589                                 if (digitValue >= fromBase) {
2590                                         if (chars > 0) {
2591                                                 throw new FormatException ("Additional unparsable "
2592                                                         + "characters are at the end of the string.");
2593                                         } else {
2594                                                 throw new FormatException ("Could not find any parsable"
2595                                                         + " digits.");
2596                                         }
2597                                 }
2598
2599                                 result = (fromBase) * result + digitValue;
2600                                 chars ++;
2601                         }
2602
2603                         if (chars == 0)
2604                                 throw new FormatException ("Could not find any parsable digits.");
2605
2606                         if (negative)
2607                                 return -result;
2608                         else
2609                                 return result;
2610                 }
2611
2612                 // note: this has nothing to do with base64 encoding (just base and Int64)
2613                 private static long ConvertFromBase64 (string value, int fromBase, bool unsigned)
2614                 {
2615                         if (NotValidBase (fromBase))
2616                                 throw new ArgumentException ("fromBase is not valid.");
2617                         if (value == null)
2618                                 return 0;
2619
2620                         int chars = 0;
2621                         int digitValue = -1;
2622                         long result = 0;
2623                         bool negative = false;
2624
2625                         int i = 0;
2626                         int len = value.Length;
2627
2628                         // special processing for some bases
2629                         switch (fromBase) {
2630                                 case 10:
2631                                         if (value.Substring(i, 1) == "-") {
2632                                                 if (unsigned) {
2633                                                         throw new OverflowException (
2634                                                                 Locale.GetText ("The string was being parsed as"
2635                                                                 + " an unsigned number and could not have a"
2636                                                                 + " negative sign."));
2637                                                 }
2638                                                 negative = true;
2639                                                 i++;
2640                                         }
2641                                         break;
2642                                 case 16:
2643                                         if (value.Substring (i, 1) == "-") {
2644                                                 throw new ArgumentException ("String cannot contain a "
2645                                                         + "minus sign if the base is not 10.");
2646                                         }
2647                                         if (len >= i + 2) {
2648                                                 // 0x00 or 0X00
2649                                                 if ((value[i] == '0') && ((value[i + 1] == 'x') || (value[i + 1] == 'X'))) {
2650                                                         i += 2;
2651                                                 }
2652                                         }
2653                                         break;
2654                                 default:
2655                                         if (value.Substring (i, 1) == "-") {
2656                                                 throw new ArgumentException ("String cannot contain a "
2657                                                         + "minus sign if the base is not 10.");
2658                                         }
2659                                         break;
2660                         }
2661
2662                         if (len == i) {
2663                                 throw new FormatException ("Could not find any parsable digits.");
2664                         }
2665
2666                         if (value[i] == '+') {
2667                                 i++;
2668                         }
2669
2670                         while (i < len) {
2671                                 char c = value[i++];
2672                                 if (Char.IsNumber (c)) {
2673                                         digitValue = c - '0';
2674                                 } else if (Char.IsLetter (c)) {
2675                                         digitValue = Char.ToLowerInvariant (c) - 'a' + 10;
2676                                 } else {
2677                                         if (chars > 0) {
2678                                                 throw new FormatException ("Additional unparsable "
2679                                                         + "characters are at the end of the string.");
2680                                         } else {
2681                                                 throw new FormatException ("Could not find any parsable"
2682                                                         + " digits.");
2683                                         }
2684                                 }
2685
2686                                 if (digitValue >= fromBase) {
2687                                         if (chars > 0) {
2688                                                 throw new FormatException ("Additional unparsable "
2689                                                         + "characters are at the end of the string.");
2690                                         } else {
2691                                                 throw new FormatException ("Could not find any parsable"
2692                                                         + " digits.");
2693                                         }
2694                                 }
2695
2696                                 result = (fromBase * result + digitValue);
2697                                 chars ++;
2698                         }
2699
2700                         if (chars == 0)
2701                                 throw new FormatException ("Could not find any parsable digits.");
2702
2703                         if (negative)
2704                                 return -1 * result;
2705                         else
2706                                 return result;
2707                 }
2708
2709                 private static void EndianSwap (ref byte[] value)
2710                 {
2711                         byte[] buf = new byte[value.Length];
2712                         for (int i = 0; i < value.Length; i++)
2713                                 buf[i] = value[value.Length-1-i];
2714                         value = buf;
2715                 }
2716
2717                 private static string ConvertToBase2 (byte[] value)
2718                 {
2719                         if (!BitConverter.IsLittleEndian)
2720                                 EndianSwap (ref value);
2721                         StringBuilder sb = new StringBuilder ();
2722                         for (int i = value.Length - 1; i >= 0; i--) {
2723                                 byte b = value [i];
2724                                 for (int j = 0; j < 8; j++) {
2725                                         if ((b & 0x80) == 0x80) {
2726                                                 sb.Append ('1');
2727                                         }
2728                                         else {
2729                                                 if (sb.Length > 0)
2730                                                         sb.Append ('0');
2731                                         }
2732                                         b <<= 1;
2733                                 }
2734                         }
2735                         return sb.ToString ();
2736                 }
2737
2738                 private static string ConvertToBase8 (byte[] value)
2739                 {
2740                         ulong l = 0;
2741                         switch (value.Length) {
2742                         case 1:
2743                                 l = (ulong) value [0];
2744                                 break;
2745                         case 2:
2746                                 l = (ulong) BitConverter.ToUInt16 (value, 0);
2747                                 break;
2748                         case 4:
2749                                 l = (ulong) BitConverter.ToUInt32 (value, 0);
2750                                 break;
2751                         case 8:
2752                                 l = BitConverter.ToUInt64 (value, 0);
2753                                 break;
2754                         default:
2755                                 throw new ArgumentException ("value");
2756                         }
2757
2758                         StringBuilder sb = new StringBuilder ();
2759                         for (int i = 21; i >= 0; i--) {
2760                                 // 3 bits at the time
2761                                 char val = (char) ((l >> i * 3) & 0x7);
2762                                 if ((val != 0) || (sb.Length > 0)) {
2763                                         val += '0';
2764                                         sb.Append (val);
2765                                 }
2766                         }
2767                         return sb.ToString ();
2768                 }
2769
2770                 private static string ConvertToBase16 (byte[] value)
2771                 {
2772                         if (!BitConverter.IsLittleEndian)
2773                                 EndianSwap (ref value);
2774                         StringBuilder sb = new StringBuilder ();
2775                         for (int i = value.Length - 1; i >= 0; i--) {
2776                                 char high = (char)((value[i] >> 4) & 0x0f);
2777                                 if ((high != 0) || (sb.Length > 0)) {
2778                                         if (high < 10) 
2779                                                 high += '0';
2780                                         else {
2781                                                 high -= (char) 10;
2782                                                 high += 'a';
2783                                         }
2784                                         sb.Append (high);
2785                                 }
2786
2787                                 char low = (char)(value[i] & 0x0f);
2788                                 if ((low != 0) || (sb.Length > 0)) {
2789                                         if (low < 10)
2790                                                 low += '0';
2791                                         else {
2792                                                 low -= (char) 10;
2793                                                 low += 'a';
2794                                         }
2795                                         sb.Append (low);
2796                                 }
2797                         }
2798                         return sb.ToString ();
2799                 }
2800
2801                 // Lookup table for the conversion ToType method. Order
2802                 // is important! Used by ToType for comparing the target
2803                 // type, and uses hardcoded array indexes.
2804                 private static readonly Type[] conversionTable = {
2805                         // Valid ICovnertible Types
2806                         null,               //  0 empty
2807                         typeof (object),   //  1 TypeCode.Object
2808                         typeof (DBNull),   //  2 TypeCode.DBNull
2809                         typeof (Boolean),  //  3 TypeCode.Boolean
2810                         typeof (Char),     //  4 TypeCode.Char
2811                         typeof (SByte),    //  5 TypeCode.SByte
2812                         typeof (Byte),     //  6 TypeCode.Byte
2813                         typeof (Int16),    //  7 TypeCode.Int16
2814                         typeof (UInt16),   //  8 TypeCode.UInt16
2815                         typeof (Int32),    //  9 TypeCode.Int32
2816                         typeof (UInt32),   // 10 TypeCode.UInt32
2817                         typeof (Int64),    // 11 TypeCode.Int64
2818                         typeof (UInt64),   // 12 TypeCode.UInt64
2819                         typeof (Single),   // 13 TypeCode.Single
2820                         typeof (Double),   // 14 TypeCode.Double
2821                         typeof (Decimal),  // 15 TypeCode.Decimal
2822                         typeof (DateTime), // 16 TypeCode.DateTime
2823                         null,               // 17 null.
2824                         typeof (String),   // 18 TypeCode.String
2825                 };
2826
2827                 // Function to convert an object to another type and return
2828                 // it as an object. In place for the core data types to use
2829                 // when implementing IConvertible. Uses hardcoded indexes in 
2830                 // the conversionTypes array, so if modify carefully.
2831         
2832                 //
2833                 // The `try_target_to_type' boolean indicates if the code
2834                 // should try to call the IConvertible.ToType method if everything
2835                 // else fails.
2836                 //
2837                 // This should be true for invocations from Convert.cs, and
2838                 // false from the mscorlib types that implement IConvertible that 
2839                 // all into this internal function.
2840                 //
2841                 // This was added to keep the fix for #481687 working and to avoid
2842                 // the regression that the simple fix introduced (485377)
2843                 internal static object ToType (object value, Type conversionType, IFormatProvider provider, bool try_target_to_type) 
2844                 {
2845                         if (value == null) {
2846                                 if ((conversionType != null) && conversionType.IsValueType){
2847                                         throw new InvalidCastException ("Null object can not be converted to a value type.");
2848                                 } else
2849                                         return null;
2850                         }
2851
2852                         if (conversionType == null)
2853                                 throw new InvalidCastException ("Cannot cast to destination type.");
2854
2855                         if (value.GetType () == conversionType)
2856                                 return value;
2857                         
2858                         if (value is IConvertible) {
2859                                 IConvertible convertValue = (IConvertible) value;
2860
2861                                 if (conversionType == conversionTable[0]) // 0 Empty
2862                                         throw new ArgumentNullException ();
2863                                 
2864                                 else if (conversionType == conversionTable[1]) // 1 TypeCode.Object
2865                                         return (object) value;
2866                                         
2867                                 else if (conversionType == conversionTable[2]) // 2 TypeCode.DBNull
2868                                         throw new InvalidCastException (
2869                                                 "Cannot cast to DBNull, it's not IConvertible");
2870                   
2871                                 else if (conversionType == conversionTable[3]) // 3 TypeCode.Boolean
2872                                         return (object) convertValue.ToBoolean (provider);
2873                                         
2874                                 else if (conversionType == conversionTable[4]) // 4 TypeCode.Char
2875                                         return (object) convertValue.ToChar (provider);
2876                   
2877                                 else if (conversionType == conversionTable[5]) // 5 TypeCode.SByte
2878                                         return (object) convertValue.ToSByte (provider);
2879
2880                                 else if (conversionType == conversionTable[6]) // 6 TypeCode.Byte
2881                                         return (object) convertValue.ToByte (provider);
2882                                 
2883                                 else if (conversionType == conversionTable[7]) // 7 TypeCode.Int16
2884                                         return (object) convertValue.ToInt16 (provider);
2885                                         
2886                                 else if (conversionType == conversionTable[8]) // 8 TypeCode.UInt16
2887                                         return (object) convertValue.ToUInt16 (provider);
2888                   
2889                                 else if (conversionType == conversionTable[9]) // 9 TypeCode.Int32
2890                                         return (object) convertValue.ToInt32 (provider);
2891                         
2892                                 else if (conversionType == conversionTable[10]) // 10 TypeCode.UInt32
2893                                         return (object) convertValue.ToUInt32 (provider);
2894                   
2895                                 else if (conversionType == conversionTable[11]) // 11 TypeCode.Int64
2896                                         return (object) convertValue.ToInt64 (provider);
2897                   
2898                                 else if (conversionType == conversionTable[12]) // 12 TypeCode.UInt64
2899                                         return (object) convertValue.ToUInt64 (provider);
2900                   
2901                                 else if (conversionType == conversionTable[13]) // 13 TypeCode.Single
2902                                         return (object) convertValue.ToSingle (provider);
2903                   
2904                                 else if (conversionType == conversionTable[14]) // 14 TypeCode.Double
2905                                         return (object) convertValue.ToDouble (provider);
2906
2907                                 else if (conversionType == conversionTable[15]) // 15 TypeCode.Decimal
2908                                         return (object) convertValue.ToDecimal (provider);
2909
2910                                 else if (conversionType == conversionTable[16]) // 16 TypeCode.DateTime
2911                                         return (object) convertValue.ToDateTime (provider);
2912                                 
2913                                 else if (conversionType == conversionTable[18]) // 18 TypeCode.String
2914                                         return (object) convertValue.ToString (provider);
2915                                 else {
2916                                         if (try_target_to_type)
2917                                                 return convertValue.ToType (conversionType, provider);
2918                                 }
2919                         } 
2920                         // Not in the conversion table
2921                         throw new InvalidCastException ((Locale.GetText (
2922                                                                  "Value is not a convertible object: " + value.GetType().ToString() + " to " + conversionType.FullName)));
2923                 }
2924         }
2925 }