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