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