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