4329219e56d2d625558c6ee345fd870a6f348cf2
[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                         return (char)value; 
685                 }
686
687                 public static char ToChar (object value)
688                 {
689                         if (value == null)
690                                 return '\0';
691                         return ToChar (value, null);
692                 }
693
694                 public static char ToChar (object value, IFormatProvider provider)
695                 {
696                         if (value == null)
697                                 return '\0';
698                         return ((IConvertible) value).ToChar (provider);
699                 }
700
701                 // ========== DateTime Conversions ========== //
702         
703                 public static DateTime ToDateTime (string value) 
704                 { 
705                         if (value == null)
706                                 return DateTime.MinValue; // LAMESPEC: Spec says throw ArgumentNullException
707                         return DateTime.Parse (value);
708                 }
709         
710                 public static DateTime ToDateTime (string value, IFormatProvider provider) 
711                 {
712                         if (value == null)
713                                 return DateTime.MinValue; // LAMESPEC: Spec says throw ArgumentNullException
714                         return DateTime.Parse (value, provider);
715                 }
716
717                 public static DateTime ToDateTime (bool value)
718                 {
719                         throw new InvalidCastException ("This conversion is not supported.");
720                 }
721
722                 public static DateTime ToDateTime (byte value)
723                 {
724                         throw new InvalidCastException ("This conversion is not supported.");
725                 }
726
727                 public static DateTime ToDateTime (char value)
728                 {
729                         throw new InvalidCastException ("This conversion is not supported.");
730                 }
731
732                 public static DateTime ToDateTime (DateTime value)
733                 {
734                         return value;
735                 }
736
737                 public static DateTime ToDateTime (decimal value)
738                 {
739                         throw new InvalidCastException ("This conversion is not supported.");
740                 }
741
742                 public static DateTime ToDateTime (double value)
743                 {
744                         throw new InvalidCastException ("This conversion is not supported.");
745                 }
746
747                 public static DateTime ToDateTime (short value)
748                 {
749                         throw new InvalidCastException ("This conversion is not supported.");
750                 }
751
752                 public static DateTime ToDateTime (int value)
753                 {
754                         throw new InvalidCastException ("This conversion is not supported.");
755                 }
756
757                 public static DateTime ToDateTime (long value)
758                 {
759                         throw new InvalidCastException ("This conversion is not supported.");
760                 }
761
762                 public static DateTime ToDateTime (float value)
763                 {
764                         throw new InvalidCastException ("This conversion is not supported.");
765                 }
766
767                 public static DateTime ToDateTime (object value)
768                 {
769                         if (value == null)
770                                 return DateTime.MinValue;
771                         return ToDateTime (value, null);
772                 }
773
774                 public static DateTime ToDateTime (object value, IFormatProvider provider)
775                 {
776                         if (value == null)
777                                 return DateTime.MinValue;
778                         return ((IConvertible) value).ToDateTime (provider);
779                 }
780
781                 [CLSCompliant (false)]
782                 public static DateTime ToDateTime (sbyte value)
783                 {
784                         throw new InvalidCastException ("This conversion is not supported.");
785                 }
786                 [CLSCompliant (false)]
787                 public static DateTime ToDateTime (ushort value)
788                 {
789                         throw new InvalidCastException ("This conversion is not supported.");
790                 }
791
792                 [CLSCompliant (false)]
793                 public static DateTime ToDateTime (uint value)
794                 {
795                         throw new InvalidCastException ("This conversion is not supported.");
796                 }
797
798                 [CLSCompliant (false)]
799                 public static DateTime ToDateTime (ulong value)
800                 {
801                         throw new InvalidCastException ("This conversion is not supported.");
802                 }
803
804                 // ========== Decimal Conversions ========== //
805         
806                 public static decimal ToDecimal (bool value) 
807                 { 
808                         return value ? 1 : 0; 
809                 }
810         
811                 public static decimal ToDecimal (byte value) 
812                 { 
813                         return (decimal)value; 
814                 }
815
816                 public static decimal ToDecimal (char value)
817                 {
818                         throw new InvalidCastException ("This conversion is not supported.");
819                 }
820
821                 public static decimal ToDecimal (DateTime value)
822                 {
823                         throw new InvalidCastException ("This conversion is not supported.");
824                 }
825                                 
826                 public static decimal ToDecimal (decimal value) 
827                 { 
828                         return value; 
829                 }
830
831                 public static decimal ToDecimal (double value) 
832                 { 
833                         return (decimal) value; 
834                 }
835
836                 public static decimal ToDecimal (float value) 
837                 {
838                         return (decimal) value;
839                 }
840
841                 public static decimal ToDecimal (int value) 
842                 { 
843                         return (decimal)value; 
844                 }
845         
846                 public static decimal ToDecimal (long value) 
847                 { 
848                         return (decimal)value; 
849                 }
850
851                 [CLSCompliant (false)]
852                 public static decimal ToDecimal (sbyte value) 
853                 { 
854                         return (decimal)value; 
855                 }
856         
857                 public static decimal ToDecimal (short value) 
858                 { 
859                         return (decimal)value; 
860                 }
861
862                 public static decimal ToDecimal (string value) 
863                 {
864                         if (value == null)
865                                 return new Decimal (0); // LAMESPEC: Spec says throw ArgumentNullException
866                         return Decimal.Parse (value);
867                 }
868
869                 public static decimal ToDecimal (string value, IFormatProvider provider) 
870                 {
871                         if (value == null)
872                                 return new Decimal (0); // LAMESPEC: Spec says throw ArgumentNullException
873                         return Decimal.Parse (value, provider);
874                 }
875
876                 [CLSCompliant (false)]
877                 public static decimal ToDecimal (uint value) 
878                 { 
879                         return (decimal)value; 
880                 }
881
882                 [CLSCompliant (false)]
883                 public static decimal ToDecimal (ulong value) 
884                 { 
885                         return (decimal)value; 
886                 }
887
888                 [CLSCompliant (false)]
889                 public static decimal ToDecimal (ushort value) 
890                 { 
891                         return (decimal)value; 
892                 }
893
894                 public static decimal ToDecimal (object value)
895                 {
896                         if (value == null)
897                                 return new Decimal (0);
898                         return ToDecimal (value, null);
899                 }
900
901                 public static decimal ToDecimal (object value, IFormatProvider provider)
902                 {
903                         if (value == null)
904                                 return new Decimal (0);
905                         return ((IConvertible) value).ToDecimal (provider);
906                 }
907                                                  
908
909                 // ========== Double Conversions ========== //
910         
911                 public static double ToDouble (bool value) 
912                 { 
913                         return value ? 1 : 0; 
914                 }
915         
916                 public static double ToDouble (byte value) 
917                 { 
918                         return (double) value;
919                 }
920
921                 public static double ToDouble (char value)
922                 {
923                         throw new InvalidCastException ("This conversion is not supported.");
924                 }
925
926                 public static double ToDouble (DateTime value)
927                 {
928                         throw new InvalidCastException ("This conversion is not supported.");
929                 }
930         
931                 public static double ToDouble (decimal value) 
932                 { 
933                         return (double)value; 
934                 }
935
936                 public static double ToDouble (double value) 
937                 { 
938                         return value; 
939                 }
940
941                 public static double ToDouble (float value) 
942                 { 
943                         return (double) value;
944                 }
945
946                 public static double ToDouble (int value) 
947                 { 
948                         return (double)value; 
949                 }
950         
951                 public static double ToDouble (long value) 
952                 { 
953                         return (double)value; 
954                 }
955
956                 [CLSCompliant (false)]
957                 public static double ToDouble (sbyte value) 
958                 { 
959                         return (double)value; 
960                 }
961         
962                 public static double ToDouble (short value) 
963                 { 
964                         return (double)value; 
965                 }
966
967                 public static double ToDouble (string value) 
968                 {
969                         if (value == null)
970                                 return 0.0; // LAMESPEC: Spec says throw ArgumentNullException
971                         return Double.Parse (value);
972                 }
973
974                 public static double ToDouble (string value, IFormatProvider provider) 
975                 {
976                         if (value == null)
977                                 return 0.0; // LAMESPEC: Spec says throw ArgumentNullException
978                         return Double.Parse (value, provider);
979                 }
980
981                 [CLSCompliant (false)]
982                 public static double ToDouble (uint value) 
983                 { 
984                         return (double)value; 
985                 }
986
987                 [CLSCompliant (false)]
988                 public static double ToDouble (ulong value) 
989                 { 
990                         return (double)value; 
991                 }
992
993                 [CLSCompliant (false)]
994                 public static double ToDouble (ushort value) 
995                 { 
996                         return (double)value; 
997                 }
998
999                 public static double ToDouble (object value)
1000                 {
1001                         if (value == null)
1002                                 return 0.0;
1003                         return ToDouble (value, null);
1004                 }
1005
1006                 public static double ToDouble (object value, IFormatProvider provider)
1007                 {
1008                         if (value == null)
1009                                 return 0.0;
1010                         return ((IConvertible) value).ToDouble (provider);
1011                 }
1012
1013                 // ========== Int16 Conversions ========== //
1014
1015                 public static short ToInt16 (bool value) 
1016                 { 
1017                         return (short)(value ? 1 : 0); 
1018                 }
1019         
1020                 public static short ToInt16 (byte value) 
1021                 { 
1022                         return (short)value; 
1023                 }
1024
1025                 public static short ToInt16 (char value) 
1026                 {
1027                         if (value > Int16.MaxValue) 
1028                                 throw new OverflowException (Locale.GetText (
1029                                         "Value is greater than Int16.MaxValue"));
1030
1031                         return (short)value;
1032                 }
1033
1034                 public static short ToInt16 (DateTime value) 
1035                 {
1036                         throw new InvalidCastException ("This conversion is not supported.");
1037                 }
1038         
1039                 public static short ToInt16 (decimal value) 
1040                 { 
1041                         if (value > Int16.MaxValue || value < Int16.MinValue) 
1042                                 throw new OverflowException (Locale.GetText (
1043                                         "Value is greater than Int16.MaxValue or less than Int16.MinValue"));
1044           
1045                         // Returned Even-Rounded
1046                         return (short)(Math.Round (value));       
1047                 }
1048
1049                 public static short ToInt16 (double value) 
1050                 { 
1051                         if (value > Int16.MaxValue || value < Int16.MinValue) 
1052                                 throw new OverflowException (Locale.GetText (
1053                                         "Value is greater than Int16.MaxValue or less than Int16.MinValue"));
1054           
1055                         // Returned Even-Rounded
1056                         return (short)(Math.Round (value));       
1057                 }
1058  
1059                 public static short ToInt16 (float value) 
1060                 { 
1061                         if (value > Int16.MaxValue || value < Int16.MinValue) 
1062                                 throw new OverflowException (Locale.GetText (
1063                                         "Value is greater than Int16.MaxValue or less than Int16.MinValue"));
1064           
1065                         // Returned Even-Rounded, use Math.Round pass as a double.
1066                         return (short)Math.Round ( (double)value);
1067                 }
1068
1069                 public static short ToInt16 (int value) 
1070                 { 
1071                         if (value > Int16.MaxValue || value < Int16.MinValue) 
1072                                 throw new OverflowException (Locale.GetText (
1073                                         "Value is greater than Int16.MaxValue or less than Int16.MinValue"));
1074
1075                         return (short)value; 
1076                 }
1077         
1078                 public static short ToInt16 (long value) 
1079                 { 
1080                         if (value > Int16.MaxValue || value < Int16.MinValue) 
1081                                 throw new OverflowException (Locale.GetText (
1082                                         "Value is greater than Int16.MaxValue or less than Int16.MinValue"));
1083
1084                         return (short)value; 
1085                 }
1086
1087                 [CLSCompliant (false)]
1088                 public static short ToInt16 (sbyte value) 
1089                 { 
1090                         return (short)value; 
1091                 }
1092         
1093                 public static short ToInt16 (short value) 
1094                 { 
1095                         return value; 
1096                 }
1097
1098                 public static short ToInt16 (string value) 
1099                 {
1100                         if (value == null)
1101                                 return 0; // LAMESPEC: Spec says throw ArgumentNullException
1102                         return Int16.Parse (value);
1103                 }
1104
1105                 public static short ToInt16 (string value, IFormatProvider provider) 
1106                 {
1107                         if (value == null)
1108                                 return 0; // LAMESPEC: Spec says throw ArgumentNullException
1109                         return Int16.Parse (value, provider);
1110                 }
1111
1112                 public static short ToInt16 (string value, int fromBase)
1113                 {
1114                         int result = ConvertFromBase (value, fromBase, false);
1115                         if (fromBase != 10) {
1116                                 if (result > ushort.MaxValue) {
1117                                         throw new OverflowException ("Value was either too large or too small for an Int16.");
1118                                 }
1119
1120                                 // note: no sign are available to detect negatives
1121                                 if (result > Int16.MaxValue) {
1122                                         // return negative 2's complement
1123                                         return Convert.ToInt16 (-(65536 - result));
1124                                 }
1125                         }
1126                         return Convert.ToInt16 (result);
1127                 }
1128
1129                 [CLSCompliant (false)]
1130                 public static short ToInt16 (uint value) 
1131                 { 
1132                         if (value > Int16.MaxValue) 
1133                                 throw new OverflowException (Locale.GetText (
1134                                         "Value is greater than Int16.MaxValue"));
1135
1136                         return (short)value; 
1137                 }
1138
1139                 [CLSCompliant (false)]
1140                 public static short ToInt16 (ulong value) 
1141                 { 
1142                         if (value > (ulong)Int16.MaxValue) 
1143                                 throw new OverflowException (Locale.GetText (
1144                                         "Value is greater than Int16.MaxValue"));
1145                         return (short)value; 
1146                 }
1147
1148                 [CLSCompliant (false)]
1149                 public static short ToInt16 (ushort value) 
1150                 { 
1151                         if (value > Int16.MaxValue) 
1152                                 throw new OverflowException (Locale.GetText (
1153                                         "Value is greater than Int16.MaxValue"));
1154
1155                         return (short)value; 
1156                 }
1157
1158                 public static short ToInt16 (object value)
1159                 {
1160                         if (value == null)
1161                                 return 0;
1162                         return ToInt16 (value, null);
1163                 }
1164
1165                 public static short ToInt16 (object value, IFormatProvider provider)
1166                 {
1167                         if (value == null)
1168                                 return 0;
1169                         return ((IConvertible) value).ToInt16 (provider);
1170                 }
1171         
1172                 // ========== Int32 Conversions ========== //
1173
1174                 public static int ToInt32 (bool value) 
1175                 { 
1176                         return value ? 1 : 0; 
1177                 }
1178         
1179                 public static int ToInt32 (byte value) 
1180                 { 
1181                         return (int)value; 
1182                 }
1183
1184                 public static int ToInt32 (char value) 
1185                 { 
1186                         return (int)value; 
1187                 }
1188
1189                 public static int ToInt32 (DateTime value)
1190                 {
1191                         throw new InvalidCastException ("This conversion is not supported.");
1192                 }
1193         
1194                 public static int ToInt32 (decimal value) 
1195                 { 
1196                         if (value > Int32.MaxValue || value < Int32.MinValue) 
1197                                 throw new OverflowException (Locale.GetText (
1198                                         "Value is greater than Int32.MaxValue or less than Int32.MinValue"));
1199
1200                         // Returned Even-Rounded
1201                         return (int)(Math.Round (value));         
1202                 }
1203
1204                 public static int ToInt32 (double value) 
1205                 { 
1206                         if (value > Int32.MaxValue || value < Int32.MinValue) 
1207                                 throw new OverflowException (Locale.GetText (
1208                                         "Value is greater than Int32.MaxValue or less than Int32.MinValue"));
1209           
1210                         // Returned Even-Rounded
1211                         return (int)(Math.Round (value));         
1212                 }
1213  
1214                 public static int ToInt32 (float value) 
1215                 { 
1216                         if (value > Int32.MaxValue || value < Int32.MinValue) 
1217                                 throw new OverflowException (Locale.GetText (
1218                                         "Value is greater than Int32.MaxValue or less than Int32.MinValue"));
1219           
1220                         // Returned Even-Rounded, pass as a double, could just call
1221                         // Convert.ToInt32 ( (double)value);
1222                         return (int)(Math.Round ( (double)value));
1223                 }
1224
1225                 public static int ToInt32 (int value) 
1226                 { 
1227                         return value; 
1228                 }
1229         
1230                 public static int ToInt32 (long value) 
1231                 { 
1232                         if (value > Int32.MaxValue || value < Int32.MinValue) 
1233                                 throw new OverflowException (Locale.GetText (
1234                                         "Value is greater than Int32.MaxValue or less than Int32.MinValue"));
1235
1236                         return (int)value; 
1237                 }
1238
1239                 [CLSCompliant (false)]
1240                 public static int ToInt32 (sbyte value) 
1241                 { 
1242                         return (int)value; 
1243                 }
1244         
1245                 public static int ToInt32 (short value) 
1246                 { 
1247                         return (int)value; 
1248                 }
1249
1250                 public static int ToInt32 (string value) 
1251                 {
1252                         if (value == null)
1253                                 return 0; // LAMESPEC: Spec says throw ArgumentNullException
1254                         return Int32.Parse (value);
1255                 }
1256
1257                 public static int ToInt32 (string value, IFormatProvider provider) 
1258                 {
1259                         if (value == null)
1260                                 return 0; // LAMESPEC: Spec says throw ArgumentNullException
1261                         return Int32.Parse (value, provider);
1262                 }
1263
1264                 
1265                 public static int ToInt32 (string value, int fromBase)
1266                 {
1267                         return ConvertFromBase (value, fromBase, false);
1268                 }
1269                 
1270                 [CLSCompliant (false)]
1271                 public static int ToInt32 (uint value) 
1272                 { 
1273                         if (value > Int32.MaxValue) 
1274                                 throw new OverflowException (Locale.GetText (
1275                                         "Value is greater than Int32.MaxValue"));
1276
1277                         return (int)value; 
1278                 }
1279
1280                 [CLSCompliant (false)]
1281                 public static int ToInt32 (ulong value) 
1282                 { 
1283                         if (value > Int32.MaxValue) 
1284                                 throw new OverflowException (Locale.GetText (
1285                                         "Value is greater than Int32.MaxValue"));
1286
1287                         return (int)value; 
1288                 }
1289
1290                 [CLSCompliant (false)]
1291                 public static int ToInt32 (ushort value) 
1292                 { 
1293                         return (int)value; 
1294                 }
1295
1296                 public static int ToInt32 (object value)
1297                 {
1298                         if (value == null)
1299                                 return 0;
1300                         return ToInt32 (value, null);
1301                 }
1302
1303                 public static int ToInt32 (object value, IFormatProvider provider)
1304                 {
1305                         if (value == null)
1306                                 return 0;
1307                         return ((IConvertible) value).ToInt32 (provider);
1308                 }
1309
1310                 // ========== Int64 Conversions ========== //
1311
1312                 public static long ToInt64 (bool value) 
1313                 { 
1314                         return value ? 1 : 0; 
1315                 }
1316         
1317                 public static long ToInt64 (byte value) 
1318                 { 
1319                         return (long)(ulong)value; 
1320                 }
1321
1322                 public static long ToInt64 (char value) 
1323                 { 
1324                         return (long)value; 
1325                 }
1326
1327                 public static long ToInt64 (DateTime value)
1328                 {
1329                         throw new InvalidCastException ("This conversion is not supported.");
1330                 }
1331         
1332                 public static long ToInt64 (decimal value) 
1333                 { 
1334                         if (value > Int64.MaxValue || value < Int64.MinValue) 
1335                                 throw new OverflowException (Locale.GetText (
1336                                         "Value is greater than Int64.MaxValue or less than Int64.MinValue"));
1337           
1338                         // Returned Even-Rounded
1339                         return (long)(Math.Round (value));        
1340                 }
1341
1342                 public static long ToInt64 (double value) 
1343                 { 
1344                         if (value > Int64.MaxValue || value < Int64.MinValue) 
1345                                 throw new OverflowException (Locale.GetText (
1346                                         "Value is greater than Int64.MaxValue or less than Int64.MinValue"));
1347           
1348                         // Returned Even-Rounded
1349                         return (long)(Math.Round (value));
1350                 }
1351  
1352                 public static long ToInt64 (float value) 
1353                 { 
1354                         if (value > Int64.MaxValue || value < Int64.MinValue) 
1355                                 throw new OverflowException (Locale.GetText (
1356                                         "Value is greater than Int64.MaxValue or less than Int64.MinValue"));
1357           
1358                         // Returned Even-Rounded, pass to Math as a double, could
1359                         // just call Convert.ToInt64 ( (double)value);
1360                         return (long)(Math.Round ( (double)value));
1361                 }
1362
1363                 public static long ToInt64 (int value) 
1364                 { 
1365                         return (long)value; 
1366                 }
1367         
1368                 public static long ToInt64 (long value) 
1369                 { 
1370                         return value; 
1371                 }
1372
1373                 [CLSCompliant (false)]
1374                 public static long ToInt64 (sbyte value) 
1375                 { 
1376                         return (long)value; 
1377                 }
1378         
1379                 public static long ToInt64 (short value) 
1380                 { 
1381                         return (long)value; 
1382                 }
1383
1384                 public static long ToInt64 (string value) 
1385                 {
1386                         if (value == null)
1387                                 return 0; // LAMESPEC: Spec says throw ArgumentNullException
1388                         return Int64.Parse (value);
1389                 }
1390
1391                 public static long ToInt64 (string value, IFormatProvider provider) 
1392                 {
1393                         if (value == null)
1394                                 return 0; // LAMESPEC: Spec says throw ArgumentNullException
1395                         return Int64.Parse (value, provider);
1396                 }
1397
1398                 public static long ToInt64 (string value, int fromBase)
1399                 {
1400                         return ConvertFromBase64 (value, fromBase, false);
1401                 }
1402
1403                 [CLSCompliant (false)]
1404                 public static long ToInt64 (uint value) 
1405                 { 
1406                         return (long)(ulong)value; 
1407                 }
1408
1409                 [CLSCompliant (false)]
1410                 public static long ToInt64 (ulong value) 
1411                 { 
1412                         if (value > Int64.MaxValue) 
1413                                 throw new OverflowException (Locale.GetText (
1414                                         "Value is greater than Int64.MaxValue"));
1415
1416                         return (long)value; 
1417                 }
1418
1419                 [CLSCompliant (false)]
1420                 public static long ToInt64 (ushort value) 
1421                 { 
1422                         return (long)(ulong)value; 
1423                 }
1424
1425                 public static long ToInt64 (object value)
1426                 {
1427                         if (value == null)
1428                                 return 0;
1429                         return ToInt64 (value, null);
1430                 }
1431
1432                 public static long ToInt64 (object value, IFormatProvider provider)
1433                 {
1434                         if (value == null)
1435                                 return 0;
1436                         return ((IConvertible) value).ToInt64 (provider);
1437                 }
1438                 
1439                 // ========== SByte Conversions ========== //
1440
1441                 [CLSCompliant (false)]
1442                 public static sbyte ToSByte (bool value) 
1443                 { 
1444                         return (sbyte)(value ? 1 : 0); 
1445                 }
1446
1447                 [CLSCompliant (false)]
1448                 public static sbyte ToSByte (byte value) 
1449                 { 
1450                         if (value > SByte.MaxValue)
1451                                 throw new OverflowException (Locale.GetText (
1452                                         "Value is greater than SByte.MaxValue"));
1453
1454                         return (sbyte)value; 
1455                 }
1456
1457                 [CLSCompliant (false)]
1458                 public static sbyte ToSByte (char value) 
1459                 { 
1460                         if (value > SByte.MaxValue)
1461                                 throw new OverflowException (Locale.GetText (
1462                                         "Value is greater than SByte.MaxValue"));
1463
1464                         return (sbyte)value;
1465                 }
1466
1467                 [CLSCompliant (false)]
1468                 public static sbyte ToSByte (DateTime value)
1469                 {
1470                         throw new InvalidCastException ("This conversion is not supported.");
1471                 }
1472                 
1473                 [CLSCompliant (false)]  
1474                 public static sbyte ToSByte (decimal value) 
1475                 { 
1476                         if (value > SByte.MaxValue || value < SByte.MinValue)
1477                                 throw new OverflowException (Locale.GetText (
1478                                         "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
1479           
1480                         // Returned Even-Rounded
1481                         return (sbyte)(Math.Round (value));
1482                 }
1483
1484                 [CLSCompliant (false)]
1485                 public static sbyte ToSByte (double value) 
1486                 { 
1487                         if (value > SByte.MaxValue || value < SByte.MinValue)
1488                                 throw new OverflowException (Locale.GetText (
1489                                         "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
1490
1491                         // Returned Even-Rounded
1492                         return (sbyte)(Math.Round (value));
1493                 }
1494
1495                 [CLSCompliant (false)]
1496                 public static sbyte ToSByte (float value) 
1497                 { 
1498                         if (value > SByte.MaxValue || value < SByte.MinValue)
1499                                 throw new OverflowException (Locale.GetText (
1500                                         "Value is greater than SByte.MaxValue or less than SByte.Minalue"));
1501
1502                         // Returned Even-Rounded, pass as double to Math
1503                         return (sbyte)(Math.Round ( (double)value));
1504                 }
1505
1506                 [CLSCompliant (false)]
1507                 public static sbyte ToSByte (int value) 
1508                 { 
1509                         if (value > SByte.MaxValue || value < SByte.MinValue)
1510                                 throw new OverflowException (Locale.GetText (
1511                                         "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
1512           
1513                         return (sbyte)value; 
1514                 }
1515
1516                 [CLSCompliant (false)]
1517                 public static sbyte ToSByte (long value) 
1518                 { 
1519                         if (value > SByte.MaxValue || value < SByte.MinValue)
1520                                 throw new OverflowException (Locale.GetText (
1521                                         "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
1522           
1523                         return (sbyte)value;
1524                 }
1525
1526                 [CLSCompliant (false)]
1527                 public static sbyte ToSByte (sbyte value) 
1528                 { 
1529                         return value;
1530                 }
1531
1532                 [CLSCompliant (false)]
1533                 public static sbyte ToSByte (short value) 
1534                 { 
1535                         if (value > SByte.MaxValue || value < SByte.MinValue)
1536                                 throw new OverflowException (Locale.GetText (
1537                                         "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
1538           
1539                         return (sbyte)value; 
1540                 }
1541
1542                 [CLSCompliant (false)]
1543                 public static sbyte ToSByte (string value) 
1544                 {
1545                         if (value == null)
1546                                 return 0; // LAMESPEC: Spec says throw ArgumentNullException
1547                         return SByte.Parse (value);
1548                 }
1549                 
1550                 [CLSCompliant (false)]
1551                 public static sbyte ToSByte (string value, IFormatProvider provider) 
1552                 {
1553                         if (value == null)
1554                                 throw new ArgumentNullException ("value");
1555                         return SByte.Parse (value, provider);
1556                 }
1557
1558                 [CLSCompliant (false)]
1559                 public static sbyte ToSByte (string value, int fromBase)
1560                 {
1561                         int result = ConvertFromBase (value, fromBase, false);
1562                         if (fromBase != 10) {
1563                                 // note: no sign are available to detect negatives
1564                                 if (result > SByte.MaxValue) {
1565                                         // return negative 2's complement
1566                                         return Convert.ToSByte (-(256 - result));
1567                                 }
1568                         }
1569                         return Convert.ToSByte (result);
1570                 }
1571                 
1572                 [CLSCompliant (false)]
1573                 public static sbyte ToSByte (uint value) 
1574                 { 
1575                         if (value > SByte.MaxValue)
1576                                 throw new OverflowException (Locale.GetText (
1577                                         "Value is greater than SByte.MaxValue"));
1578
1579                         return (sbyte)value;
1580                 }
1581
1582                 [CLSCompliant (false)]
1583                 public static sbyte ToSByte (ulong value) 
1584                 { 
1585                         if (value > (ulong)SByte.MaxValue)
1586                                 throw new OverflowException (Locale.GetText (
1587                                         "Value is greater than SByte.MaxValue"));
1588
1589                         return (sbyte)value;
1590                 }
1591
1592                 [CLSCompliant (false)]
1593                 public static sbyte ToSByte (ushort value) 
1594                 { 
1595                         if (value > SByte.MaxValue)
1596                                 throw new OverflowException (Locale.GetText (
1597                                         "Value is greater than SByte.MaxValue"));
1598
1599                         return (sbyte)value;
1600                 }
1601
1602                 [CLSCompliant (false)]
1603                 public static sbyte ToSByte (object value)
1604                 {
1605                         if (value == null)
1606                                 return 0;
1607                         return ToSByte (value, null);
1608                 }
1609
1610                 [CLSCompliant (false)]
1611                 public static sbyte ToSByte (object value, IFormatProvider provider)
1612                 {
1613                         if (value == null)
1614                                 return 0;
1615                         return ((IConvertible) value).ToSByte (provider);
1616                 }
1617
1618                 // ========== Single Conversions ========== //
1619         
1620                 public static float ToSingle (bool value) 
1621                 { 
1622                         return value ? 1 : 0; 
1623                 }
1624         
1625                 public static float ToSingle (byte value) 
1626                 { 
1627                         return (float)value; 
1628                 }
1629
1630                 public static float ToSingle (Char value)
1631                 {
1632                         throw new InvalidCastException ("This conversion is not supported.");
1633                 }
1634
1635                 public static float ToSingle (DateTime value)
1636                 {
1637                         throw new InvalidCastException ("This conversion is not supported.");
1638                 }
1639         
1640                 public static float ToSingle (decimal value) 
1641                 { 
1642                         return (float)value; 
1643                 }
1644
1645                 public static float ToSingle (double value) 
1646                 { 
1647                         return (float)value; 
1648                 }
1649         
1650                 public static float ToSingle (float value) 
1651                 { 
1652                         return value; 
1653                 }
1654
1655                 public static float ToSingle (int value) 
1656                 { 
1657                         return (float)value; 
1658                 }
1659         
1660                 public static float ToSingle (long value) 
1661                 { 
1662                         return (float)value; 
1663                 }
1664
1665                 [CLSCompliant (false)]
1666                 public static float ToSingle (sbyte value) 
1667                 { 
1668                         return (float)value; 
1669                 }
1670         
1671                 public static float ToSingle (short value) 
1672                 { 
1673                         return (float)value; 
1674                 }
1675
1676                 public static float ToSingle (string value) 
1677                 {
1678                         if (value == null)
1679                                 return 0.0f; // LAMESPEC: Spec says throw ArgumentNullException
1680                         return Single.Parse (value);
1681                 }
1682
1683                 public static float ToSingle (string value, IFormatProvider provider) 
1684                 {
1685                         if (value == null)
1686                                 return 0.0f; // LAMESPEC: Spec says throw ArgumentNullException
1687                         return Single.Parse (value, provider);
1688                 }              
1689
1690                 [CLSCompliant (false)]
1691                 public static float ToSingle (uint value) 
1692                 { 
1693                         return (float)value; 
1694                 }
1695
1696                 [CLSCompliant (false)]
1697                 public static float ToSingle (ulong value) 
1698                 { 
1699                         return (float)value; 
1700                 }
1701
1702                 [CLSCompliant (false)]
1703                 public static float ToSingle (ushort value) 
1704                 { 
1705                         return (float)value; 
1706                 }
1707
1708                 public static float ToSingle (object value)
1709                 {
1710                         if (value == null)
1711                                 return 0.0f;
1712                         return ToSingle (value, null);
1713                 }
1714
1715 //              [CLSCompliant (false)]
1716                 public static float ToSingle (object value, IFormatProvider provider)
1717                 {
1718                         if (value == null)
1719                                 return 0.0f;
1720                         return ((IConvertible) value).ToSingle (provider);
1721                 }
1722
1723                 // ========== String Conversions ========== //
1724         
1725                 public static string ToString (bool value) 
1726                 { 
1727                         return value.ToString (); 
1728                 }
1729
1730                 public static string ToString (bool value, IFormatProvider provider)
1731                 {
1732                         return value.ToString (); // the same as ToString (bool).
1733                 }
1734         
1735                 public static string ToString (byte value) 
1736                 { 
1737                         return value.ToString (); 
1738                 }
1739         
1740                 public static string ToString (byte value, IFormatProvider provider) 
1741                 {
1742                         return value.ToString (provider); 
1743                 }
1744
1745                 public static string ToString (byte value, int toBase)
1746                 {
1747                         if (value == 0)
1748                                 return "0";
1749                         if (toBase == 10)
1750                                 return value.ToString ();
1751                         
1752                         byte[] val = BitConverter.GetBytes (value);
1753
1754                         switch (toBase) {
1755                         case 2:
1756                                 return ConvertToBase2 (val);
1757                         case 8:
1758                                 return ConvertToBase8 (val);
1759                         case 16:
1760                                 return ConvertToBase16 (val);
1761                         default:
1762                                 throw new ArgumentException (Locale.GetText ("toBase is not valid."));
1763                         }
1764                 }
1765
1766                 public static string ToString (char value) 
1767                 { 
1768                         return value.ToString (); 
1769                 }
1770
1771                 public static string ToString (char value, IFormatProvider provider)
1772                 {
1773                         return value.ToString (); // the same as ToString (char)
1774                 }
1775
1776                 public static string ToString (DateTime value) 
1777                 { 
1778                         return value.ToString (); 
1779                 }
1780
1781                 public static string ToString (DateTime value, IFormatProvider provider) 
1782                 { 
1783                         return value.ToString (provider); 
1784                 }
1785
1786                 public static string ToString (decimal value) 
1787                 {
1788                         return value.ToString ();
1789                 }
1790
1791                 public static string ToString (decimal value, IFormatProvider provider) 
1792                 { 
1793                         return value.ToString (provider); 
1794                 }
1795         
1796                 public static string ToString (double value) 
1797                 { 
1798                         return value.ToString (); 
1799                 }
1800
1801                 public static string ToString (double value, IFormatProvider provider) 
1802                 { 
1803                         return value.ToString (provider);
1804                 }
1805         
1806                 public static string ToString (float value) 
1807                 { 
1808                         return value.ToString (); 
1809                 }
1810
1811                 public static string ToString (float value, IFormatProvider provider) 
1812                 { 
1813                         return value.ToString (provider); 
1814                 }
1815
1816                 public static string ToString (int value) 
1817                 { 
1818                         return value.ToString (); 
1819                 }
1820
1821                 public static string ToString (int value, int toBase)
1822                 {
1823                         if (value == 0)
1824                                 return "0";
1825                         if (toBase == 10)
1826                                 return value.ToString ();
1827                         
1828                         byte[] val = BitConverter.GetBytes (value);
1829
1830                         switch (toBase) {
1831                         case 2:
1832                                 return ConvertToBase2 (val);
1833                         case 8:
1834                                 return ConvertToBase8 (val);
1835                         case 16:
1836                                 return ConvertToBase16 (val);
1837                         default:
1838                                 throw new ArgumentException (Locale.GetText ("toBase is not valid."));
1839                         }
1840                 }
1841
1842                 public static string ToString (int value, IFormatProvider provider) 
1843                 { 
1844                         return value.ToString (provider); 
1845                 }
1846         
1847                 public static string ToString (long value) 
1848                 { 
1849                         return value.ToString (); 
1850                 }
1851
1852                 public static string ToString (long value, int toBase)
1853                 {
1854                         if (value == 0)
1855                                 return "0";
1856                         if (toBase == 10)
1857                                 return value.ToString ();
1858                         
1859                         byte[] val = BitConverter.GetBytes (value);
1860
1861                         switch (toBase) {
1862                         case 2:
1863                                 return ConvertToBase2 (val);
1864                         case 8:
1865                                 return ConvertToBase8 (val);
1866                         case 16:
1867                                 return ConvertToBase16 (val);
1868                         default:
1869                                 throw new ArgumentException (Locale.GetText ("toBase is not valid."));
1870                         }
1871                 }
1872
1873                 public static string ToString (long value, IFormatProvider provider) 
1874                 { 
1875                         return value.ToString (provider); 
1876                 }
1877
1878                 public static string ToString (object value)
1879                 {
1880                         return ToString (value, null);
1881                 }               
1882
1883                 public static string ToString (object value, IFormatProvider provider)
1884                 {
1885                         if (value is IConvertible)
1886                                 return ((IConvertible) value).ToString (provider);
1887                         else if (value != null)
1888                                 return value.ToString ();
1889                         return String.Empty;
1890                 }                               
1891
1892                 [CLSCompliant (false)]
1893                 public static string ToString (sbyte value) 
1894                 { 
1895                         return value.ToString (); 
1896                 }
1897
1898                 [CLSCompliant (false)]                          
1899                 public static string ToString (sbyte value, IFormatProvider provider) 
1900                 { 
1901                         return value.ToString (provider); 
1902                 }
1903         
1904                 public static string ToString (short value) 
1905                 { 
1906                         return value.ToString (); 
1907                 }
1908
1909                 public static string ToString (short value, int toBase)
1910                 {
1911                         if (value == 0)
1912                                 return "0";
1913                         if (toBase == 10)
1914                                 return value.ToString ();
1915                         
1916                         byte[] val = BitConverter.GetBytes (value);
1917
1918                         switch (toBase) {
1919                         case 2:
1920                                 return ConvertToBase2 (val);
1921                         case 8:
1922                                 return ConvertToBase8 (val);
1923                         case 16:
1924                                 return ConvertToBase16 (val);
1925                         default:
1926                                 throw new ArgumentException (Locale.GetText ("toBase is not valid."));
1927                         }
1928                 }
1929
1930                 public static string ToString (short value, IFormatProvider provider) 
1931                 { 
1932                         return value.ToString (provider); 
1933                 }
1934
1935                 public static string ToString (string value) 
1936                 {
1937                         return value;
1938                 }
1939
1940                 public static string ToString (string value, IFormatProvider provider)
1941                 {
1942                         return value; // provider is ignored.
1943                 }
1944
1945                 [CLSCompliant (false)]
1946                 public static string ToString (uint value) 
1947                 { 
1948                         return value.ToString (); 
1949                 }
1950
1951                 [CLSCompliant (false)]
1952                 public static string ToString (uint value, IFormatProvider provider) 
1953                 { 
1954                         return value.ToString (provider); 
1955                 }
1956
1957                 [CLSCompliant (false)]
1958                 public static string ToString (ulong value) 
1959                 { 
1960                         return value.ToString (); 
1961                 }
1962
1963                 [CLSCompliant (false)]
1964                 public static string ToString (ulong value, IFormatProvider provider) 
1965                 { 
1966                         return value.ToString (provider); 
1967                 }
1968
1969                 [CLSCompliant (false)]
1970                 public static string ToString (ushort value) 
1971                 { 
1972                         return value.ToString (); 
1973                 }
1974
1975                 [CLSCompliant (false)]
1976                 public static string ToString (ushort value, IFormatProvider provider) 
1977                 { 
1978                         return value.ToString (provider); 
1979                 }
1980                 
1981                 // ========== UInt16 Conversions ========== //
1982
1983                 [CLSCompliant (false)]
1984                 public static ushort ToUInt16 (bool value) 
1985                 { 
1986                         return (ushort)(value ? 1 : 0); 
1987                 }
1988
1989                 [CLSCompliant (false)]
1990                 public static ushort ToUInt16 (byte value) 
1991                 { 
1992                         return (ushort)value; 
1993                 }
1994
1995                 [CLSCompliant (false)]
1996                 public static ushort ToUInt16 (char value) 
1997                 { 
1998                         return (ushort)value; 
1999                 }
2000
2001                 [CLSCompliant (false)]
2002                 public static ushort ToUInt16 (DateTime value)
2003                 {
2004                         throw new InvalidCastException ("This conversion is not supported.");
2005                 }
2006
2007                 [CLSCompliant (false)]
2008                 public static ushort ToUInt16 (decimal value) 
2009                 { 
2010                         if (value > UInt16.MaxValue || value < UInt16.MinValue) 
2011                                 throw new OverflowException (Locale.GetText (
2012                                         "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
2013           
2014                         // Returned Even-Rounded
2015                         return (ushort)(Math.Round (value));      
2016                 }
2017
2018                 [CLSCompliant (false)]
2019                 public static ushort ToUInt16 (double value) 
2020                 { 
2021                         if (value > UInt16.MaxValue || value < UInt16.MinValue) 
2022                                 throw new OverflowException (Locale.GetText (
2023                                         "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
2024           
2025                         // Returned Even-Rounded
2026                         return (ushort)(Math.Round (value));
2027                 }
2028
2029                 [CLSCompliant (false)]
2030                 public static ushort ToUInt16 (float value) 
2031                 { 
2032                         if (value > UInt16.MaxValue || value < UInt16.MinValue) 
2033                                 throw new OverflowException (Locale.GetText (
2034                                         "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
2035           
2036                         // Returned Even-Rounded, pass as double to Math
2037                         return (ushort)(Math.Round ( (double)value));
2038                 }
2039
2040                 [CLSCompliant (false)]
2041                 public static ushort ToUInt16 (int value) 
2042                 { 
2043                         if (value > UInt16.MaxValue || value < UInt16.MinValue) 
2044                                 throw new OverflowException (Locale.GetText (
2045                                         "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
2046
2047                         return (ushort)value; 
2048                 }
2049
2050                 [CLSCompliant (false)]
2051                 public static ushort ToUInt16 (long value) 
2052                 { 
2053                         if (value > UInt16.MaxValue || value < UInt16.MinValue) 
2054                                 throw new OverflowException (Locale.GetText (
2055                                         "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
2056
2057                         return (ushort)value; 
2058                 }
2059
2060                 [CLSCompliant (false)]
2061                 public static ushort ToUInt16 (sbyte value) 
2062                 { 
2063                         if (value < UInt16.MinValue) 
2064                                 throw new OverflowException (Locale.GetText (
2065                                         "Value is less than UInt16.MinValue"));
2066
2067                         return (ushort)value; 
2068                 }
2069
2070                 [CLSCompliant (false)]
2071                 public static ushort ToUInt16 (short value) 
2072                 { 
2073                         if (value < UInt16.MinValue) 
2074                                 throw new OverflowException (Locale.GetText (
2075                                         "Value is less than UInt16.MinValue"));
2076
2077                         return (ushort)value; 
2078                 }
2079                 
2080                 [CLSCompliant (false)]
2081                 public static ushort ToUInt16 (string value) 
2082                 {
2083                         if (value == null)
2084                                 return 0; // LAMESPEC: Spec says throw ArgumentNullException
2085                         return UInt16.Parse (value);
2086                 }
2087
2088                 [CLSCompliant (false)]
2089                 public static ushort ToUInt16 (string value, IFormatProvider provider) 
2090                 {
2091                         if (value == null)
2092                                 return 0; // LAMESPEC: Spec says throw ArgumentNullException
2093                         return UInt16.Parse (value, provider);
2094                 }
2095
2096                 [CLSCompliant (false)]
2097                 public static ushort ToUInt16 (string value, int fromBase) 
2098                 {
2099                         return ToUInt16 (ConvertFromBase (value, fromBase, true));
2100                 } 
2101
2102                 [CLSCompliant (false)]
2103                 public static ushort ToUInt16 (uint value) 
2104                 { 
2105                         if (value > UInt16.MaxValue) 
2106                                 throw new OverflowException (Locale.GetText (
2107                                         "Value is greater than UInt16.MaxValue"));
2108
2109                         return (ushort)value; 
2110                 }
2111
2112                 [CLSCompliant (false)]
2113                 public static ushort ToUInt16 (ulong value) 
2114                 { 
2115                         if (value > (ulong)UInt16.MaxValue) 
2116                                 throw new OverflowException (Locale.GetText (
2117                                         "Value is greater than UInt16.MaxValue"));
2118
2119                         return (ushort)value; 
2120                 }
2121
2122                 [CLSCompliant (false)]
2123                 public static ushort ToUInt16 (ushort value) 
2124                 { 
2125                         return value; 
2126                 }
2127
2128                 [CLSCompliant (false)]
2129                 public static ushort ToUInt16 (object value)
2130                 {
2131                         if (value == null)
2132                                 return 0;
2133                         return ToUInt16 (value, null);
2134                 }
2135
2136                 [CLSCompliant (false)]
2137                 public static ushort ToUInt16 (object value, IFormatProvider provider)
2138                 {
2139                         if (value == null)
2140                                 return 0;
2141                         return ((IConvertible) value).ToUInt16 (provider);
2142                 }
2143
2144                 // ========== UInt32 Conversions ========== //
2145
2146                 [CLSCompliant (false)]
2147                 public static uint ToUInt32 (bool value) 
2148                 { 
2149                         return (uint)(value ? 1 : 0); 
2150                 }
2151
2152                 [CLSCompliant (false)]
2153                 public static uint ToUInt32 (byte value) 
2154                 { 
2155                         return (uint)value; 
2156                 }
2157
2158                 [CLSCompliant (false)]
2159                 public static uint ToUInt32 (char value) 
2160                 { 
2161                         return (uint)value; 
2162                 }
2163
2164                 [CLSCompliant (false)]
2165                 public static uint ToUInt32 (DateTime value)
2166                 {
2167                         throw new InvalidCastException ("This conversion is not supported.");
2168                 }
2169                 
2170                 [CLSCompliant (false)]
2171                 public static uint ToUInt32 (decimal value) 
2172                 { 
2173                         if (value > UInt32.MaxValue || value < UInt32.MinValue) 
2174                                 throw new OverflowException (Locale.GetText (
2175                                         "Value is greater than UInt32.MaxValue or less than UInt32.MinValue"));
2176           
2177                         // Returned Even-Rounded
2178                         return (uint)(Math.Round (value));        
2179                 }
2180
2181                 [CLSCompliant (false)]
2182                 public static uint ToUInt32 (double value) 
2183                 { 
2184                         if (value > UInt32.MaxValue || value < UInt32.MinValue) 
2185                                 throw new OverflowException (Locale.GetText (
2186                                         "Value is greater than UInt32.MaxValue or less than UInt32.MinValue"));
2187           
2188                         // Returned Even-Rounded
2189                         return (uint)(Math.Round (value));        
2190                 }
2191
2192                 [CLSCompliant (false)]
2193                 public static uint ToUInt32 (float value) 
2194                 { 
2195                         if (value > UInt32.MaxValue || value < UInt32.MinValue) 
2196                                 throw new OverflowException (Locale.GetText (
2197                                         "Value is greater than UInt32.MaxValue or less than UInt32.MinValue"));
2198           
2199                         // Returned Even-Rounded, pass as double to Math
2200                         return (uint)(Math.Round ( (double)value));
2201                 }
2202
2203                 [CLSCompliant (false)]
2204                 public static uint ToUInt32 (int value) 
2205                 { 
2206                         if (value < UInt32.MinValue) 
2207                                 throw new OverflowException (Locale.GetText (
2208                                         "Value is less than UInt32.MinValue"));
2209
2210                         return (uint)value; 
2211                 }
2212
2213                 [CLSCompliant (false)]
2214                 public static uint ToUInt32 (long value) 
2215                 { 
2216                         if (value > UInt32.MaxValue || value < UInt32.MinValue) 
2217                                 throw new OverflowException (Locale.GetText (
2218                                         "Value is greater than UInt32.MaxValue or less than UInt32.MinValue"));
2219
2220                         return (uint)value; 
2221                 }
2222
2223                 [CLSCompliant (false)]
2224                 public static uint ToUInt32 (sbyte value) 
2225                 { 
2226                         if (value < UInt32.MinValue) 
2227                                 throw new OverflowException (Locale.GetText (
2228                                         "Value is less than UInt32.MinValue"));
2229
2230                         return (uint)value; 
2231                 }
2232
2233                 [CLSCompliant (false)]
2234                 public static uint ToUInt32 (short value) 
2235                 { 
2236                         if (value < UInt32.MinValue) 
2237                                 throw new OverflowException (Locale.GetText (
2238                                         "Value is less than UInt32.MinValue"));
2239
2240                         return (uint)value; 
2241                 }
2242
2243                 [CLSCompliant (false)]
2244                 public static uint ToUInt32 (string value) 
2245                 {
2246                         if (value == null)
2247                                 return 0; // LAMESPEC: Spec says throw ArgumentNullException
2248                         return UInt32.Parse (value);
2249                 }
2250
2251                 [CLSCompliant (false)]
2252                 public static uint ToUInt32 (string value, IFormatProvider provider) 
2253                 {
2254                         if (value == null)
2255                                 return 0; // LAMESPEC: Spec says throw ArgumentNullException
2256                         return UInt32.Parse (value, provider);
2257                 }
2258
2259                 [CLSCompliant (false)]
2260                 public static uint ToUInt32 (string value, int fromBase)
2261                 {
2262                         return (uint) ConvertFromBase (value, fromBase, true);
2263                 }
2264
2265                 [CLSCompliant (false)]
2266                 public static uint ToUInt32 (uint value) 
2267                 { 
2268                         return value; 
2269                 }
2270
2271                 [CLSCompliant (false)]
2272                 public static uint ToUInt32 (ulong value) 
2273                 { 
2274                         if (value > UInt32.MaxValue) 
2275                                 throw new OverflowException (Locale.GetText (
2276                                         "Value is greater than UInt32.MaxValue"));
2277
2278                         return (uint)value; 
2279                 }
2280
2281                 [CLSCompliant (false)]
2282                 public static uint ToUInt32 (ushort value) 
2283                 { 
2284                         return (uint)value; 
2285                 }
2286
2287                 [CLSCompliant (false)]
2288                 public static uint ToUInt32 (object value)
2289                 {
2290                         if (value == null)
2291                                 return 0;
2292                         return ToUInt32 (value, null);
2293                 }               
2294
2295                 [CLSCompliant (false)]
2296                 public static uint ToUInt32 (object value, IFormatProvider provider)
2297                 {
2298                         if (value == null)
2299                                 return 0;
2300                         return ((IConvertible) value).ToUInt32 (provider);
2301                 }               
2302                 
2303
2304                 // ========== UInt64 Conversions ========== //
2305
2306                 [CLSCompliant (false)]
2307                 public static ulong ToUInt64 (bool value) 
2308                 { 
2309                         return (ulong)(value ? 1 : 0); 
2310                 }
2311
2312                 [CLSCompliant (false)]
2313                 public static ulong ToUInt64 (byte value) 
2314                 { 
2315                         return (ulong)value; 
2316                 }
2317
2318                 [CLSCompliant (false)]
2319                 public static ulong ToUInt64 (char value) 
2320                 { 
2321                         return (ulong)value; 
2322                 }
2323
2324                 [CLSCompliant (false)]
2325                 public static ulong ToUInt64 (DateTime value)
2326                 {
2327                         throw new InvalidCastException ("The conversion is not supported.");
2328                 }
2329
2330                 [CLSCompliant (false)]
2331                 public static ulong ToUInt64 (decimal value) 
2332                 { 
2333                         if (value > UInt64.MaxValue || value < UInt64.MinValue) 
2334                                 throw new OverflowException (Locale.GetText (
2335                                         "Value is greater than UInt64.MaxValue or less than UInt64.MinValue"));
2336           
2337                         // Returned Even-Rounded
2338                         return (ulong)(Math.Round (value));       
2339                 }
2340
2341                 [CLSCompliant (false)]
2342                 public static ulong ToUInt64 (double value) 
2343                 { 
2344                         if (value > UInt64.MaxValue || value < UInt64.MinValue) 
2345                                 throw new OverflowException (Locale.GetText (
2346                                         "Value is greater than UInt64.MaxValue or less than UInt64.MinValue"));
2347           
2348                         // Returned Even-Rounded
2349                         return (ulong)(Math.Round (value));       
2350                 }
2351                 
2352                 [CLSCompliant (false)] 
2353                 public static ulong ToUInt64 (float value) 
2354                 { 
2355                         if (value > UInt64.MaxValue || value < UInt64.MinValue) 
2356                                 throw new OverflowException (Locale.GetText (
2357                                         "Value is greater than UInt64.MaxValue or less than UInt64.MinValue"));
2358           
2359                         // Returned Even-Rounded, pass as a double to Math
2360                         return (ulong)(Math.Round ( (double)value));
2361                 }
2362                 
2363                 [CLSCompliant (false)]
2364                 public static ulong ToUInt64 (int value) 
2365                 { 
2366                         if (value < (int)UInt64.MinValue) 
2367                                 throw new OverflowException (Locale.GetText (
2368                                         "Value is less than UInt64.MinValue"));
2369
2370                         return (ulong)value; 
2371                 }
2372                 
2373                 [CLSCompliant (false)]
2374                 public static ulong ToUInt64 (long value) 
2375                 { 
2376                         if (value < (long)UInt64.MinValue) 
2377                                 throw new OverflowException (Locale.GetText (
2378                                         "Value is less than UInt64.MinValue"));
2379
2380                         return (ulong)value; 
2381                 }
2382
2383                 [CLSCompliant (false)]
2384                 public static ulong ToUInt64 (sbyte value) 
2385                 { 
2386                         if (value < (sbyte)UInt64.MinValue) 
2387                                 throw new OverflowException
2388                                 ("Value is less than UInt64.MinValue");
2389
2390                         return (ulong)value; 
2391                 }
2392                 
2393                 [CLSCompliant (false)]  
2394                 public static ulong ToUInt64 (short value) 
2395                 { 
2396                         if (value < (short)UInt64.MinValue) 
2397                                 throw new OverflowException (Locale.GetText (
2398                                         "Value is less than UInt64.MinValue"));
2399
2400                         return (ulong)value; 
2401                 }
2402
2403                 [CLSCompliant (false)]
2404                 public static ulong ToUInt64 (string value) 
2405                 {
2406                         if (value == null)
2407                                 return 0; // LAMESPEC: Spec says throw ArgumentNullException
2408                         return UInt64.Parse (value);
2409                 }
2410
2411                 [CLSCompliant (false)]
2412                 public static ulong ToUInt64 (string value, IFormatProvider provider) 
2413                 {
2414                         if (value == null)
2415                                 return 0; // LAMESPEC: Spec says throw ArgumentNullException
2416                         return UInt64.Parse (value, provider);
2417                 }
2418
2419                 [CLSCompliant (false)]
2420                 public static ulong ToUInt64 (string value, int fromBase)
2421                 {
2422                         return (ulong) ConvertFromBase64 (value, fromBase, true);
2423                 }
2424
2425                 [CLSCompliant (false)]
2426                 public static ulong ToUInt64 (uint value) 
2427                 { 
2428                         return (ulong)value; 
2429                 }
2430
2431                 [CLSCompliant (false)]
2432                 public static ulong ToUInt64 (ulong value) 
2433                 { 
2434                         return value; 
2435                 }
2436                 
2437                 [CLSCompliant (false)]
2438                 public static ulong ToUInt64 (ushort value) 
2439                 { 
2440                         return (ulong)value; 
2441                 }
2442
2443                 [CLSCompliant (false)]
2444                 public static ulong ToUInt64 (object value)
2445                 {
2446                         if (value == null)
2447                                 return 0;
2448                         return ToUInt64 (value, null);
2449                 }               
2450
2451                 [CLSCompliant (false)]
2452                 public static ulong ToUInt64 (object value, IFormatProvider provider)
2453                 {
2454                         if (value == null)
2455                                 return 0;
2456                         return ((IConvertible) value).ToUInt64 (provider);
2457                 }               
2458                 
2459
2460                 // ========== Conversion / Helper Functions ========== //
2461
2462                 public static object ChangeType (object value, Type conversionType)
2463                 {
2464                         if ((value != null) && (conversionType == null))
2465                                 throw new ArgumentNullException ("conversionType");
2466                         CultureInfo ci = CultureInfo.CurrentCulture;
2467                         IFormatProvider provider;
2468                         if (conversionType == typeof(DateTime)) {
2469                                 provider = ci.DateTimeFormat;
2470                         }
2471                         else {
2472                                 provider = ci.NumberFormat;
2473                         }
2474                         return ToType (value, conversionType, provider);
2475                 }
2476                 
2477                 public static object ChangeType (object value, TypeCode typeCode)
2478                 {
2479                         CultureInfo ci = CultureInfo.CurrentCulture;
2480                         Type conversionType = conversionTable [(int) typeCode];
2481                         IFormatProvider provider;
2482                         if (conversionType == typeof(DateTime)) {
2483                                 provider = ci.DateTimeFormat;
2484                         }
2485                         else {
2486                                 provider = ci.NumberFormat;
2487                         }
2488                         return ToType (value, conversionType, provider);
2489                 }
2490
2491                 public static object ChangeType (object value, Type conversionType, IFormatProvider provider)
2492                 {
2493                         if ((value != null) && (conversionType == null))
2494                                 throw new ArgumentNullException ("conversionType");
2495                         return ToType (value, conversionType, provider);
2496                 }
2497                 
2498                 public static object ChangeType (object value, TypeCode typeCode, IFormatProvider provider)
2499                 {
2500                         Type conversionType = conversionTable [(int)typeCode];
2501                         return ToType (value, conversionType, provider);
2502                 }
2503
2504                 private static bool NotValidBase (int value)
2505                 {
2506                         if ((value == 2) || (value == 8) ||
2507                            (value == 10) || (value == 16))
2508                                 return false;
2509                         
2510                         return true;
2511                 }
2512
2513                 private static int ConvertFromBase (string value, int fromBase, bool unsigned)
2514                 {
2515                         if (NotValidBase (fromBase))
2516                                 throw new ArgumentException ("fromBase is not valid.");
2517                         if (value == null)
2518                                 return 0;
2519
2520                         int chars = 0;
2521                         int result = 0;
2522                         int digitValue;
2523
2524                         int i=0; 
2525                         int len = value.Length;
2526                         bool negative = false;
2527
2528                         // special processing for some bases
2529                         switch (fromBase) {
2530                                 case 10:
2531                                         if (value.Substring (i, 1) == "-") {
2532                                                 if (unsigned) {
2533                                                         throw new OverflowException (
2534                                                                 Locale.GetText ("The string was being parsed as"
2535                                                                 + " an unsigned number and could not have a"
2536                                                                 + " negative sign."));
2537                                                 }
2538                                                 negative = true;
2539                                                 i++;
2540                                         }
2541                                         break;
2542                                 case 16:
2543                                         if (value.Substring (i, 1) == "-") {
2544                                                 throw new ArgumentException ("String cannot contain a "
2545                                                         + "minus sign if the base is not 10.");
2546                                         }
2547                                         if (len >= i + 2) {
2548                                                 // 0x00 or 0X00
2549                                                 if ((value[i] == '0') && ((value [i+1] == 'x') || (value [i+1] == 'X'))) {
2550                                                         i+=2;
2551                                                 }
2552                                         }
2553                                         break;
2554                                 default:
2555                                         if (value.Substring (i, 1) == "-") {
2556                                                 throw new ArgumentException ("String cannot contain a "
2557                                                         + "minus sign if the base is not 10.");
2558                                         }
2559                                         break;
2560                         }
2561
2562                         if (len == i) {
2563                                 throw new FormatException ("Could not find any parsable digits.");
2564                         }
2565
2566                         if (value[i] == '+') {
2567                                 i++;
2568                         }
2569
2570                         while (i < len) {
2571                                 char c = value[i++];
2572                                 if (Char.IsNumber (c)) {
2573                                         digitValue = c - '0';
2574                                 } else if (Char.IsLetter (c)) {
2575                                         digitValue = Char.ToLowerInvariant (c) - 'a' + 10;
2576                                 } else {
2577                                         if (chars > 0) {
2578                                                 throw new FormatException ("Additional unparsable "
2579                                                         + "characters are at the end of the string.");
2580                                         } else {
2581                                                 throw new FormatException ("Could not find any parsable"
2582                                                         + " digits.");
2583                                         }
2584                                 }
2585
2586                                 if (digitValue >= fromBase) {
2587                                         if (chars > 0) {
2588                                                 throw new FormatException ("Additional unparsable "
2589                                                         + "characters are at the end of the string.");
2590                                         } else {
2591                                                 throw new FormatException ("Could not find any parsable"
2592                                                         + " digits.");
2593                                         }
2594                                 }
2595
2596                                 result = (fromBase) * result + digitValue;
2597                                 chars ++;
2598                         }
2599
2600                         if (chars == 0)
2601                                 throw new FormatException ("Could not find any parsable digits.");
2602
2603                         if (negative)
2604                                 return -result;
2605                         else
2606                                 return result;
2607                 }
2608
2609                 // note: this has nothing to do with base64 encoding (just base and Int64)
2610                 private static long ConvertFromBase64 (string value, int fromBase, bool unsigned)
2611                 {
2612                         if (NotValidBase (fromBase))
2613                                 throw new ArgumentException ("fromBase is not valid.");
2614                         if (value == null)
2615                                 return 0;
2616
2617                         int chars = 0;
2618                         int digitValue = -1;
2619                         long result = 0;
2620                         bool negative = false;
2621
2622                         int i = 0;
2623                         int len = value.Length;
2624
2625                         // special processing for some bases
2626                         switch (fromBase) {
2627                                 case 10:
2628                                         if (value.Substring(i, 1) == "-") {
2629                                                 if (unsigned) {
2630                                                         throw new OverflowException (
2631                                                                 Locale.GetText ("The string was being parsed as"
2632                                                                 + " an unsigned number and could not have a"
2633                                                                 + " negative sign."));
2634                                                 }
2635                                                 negative = true;
2636                                                 i++;
2637                                         }
2638                                         break;
2639                                 case 16:
2640                                         if (value.Substring (i, 1) == "-") {
2641                                                 throw new ArgumentException ("String cannot contain a "
2642                                                         + "minus sign if the base is not 10.");
2643                                         }
2644                                         if (len >= i + 2) {
2645                                                 // 0x00 or 0X00
2646                                                 if ((value[i] == '0') && ((value[i + 1] == 'x') || (value[i + 1] == 'X'))) {
2647                                                         i += 2;
2648                                                 }
2649                                         }
2650                                         break;
2651                                 default:
2652                                         if (value.Substring (i, 1) == "-") {
2653                                                 throw new ArgumentException ("String cannot contain a "
2654                                                         + "minus sign if the base is not 10.");
2655                                         }
2656                                         break;
2657                         }
2658
2659                         if (len == i) {
2660                                 throw new FormatException ("Could not find any parsable digits.");
2661                         }
2662
2663                         if (value[i] == '+') {
2664                                 i++;
2665                         }
2666
2667                         while (i < len) {
2668                                 char c = value[i++];
2669                                 if (Char.IsNumber (c)) {
2670                                         digitValue = c - '0';
2671                                 } else if (Char.IsLetter (c)) {
2672                                         digitValue = Char.ToLowerInvariant (c) - 'a' + 10;
2673                                 } else {
2674                                         if (chars > 0) {
2675                                                 throw new FormatException ("Additional unparsable "
2676                                                         + "characters are at the end of the string.");
2677                                         } else {
2678                                                 throw new FormatException ("Could not find any parsable"
2679                                                         + " digits.");
2680                                         }
2681                                 }
2682
2683                                 if (digitValue >= fromBase) {
2684                                         if (chars > 0) {
2685                                                 throw new FormatException ("Additional unparsable "
2686                                                         + "characters are at the end of the string.");
2687                                         } else {
2688                                                 throw new FormatException ("Could not find any parsable"
2689                                                         + " digits.");
2690                                         }
2691                                 }
2692
2693                                 result = (fromBase * result + digitValue);
2694                                 chars ++;
2695                         }
2696
2697                         if (chars == 0)
2698                                 throw new FormatException ("Could not find any parsable digits.");
2699
2700                         if (negative)
2701                                 return -1 * result;
2702                         else
2703                                 return result;
2704                 }
2705
2706                 private static void EndianSwap (ref byte[] value)
2707                 {
2708                         byte[] buf = new byte[value.Length];
2709                         for (int i = 0; i < value.Length; i++)
2710                                 buf[i] = value[value.Length-1-i];
2711                         value = buf;
2712                 }
2713
2714                 private static string ConvertToBase2 (byte[] value)
2715                 {
2716                         if (!BitConverter.IsLittleEndian)
2717                                 EndianSwap (ref value);
2718                         StringBuilder sb = new StringBuilder ();
2719                         for (int i = value.Length - 1; i >= 0; i--) {
2720                                 byte b = value [i];
2721                                 for (int j = 0; j < 8; j++) {
2722                                         if ((b & 0x80) == 0x80) {
2723                                                 sb.Append ('1');
2724                                         }
2725                                         else {
2726                                                 if (sb.Length > 0)
2727                                                         sb.Append ('0');
2728                                         }
2729                                         b <<= 1;
2730                                 }
2731                         }
2732                         return sb.ToString ();
2733                 }
2734
2735                 private static string ConvertToBase8 (byte[] value)
2736                 {
2737                         ulong l = 0;
2738                         switch (value.Length) {
2739                         case 1:
2740                                 l = (ulong) value [0];
2741                                 break;
2742                         case 2:
2743                                 l = (ulong) BitConverter.ToUInt16 (value, 0);
2744                                 break;
2745                         case 4:
2746                                 l = (ulong) BitConverter.ToUInt32 (value, 0);
2747                                 break;
2748                         case 8:
2749                                 l = BitConverter.ToUInt64 (value, 0);
2750                                 break;
2751                         default:
2752                                 throw new ArgumentException ("value");
2753                         }
2754
2755                         StringBuilder sb = new StringBuilder ();
2756                         for (int i = 21; i >= 0; i--) {
2757                                 // 3 bits at the time
2758                                 char val = (char) ((l >> i * 3) & 0x7);
2759                                 if ((val != 0) || (sb.Length > 0)) {
2760                                         val += '0';
2761                                         sb.Append (val);
2762                                 }
2763                         }
2764                         return sb.ToString ();
2765                 }
2766
2767                 private static string ConvertToBase16 (byte[] value)
2768                 {
2769                         if (!BitConverter.IsLittleEndian)
2770                                 EndianSwap (ref value);
2771                         StringBuilder sb = new StringBuilder ();
2772                         for (int i = value.Length - 1; i >= 0; i--) {
2773                                 char high = (char)((value[i] >> 4) & 0x0f);
2774                                 if ((high != 0) || (sb.Length > 0)) {
2775                                         if (high < 10) 
2776                                                 high += '0';
2777                                         else {
2778                                                 high -= (char) 10;
2779                                                 high += 'a';
2780                                         }
2781                                         sb.Append (high);
2782                                 }
2783
2784                                 char low = (char)(value[i] & 0x0f);
2785                                 if ((low != 0) || (sb.Length > 0)) {
2786                                         if (low < 10)
2787                                                 low += '0';
2788                                         else {
2789                                                 low -= (char) 10;
2790                                                 low += 'a';
2791                                         }
2792                                         sb.Append (low);
2793                                 }
2794                         }
2795                         return sb.ToString ();
2796                 }
2797
2798                 // Lookup table for the conversion ToType method. Order
2799                 // is important! Used by ToType for comparing the target
2800                 // type, and uses hardcoded array indexes.
2801                 private static readonly Type[] conversionTable = {
2802                         // Valid ICovnertible Types
2803                         null,               //  0 empty
2804                         typeof (object),   //  1 TypeCode.Object
2805                         typeof (DBNull),   //  2 TypeCode.DBNull
2806                         typeof (Boolean),  //  3 TypeCode.Boolean
2807                         typeof (Char),     //  4 TypeCode.Char
2808                         typeof (SByte),    //  5 TypeCode.SByte
2809                         typeof (Byte),     //  6 TypeCode.Byte
2810                         typeof (Int16),    //  7 TypeCode.Int16
2811                         typeof (UInt16),   //  8 TypeCode.UInt16
2812                         typeof (Int32),    //  9 TypeCode.Int32
2813                         typeof (UInt32),   // 10 TypeCode.UInt32
2814                         typeof (Int64),    // 11 TypeCode.Int64
2815                         typeof (UInt64),   // 12 TypeCode.UInt64
2816                         typeof (Single),   // 13 TypeCode.Single
2817                         typeof (Double),   // 14 TypeCode.Double
2818                         typeof (Decimal),  // 15 TypeCode.Decimal
2819                         typeof (DateTime), // 16 TypeCode.DateTime
2820                         null,               // 17 null.
2821                         typeof (String),   // 18 TypeCode.String
2822                 };
2823
2824                 // Function to convert an object to another type and return
2825                 // it as an object. In place for the core data types to use
2826                 // when implementing IConvertible. Uses hardcoded indexes in 
2827                 // the conversionTypes array, so if modify carefully.
2828                 internal static object ToType (object value, Type conversionType, 
2829                                                IFormatProvider provider) 
2830                 {
2831                         if (value == null) {
2832                                 if ((conversionType != null) && conversionType.IsValueType){
2833 #if NET_2_0
2834                                         throw new InvalidCastException ("Null object can not be converted to a value type.");
2835 #else
2836                                         //
2837                                         // Bug compatibility with 1.0
2838                                         //
2839                                         throw new NullReferenceException ("Null object can not be converted to a value type.");
2840 #endif
2841                                 } else
2842                                         return null;
2843                         }
2844
2845                         if (conversionType == null)
2846                                 throw new InvalidCastException ("Cannot cast to destination type.");
2847
2848                         if (value.GetType () == conversionType)
2849                                 return value;
2850                         
2851                         if (value is IConvertible) {
2852                                 IConvertible convertValue = (IConvertible) value;
2853
2854                                 if (conversionType == conversionTable[0]) // 0 Empty
2855                                         throw new ArgumentNullException ();
2856                                 
2857                                 else if (conversionType == conversionTable[1]) // 1 TypeCode.Object
2858                                         return (object) value;
2859                                         
2860                                 else if (conversionType == conversionTable[2]) // 2 TypeCode.DBNull
2861                                         throw new InvalidCastException (
2862                                                 "Cannot cast to DBNull, it's not IConvertible");
2863                   
2864                                 else if (conversionType == conversionTable[3]) // 3 TypeCode.Boolean
2865                                         return (object) convertValue.ToBoolean (provider);
2866                                         
2867                                 else if (conversionType == conversionTable[4]) // 4 TypeCode.Char
2868                                         return (object) convertValue.ToChar (provider);
2869                   
2870                                 else if (conversionType == conversionTable[5]) // 5 TypeCode.SByte
2871                                         return (object) convertValue.ToSByte (provider);
2872
2873                                 else if (conversionType == conversionTable[6]) // 6 TypeCode.Byte
2874                                         return (object) convertValue.ToByte (provider);
2875                                 
2876                                 else if (conversionType == conversionTable[7]) // 7 TypeCode.Int16
2877                                         return (object) convertValue.ToInt16 (provider);
2878                                         
2879                                 else if (conversionType == conversionTable[8]) // 8 TypeCode.UInt16
2880                                         return (object) convertValue.ToUInt16 (provider);
2881                   
2882                                 else if (conversionType == conversionTable[9]) // 9 TypeCode.Int32
2883                                         return (object) convertValue.ToInt32 (provider);
2884                         
2885                                 else if (conversionType == conversionTable[10]) // 10 TypeCode.UInt32
2886                                         return (object) convertValue.ToUInt32 (provider);
2887                   
2888                                 else if (conversionType == conversionTable[11]) // 11 TypeCode.Int64
2889                                         return (object) convertValue.ToInt64 (provider);
2890                   
2891                                 else if (conversionType == conversionTable[12]) // 12 TypeCode.UInt64
2892                                         return (object) convertValue.ToUInt64 (provider);
2893                   
2894                                 else if (conversionType == conversionTable[13]) // 13 TypeCode.Single
2895                                         return (object) convertValue.ToSingle (provider);
2896                   
2897                                 else if (conversionType == conversionTable[14]) // 14 TypeCode.Double
2898                                         return (object) convertValue.ToDouble (provider);
2899
2900                                 else if (conversionType == conversionTable[15]) // 15 TypeCode.Decimal
2901                                         return (object) convertValue.ToDecimal (provider);
2902
2903                                 else if (conversionType == conversionTable[16]) // 16 TypeCode.DateTime
2904                                         return (object) convertValue.ToDateTime (provider);
2905                                 
2906                                 else if (conversionType == conversionTable[18]) // 18 TypeCode.String
2907                                         return (object) convertValue.ToString (provider);
2908                                 else {
2909                                         return convertValue.ToType (conversionType, provider);
2910                                 }
2911                         } else
2912                                 // Not in the conversion table
2913                                 throw new InvalidCastException ((Locale.GetText (
2914                                         "Value is not a convertible object: " + value.GetType().ToString() + " to " + conversionType.FullName)));
2915                 }
2916         }
2917 }