Bump corefx
[mono.git] / mcs / class / referencesource / mscorlib / system / convert.cs
1 // ==++==
2 // 
3 //   Copyright (c) Microsoft Corporation.  All rights reserved.
4 // 
5 // ==--==
6 /*============================================================
7 **
8 ** Class:   Convert
9 **
10 **
11 ** Purpose: Home for static conversion methods.
12 **
13 **
14 ===========================================================*/
15
16 using System;
17 using System.Globalization;
18 using System.Threading;
19 using System.Reflection;
20 using System.Runtime.CompilerServices;
21 using System.Runtime.InteropServices;
22 using System.Runtime.Versioning;
23 using System.Security;
24 using System.Diagnostics.Contracts;
25
26
27 namespace System {    
28
29     [Flags]
30     public enum Base64FormattingOptions {
31         None = 0,
32         InsertLineBreaks = 1
33     }
34
35     // Returns the type code of this object. An implementation of this method
36     // must not return TypeCode.Empty (which represents a null reference) or
37     // TypeCode.Object (which represents an object that doesn't implement the
38     // IConvertible interface). An implementation of this method should return
39     // TypeCode.DBNull if the value of this object is a database null. For
40     // example, a nullable integer type should return TypeCode.DBNull if the
41     // value of the object is the database null. Otherwise, an implementation
42     // of this method should return the TypeCode that best describes the
43     // internal representation of the object.
44     // The Value class provides conversion and querying methods for values. The
45     // Value class contains static members only, and it is not possible to create
46     // instances of the class.
47     //
48     // The statically typed conversion methods provided by the Value class are all
49     // of the form:
50     //
51     //    public static XXX ToXXX(YYY value)
52     //
53     // where XXX is the target type and YYY is the source type. The matrix below
54     // shows the set of supported conversions. The set of conversions is symmetric
55     // such that for every ToXXX(YYY) there is also a ToYYY(XXX).
56     //
57     // From:  To: Bol Chr SBy Byt I16 U16 I32 U32 I64 U64 Sgl Dbl Dec Dat Str
58     // ----------------------------------------------------------------------
59     // Boolean     x       x   x   x   x   x   x   x   x   x   x   x       x
60     // Char            x   x   x   x   x   x   x   x   x                   x
61     // SByte       x   x   x   x   x   x   x   x   x   x   x   x   x       x
62     // Byte        x   x   x   x   x   x   x   x   x   x   x   x   x       x
63     // Int16       x   x   x   x   x   x   x   x   x   x   x   x   x       x
64     // UInt16      x   x   x   x   x   x   x   x   x   x   x   x   x       x
65     // Int32       x   x   x   x   x   x   x   x   x   x   x   x   x       x
66     // UInt32      x   x   x   x   x   x   x   x   x   x   x   x   x       x
67     // Int64       x   x   x   x   x   x   x   x   x   x   x   x   x       x
68     // UInt64      x   x   x   x   x   x   x   x   x   x   x   x   x       x
69     // Single      x       x   x   x   x   x   x   x   x   x   x   x       x
70     // Double      x       x   x   x   x   x   x   x   x   x   x   x       x
71     // Decimal     x       x   x   x   x   x   x   x   x   x   x   x       x
72     // DateTime                                                        x   x
73     // String      x   x   x   x   x   x   x   x   x   x   x   x   x   x   x
74     // ----------------------------------------------------------------------
75     //
76     // For dynamic conversions, the Value class provides a set of methods of the
77     // form:
78     //
79     //    public static XXX ToXXX(object value)
80     //
81     // where XXX is the target type (Boolean, Char, SByte, Byte, Int16, UInt16,
82     // Int32, UInt32, Int64, UInt64, Single, Double, Decimal, DateTime,
83     // or String). The implementations of these methods all take the form:
84     //
85     //    public static XXX toXXX(object value) {
86     //        return value == null? XXX.Default: ((IConvertible)value).ToXXX();
87     //    }
88     //
89     // The code first checks if the given value is a null reference (which is the
90     // same as Value.Empty), in which case it returns the default value for type
91     // XXX. Otherwise, a cast to IConvertible is performed, and the appropriate ToXXX()
92     // method is invoked on the object. An InvalidCastException is thrown if the
93     // cast to IConvertible fails, and that exception is simply allowed to propagate out
94     // of the conversion method.
95         
96     // Constant representing the database null value. This value is used in
97     // database applications to indicate the absense of a known value. Note
98     // that Value.DBNull is NOT the same as a null object reference, which is
99     // represented by Value.Empty.
100     //
101     // The Equals() method of DBNull always returns false, even when the
102     // argument is itself DBNull.
103     //
104     // When passed Value.DBNull, the Value.GetTypeCode() method returns
105     // TypeCode.DBNull.
106     //
107     // When passed Value.DBNull, the Value.ToXXX() methods all throw an
108     // InvalidCastException.
109
110     public static class Convert {
111         
112         //A typeof operation is fairly expensive (does a system call), so we'll cache these here
113         //statically.  These are exactly lined up with the TypeCode, eg. ConvertType[TypeCode.Int16]
114         //will give you the type of an Int16.
115         internal static readonly RuntimeType[] ConvertTypes = {
116             (RuntimeType)typeof(System.Empty),
117             (RuntimeType)typeof(Object),
118             (RuntimeType)typeof(System.DBNull),
119             (RuntimeType)typeof(Boolean),
120             (RuntimeType)typeof(Char),
121             (RuntimeType)typeof(SByte),
122             (RuntimeType)typeof(Byte),
123             (RuntimeType)typeof(Int16),
124             (RuntimeType)typeof(UInt16),
125             (RuntimeType)typeof(Int32),
126             (RuntimeType)typeof(UInt32),
127             (RuntimeType)typeof(Int64),
128             (RuntimeType)typeof(UInt64),
129             (RuntimeType)typeof(Single),
130             (RuntimeType)typeof(Double),
131             (RuntimeType)typeof(Decimal),
132             (RuntimeType)typeof(DateTime),
133             (RuntimeType)typeof(Object), //TypeCode is discontinuous so we need a placeholder.
134             (RuntimeType)typeof(String)
135         };
136
137         // Need to special case Enum because typecode will be underlying type, e.g. Int32
138         private static readonly RuntimeType EnumType = (RuntimeType)typeof(Enum);
139
140         internal static readonly char[] base64Table = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O',
141                                                        'P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d',
142                                                        'e','f','g','h','i','j','k','l','m','n','o','p','q','r','s',
143                                                        't','u','v','w','x','y','z','0','1','2','3','4','5','6','7',
144                                                        '8','9','+','/','=' };        
145
146         private const Int32 base64LineBreakPosition = 76;       
147
148 #if _DEBUG
149         private static bool TriggerAsserts = DoAsserts();
150         private static bool DoAsserts() {
151             Contract.Assert(ConvertTypes!=null, "[Convert.cctor]ConvertTypes!=null");
152             Contract.Assert(ConvertTypes.Length == ((int)TypeCode.String + 1), "[Convert.cctor]ConvertTypes.Length == ((int)TypeCode.String + 1)");
153             Contract.Assert(ConvertTypes[(int)TypeCode.Empty]==typeof(System.Empty),
154                             "[Convert.cctor]ConvertTypes[(int)TypeCode.Empty]==typeof(System.Empty)");
155             Contract.Assert(ConvertTypes[(int)TypeCode.String]==typeof(String),
156                             "[Convert.cctor]ConvertTypes[(int)TypeCode.String]==typeof(System.String)");
157             Contract.Assert(ConvertTypes[(int)TypeCode.Int32]==typeof(int),
158                             "[Convert.cctor]ConvertTypes[(int)TypeCode.Int32]==typeof(int)");
159             return true;
160         }
161 #endif
162
163         public static readonly Object DBNull = System.DBNull.Value;
164
165         // Returns the type code for the given object. If the argument is null,
166         // the result is TypeCode.Empty. If the argument is not a value (i.e. if
167         // the object does not implement IConvertible), the result is TypeCode.Object.
168         // Otherwise, the result is the type code of the object, as determined by
169         // the object's implementation of IConvertible.
170         [Pure]
171         public static TypeCode GetTypeCode(object value) {
172             if (value == null) return TypeCode.Empty;
173             IConvertible temp = value as IConvertible;
174             if (temp != null)
175             {
176                 return temp.GetTypeCode();
177             }
178             return TypeCode.Object;
179         }
180
181         // Returns true if the given object is a database null. This operation
182         // corresponds to "value.GetTypeCode() == TypeCode.DBNull".
183         [Pure]
184         public static bool IsDBNull(object value) {
185             if (value == System.DBNull.Value) return true;
186             IConvertible convertible = value as IConvertible;
187             return convertible != null? convertible.GetTypeCode() == TypeCode.DBNull: false;
188         }
189
190         // Converts the given object to the given type. In general, this method is
191         // equivalent to calling the Value.ToXXX(value) method for the given
192         // typeCode and boxing the result.
193         //
194         // The method first checks if the given object implements IConvertible. If not,
195         // the only permitted conversion is from a null to TypeCode.Empty, the
196         // result of which is null.
197         //
198         // If the object does implement IConvertible, a check is made to see if the
199         // object already has the given type code, in which case the object is
200         // simply returned. Otherwise, the appropriate ToXXX() is invoked on the
201         // object's implementation of IConvertible.
202         public static Object ChangeType(Object value, TypeCode typeCode) {
203             return ChangeType(value, typeCode, Thread.CurrentThread.CurrentCulture);
204         }
205
206         public static Object ChangeType(Object value, TypeCode typeCode, IFormatProvider provider) {
207             if (value == null && (typeCode == TypeCode.Empty || typeCode == TypeCode.String || typeCode == TypeCode.Object)) {
208                 return null;
209             }
210         
211             IConvertible v = value as IConvertible;
212             if (v == null) {
213                 throw new InvalidCastException(Environment.GetResourceString("InvalidCast_IConvertible"));
214             }
215
216             // This line is invalid for things like Enums that return a TypeCode
217             // of Int32, but the object can't actually be cast to an Int32.
218             //            if (v.GetTypeCode() == typeCode) return value;
219             switch (typeCode) {
220             case TypeCode.Boolean:
221                 return v.ToBoolean(provider);
222             case TypeCode.Char:
223                 return v.ToChar(provider);
224             case TypeCode.SByte:
225                 return v.ToSByte(provider);
226             case TypeCode.Byte:
227                 return v.ToByte(provider);
228             case TypeCode.Int16:
229                 return v.ToInt16(provider);
230             case TypeCode.UInt16:
231                 return v.ToUInt16(provider);
232             case TypeCode.Int32:
233                 return v.ToInt32(provider);
234             case TypeCode.UInt32:
235                 return v.ToUInt32(provider);
236             case TypeCode.Int64:
237                 return v.ToInt64(provider);
238             case TypeCode.UInt64:
239                 return v.ToUInt64(provider);
240             case TypeCode.Single:
241                 return v.ToSingle(provider);
242             case TypeCode.Double:
243                 return v.ToDouble(provider);
244             case TypeCode.Decimal:
245                 return v.ToDecimal(provider);
246             case TypeCode.DateTime:
247                 return v.ToDateTime(provider);
248             case TypeCode.String:
249                 return v.ToString(provider);
250             case TypeCode.Object:
251                 return value;
252             case TypeCode.DBNull:
253                 throw new InvalidCastException(Environment.GetResourceString("InvalidCast_DBNull"));
254             case TypeCode.Empty:
255                 throw new InvalidCastException(Environment.GetResourceString("InvalidCast_Empty"));
256             default:
257                 throw new ArgumentException(Environment.GetResourceString("Arg_UnknownTypeCode"));
258             }
259         }
260
261         internal static Object DefaultToType(IConvertible value, Type targetType, IFormatProvider provider) {
262             Contract.Requires(value != null, "[Convert.DefaultToType]value!=null");
263             if (targetType==null) {
264                 throw new ArgumentNullException("targetType");
265             }
266             Contract.EndContractBlock();
267
268             RuntimeType rtTargetType = targetType as RuntimeType;
269
270             if (rtTargetType != null)
271             {
272                 if (value.GetType() == targetType)
273                 {
274                     return value;
275                 }
276
277                 if (rtTargetType == ConvertTypes[(int)TypeCode.Boolean])
278                     return value.ToBoolean(provider);
279                 if (rtTargetType == ConvertTypes[(int)TypeCode.Char])
280                     return value.ToChar(provider);
281                 if (rtTargetType == ConvertTypes[(int)TypeCode.SByte])
282                     return value.ToSByte(provider);
283                 if (rtTargetType == ConvertTypes[(int)TypeCode.Byte])
284                     return value.ToByte(provider);
285                 if (rtTargetType == ConvertTypes[(int)TypeCode.Int16])
286                     return value.ToInt16(provider);
287                 if (rtTargetType == ConvertTypes[(int)TypeCode.UInt16])
288                     return value.ToUInt16(provider);
289                 if (rtTargetType == ConvertTypes[(int)TypeCode.Int32])
290                     return value.ToInt32(provider);
291                 if (rtTargetType == ConvertTypes[(int)TypeCode.UInt32])
292                     return value.ToUInt32(provider);
293                 if (rtTargetType == ConvertTypes[(int)TypeCode.Int64])
294                     return value.ToInt64(provider);
295                 if (rtTargetType == ConvertTypes[(int)TypeCode.UInt64])
296                     return value.ToUInt64(provider);
297                 if (rtTargetType == ConvertTypes[(int)TypeCode.Single])
298                     return value.ToSingle(provider);
299                 if (rtTargetType == ConvertTypes[(int)TypeCode.Double])
300                     return value.ToDouble(provider);
301                 if (rtTargetType == ConvertTypes[(int)TypeCode.Decimal])
302                     return value.ToDecimal(provider);
303                 if (rtTargetType == ConvertTypes[(int)TypeCode.DateTime])
304                     return value.ToDateTime(provider);
305                 if (rtTargetType == ConvertTypes[(int)TypeCode.String])
306                     return value.ToString(provider);
307                 if (rtTargetType == ConvertTypes[(int)TypeCode.Object])
308                     return (Object)value;
309                 //  Need to special case Enum because typecode will be underlying type, e.g. Int32
310                 if (rtTargetType == EnumType)
311                     return (Enum)value;
312                 if (rtTargetType == ConvertTypes[(int)TypeCode.DBNull])
313                     throw new InvalidCastException(Environment.GetResourceString("InvalidCast_DBNull"));
314                 if (rtTargetType == ConvertTypes[(int)TypeCode.Empty])
315                     throw new InvalidCastException(Environment.GetResourceString("InvalidCast_Empty"));
316             }
317
318             throw new InvalidCastException(Environment.GetResourceString("InvalidCast_FromTo", value.GetType().FullName, targetType.FullName));
319         }
320
321         public static Object ChangeType(Object value, Type conversionType) {
322             return ChangeType(value, conversionType, Thread.CurrentThread.CurrentCulture);
323         }
324
325         public static Object ChangeType(Object value, Type conversionType, IFormatProvider provider) {
326             if( conversionType == null) {
327                 throw new ArgumentNullException("conversionType");
328             }
329             Contract.EndContractBlock();
330
331             if( value == null ) {
332                 if(conversionType.IsValueType) {
333                     throw new InvalidCastException(Environment.GetResourceString("InvalidCast_CannotCastNullToValueType"));
334                 }
335                 return null; 
336             }
337
338             IConvertible ic = value as IConvertible;
339             if (ic == null) {
340                 if ( value.GetType() == conversionType) {
341                     return value;
342                 }
343                 throw new InvalidCastException(Environment.GetResourceString("InvalidCast_IConvertible"));
344             }
345
346             RuntimeType rtConversionType = conversionType as RuntimeType;
347
348             if (rtConversionType==ConvertTypes[(int)TypeCode.Boolean])
349                 return ic.ToBoolean(provider);
350             if (rtConversionType==ConvertTypes[(int)TypeCode.Char])
351                 return ic.ToChar(provider);
352             if (rtConversionType==ConvertTypes[(int)TypeCode.SByte])
353                 return ic.ToSByte(provider);
354             if (rtConversionType==ConvertTypes[(int)TypeCode.Byte])
355                 return ic.ToByte(provider);
356             if (rtConversionType==ConvertTypes[(int)TypeCode.Int16]) 
357                 return ic.ToInt16(provider);
358             if (rtConversionType==ConvertTypes[(int)TypeCode.UInt16])
359                 return ic.ToUInt16(provider);
360             if (rtConversionType==ConvertTypes[(int)TypeCode.Int32])
361                 return ic.ToInt32(provider);
362             if (rtConversionType==ConvertTypes[(int)TypeCode.UInt32])
363                 return ic.ToUInt32(provider);
364             if (rtConversionType==ConvertTypes[(int)TypeCode.Int64])
365                 return ic.ToInt64(provider);
366             if (rtConversionType==ConvertTypes[(int)TypeCode.UInt64])
367                 return ic.ToUInt64(provider);
368             if (rtConversionType==ConvertTypes[(int)TypeCode.Single])
369                 return ic.ToSingle(provider);
370             if (rtConversionType==ConvertTypes[(int)TypeCode.Double])
371                 return ic.ToDouble(provider);
372             if (rtConversionType==ConvertTypes[(int)TypeCode.Decimal])
373                 return ic.ToDecimal(provider);
374             if (rtConversionType==ConvertTypes[(int)TypeCode.DateTime])
375                 return ic.ToDateTime(provider);
376             if (rtConversionType==ConvertTypes[(int)TypeCode.String])
377                 return ic.ToString(provider);
378             if (rtConversionType==ConvertTypes[(int)TypeCode.Object])
379                 return (Object)value;
380
381             return ic.ToType(conversionType, provider);
382         }
383
384         // Conversions to Boolean
385         public static bool ToBoolean(Object value) {
386             return value == null? false: ((IConvertible)value).ToBoolean(null);
387         }
388
389         public static bool ToBoolean(Object value, IFormatProvider provider) {
390             return value == null? false: ((IConvertible)value).ToBoolean(provider);
391         }
392
393
394         public static bool ToBoolean(bool value) {
395             return value;
396         }
397
398         [CLSCompliant(false)]
399         public static bool ToBoolean(sbyte value) {
400             return value != 0;
401         }
402
403         // To be consistent with IConvertible in the base data types else we get different semantics
404         // with widening operations. Without this operator this widen succeeds,with this API the widening throws.
405         public static bool ToBoolean(char value) {
406             return ((IConvertible)value).ToBoolean(null);
407         }
408
409         public static bool ToBoolean(byte value) {
410             return value != 0;
411         }
412
413
414         public static bool ToBoolean(short value) {
415             return value != 0;
416         }
417
418         [CLSCompliant(false)]   
419         public static bool ToBoolean(ushort value) {
420             return value != 0;
421         }
422
423         public static bool ToBoolean(int value) {
424             return value != 0;
425         }
426
427         [CLSCompliant(false)]   
428         public static bool ToBoolean(uint value) {
429             return value != 0;
430         }
431
432         public static bool ToBoolean(long value) {
433             return value != 0;
434         }
435
436         [CLSCompliant(false)]   
437         public static bool ToBoolean(ulong value) {
438             return value != 0;
439         }
440
441         public static bool ToBoolean(String value) {
442             if (value == null)
443                 return false;
444             return Boolean.Parse(value);
445         }
446
447         public static bool ToBoolean(String value, IFormatProvider provider) {
448             if (value == null)
449                 return false;
450             return Boolean.Parse(value);
451         }
452
453         public static bool ToBoolean(float value)
454         {
455             return value != 0;
456         }
457         
458         public static bool ToBoolean(double value)
459         {
460             return value != 0;
461         }
462
463         public static bool ToBoolean(decimal value)
464         {
465             return value != 0;
466         }
467
468         public static bool ToBoolean(DateTime value)
469         {
470             return ((IConvertible)value).ToBoolean(null);
471         }
472  
473         // Disallowed conversions to Boolean
474         // public static bool ToBoolean(TimeSpan value)
475
476         // Conversions to Char
477
478
479         public static char ToChar(object value) {
480             return value == null? (char)0: ((IConvertible)value).ToChar(null);
481         }
482
483         public static char ToChar(object value, IFormatProvider provider) {
484             return value == null? (char)0: ((IConvertible)value).ToChar(provider);
485         }
486
487         public static char ToChar(bool value) {
488             return ((IConvertible)value).ToChar(null);
489         }
490
491         public static char ToChar(char value) {
492             return value;
493         }
494
495         [CLSCompliant(false)]
496         public static char ToChar(sbyte value) {
497             if (value < 0) throw new OverflowException(Environment.GetResourceString("Overflow_Char"));
498             Contract.EndContractBlock();
499             return (char)value;
500         }
501
502         public static char ToChar(byte value) {
503             return (char)value;
504         }
505
506         public static char ToChar(short value) {
507             if (value < 0) throw new OverflowException(Environment.GetResourceString("Overflow_Char"));
508             Contract.EndContractBlock();
509             return (char)value;
510         }
511
512         [CLSCompliant(false)]   
513         public static char ToChar(ushort value) {
514             return (char)value;
515         }
516
517         public static char ToChar(int value) {
518             if (value < 0 || value > Char.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_Char"));
519             Contract.EndContractBlock();
520             return (char)value;
521         }
522
523         [CLSCompliant(false)]   
524         public static char ToChar(uint value) {
525             if (value > Char.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_Char"));
526             Contract.EndContractBlock();
527             return (char)value;
528         }
529
530         public static char ToChar(long value) {
531             if (value < 0 || value > Char.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_Char"));
532             Contract.EndContractBlock();
533             return (char)value;
534         }
535
536         [CLSCompliant(false)]   
537         public static char ToChar(ulong value) {
538             if (value > Char.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_Char"));
539             Contract.EndContractBlock();
540             return (char)value;
541         }
542
543         //
544         // @VariantSwitch
545         // Remove FormatExceptions;
546         //
547         public static char ToChar(String value) {
548             return ToChar(value, null);
549         }
550
551         public static char ToChar(String value, IFormatProvider provider) {
552             if (value == null) 
553                 throw new ArgumentNullException("value");
554             Contract.EndContractBlock();
555          
556             if (value.Length != 1) 
557                 throw new FormatException(Environment.GetResourceString(ResId.Format_NeedSingleChar));
558
559             return value[0];
560         }
561
562         // To be consistent with IConvertible in the base data types else we get different semantics
563         // with widening operations. Without this operator this widen succeeds,with this API the widening throws.
564         public static char ToChar(float value)
565         {
566             return ((IConvertible)value).ToChar(null);
567         }
568
569         // To be consistent with IConvertible in the base data types else we get different semantics
570         // with widening operations. Without this operator this widen succeeds,with this API the widening throws.
571         public static char ToChar(double value)
572         {
573             return ((IConvertible)value).ToChar(null);
574         }
575
576         // To be consistent with IConvertible in the base data types else we get different semantics
577         // with widening operations. Without this operator this widen succeeds,with this API the widening throws.
578         public static char ToChar(decimal value)
579         {
580             return ((IConvertible)value).ToChar(null);
581         }
582
583         public static char ToChar(DateTime value)
584         {
585             return ((IConvertible)value).ToChar(null);
586         }
587
588
589         // Disallowed conversions to Char
590         // public static char ToChar(TimeSpan value)
591         
592         // Conversions to SByte
593
594         [CLSCompliant(false)]
595         public static sbyte ToSByte(object value) {
596             return value == null? (sbyte)0: ((IConvertible)value).ToSByte(null);
597         }
598
599         [CLSCompliant(false)]
600         public static sbyte ToSByte(object value, IFormatProvider provider) {
601             return value == null? (sbyte)0: ((IConvertible)value).ToSByte(provider);
602         }
603
604         [CLSCompliant(false)]
605         public static sbyte ToSByte(bool value) {
606             return value? (sbyte)Boolean.True: (sbyte)Boolean.False;
607         }
608
609         [CLSCompliant(false)]
610         public static sbyte ToSByte(sbyte value) {
611             return value;
612         }
613
614         [CLSCompliant(false)]
615         public static sbyte ToSByte(char value) {
616             if (value > SByte.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_SByte"));
617             Contract.EndContractBlock();
618             return (sbyte)value;
619         }
620
621         [CLSCompliant(false)]
622         public static sbyte ToSByte(byte value) {
623             if (value > SByte.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_SByte"));
624             Contract.EndContractBlock();
625             return (sbyte)value;
626         }
627
628         [CLSCompliant(false)]
629         public static sbyte ToSByte(short value) {
630             if (value < SByte.MinValue || value > SByte.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_SByte"));
631             Contract.EndContractBlock();
632             return (sbyte)value;
633         }
634
635         [CLSCompliant(false)]   
636         public static sbyte ToSByte(ushort value) {
637             if (value > SByte.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_SByte"));
638             Contract.EndContractBlock();
639             return (sbyte)value;
640         }
641
642         [CLSCompliant(false)]
643         public static sbyte ToSByte(int value) {
644             if (value < SByte.MinValue || value > SByte.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_SByte"));
645             Contract.EndContractBlock();
646             return (sbyte)value;
647         }
648
649         [CLSCompliant(false)]   
650         public static sbyte ToSByte(uint value) {
651             if (value > SByte.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_SByte"));
652             Contract.EndContractBlock();
653             return (sbyte)value;
654         }
655
656         [CLSCompliant(false)]
657         public static sbyte ToSByte(long value) {
658             if (value < SByte.MinValue || value > SByte.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_SByte"));
659             Contract.EndContractBlock();
660             return (sbyte)value;
661         }
662
663         [CLSCompliant(false)]   
664         public static sbyte ToSByte(ulong value) {
665             if (value > (ulong)SByte.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_SByte"));
666             Contract.EndContractBlock();
667             return (sbyte)value;
668         }
669
670         [CLSCompliant(false)]
671         public static sbyte ToSByte(float value) {
672             return ToSByte((double)value);
673         }
674
675         [CLSCompliant(false)]
676         public static sbyte ToSByte(double value) {
677             return ToSByte(ToInt32(value));
678         }
679
680         [CLSCompliant(false)]
681         public static sbyte ToSByte(decimal value) {
682             return Decimal.ToSByte(Decimal.Round(value, 0));
683         }
684
685         [CLSCompliant(false)]
686         public static sbyte ToSByte(String value) {
687             if (value == null)
688                 return 0;
689             return SByte.Parse(value, CultureInfo.CurrentCulture);
690         }
691
692         [CLSCompliant(false)]
693         public static sbyte ToSByte(String value, IFormatProvider provider) {
694             return SByte.Parse(value, NumberStyles.Integer, provider);
695         }
696
697         [CLSCompliant(false)]
698         public static sbyte ToSByte(DateTime value)
699         {
700             return ((IConvertible)value).ToSByte(null);
701         }
702
703         // Disallowed conversions to SByte
704         // public static sbyte ToSByte(TimeSpan value)
705
706         // Conversions to Byte
707
708         public static byte ToByte(object value) {
709             return value == null? (byte)0: ((IConvertible)value).ToByte(null);
710         }
711
712         public static byte ToByte(object value, IFormatProvider provider) {
713             return value == null? (byte)0: ((IConvertible)value).ToByte(provider);
714         }
715
716         public static byte ToByte(bool value) {
717             return value? (byte)Boolean.True: (byte)Boolean.False;
718         }
719
720         public static byte ToByte(byte value) {
721             return value;
722         }
723
724         public static byte ToByte(char value) {
725             if (value > Byte.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_Byte"));
726             Contract.EndContractBlock();
727             return (byte)value;
728         }
729
730         [CLSCompliant(false)]
731         public static byte ToByte(sbyte value) {
732             if (value < Byte.MinValue) throw new OverflowException(Environment.GetResourceString("Overflow_Byte"));
733             Contract.EndContractBlock();
734             return (byte)value;
735         }
736
737         public static byte ToByte(short value) {
738             if (value < Byte.MinValue || value > Byte.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_Byte"));
739             Contract.EndContractBlock();
740             return (byte)value;
741         }
742
743         [CLSCompliant(false)]   
744         public static byte ToByte(ushort value) {
745             if (value > Byte.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_Byte"));
746             Contract.EndContractBlock();
747             return (byte)value;
748         }
749
750         public static byte ToByte(int value) {
751             if (value < Byte.MinValue || value > Byte.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_Byte"));
752             Contract.EndContractBlock();
753             return (byte)value;
754         }
755
756         [CLSCompliant(false)]   
757         public static byte ToByte(uint value) {
758             if (value > Byte.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_Byte"));
759             Contract.EndContractBlock();
760             return (byte)value;
761         }
762
763         public static byte ToByte(long value) {
764             if (value < Byte.MinValue || value > Byte.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_Byte"));
765             Contract.EndContractBlock();
766             return (byte)value;
767         }
768
769         [CLSCompliant(false)]   
770         public static byte ToByte(ulong value) {
771             if (value > Byte.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_Byte"));
772             Contract.EndContractBlock();
773             return (byte)value;
774         }
775
776         public static byte ToByte(float value) {
777             return ToByte((double)value);
778         }
779
780         public static byte ToByte(double value) {
781             return ToByte(ToInt32(value));
782         }
783
784         public static byte ToByte(decimal value) {
785             return Decimal.ToByte(Decimal.Round(value, 0));
786         }
787
788         public static byte ToByte(String value) {
789             if (value == null)
790                 return 0;
791             return Byte.Parse(value, CultureInfo.CurrentCulture);
792         }
793
794         public static byte ToByte(String value, IFormatProvider provider) {
795             if (value == null)
796                 return 0;
797             return Byte.Parse(value, NumberStyles.Integer, provider);
798         }
799
800         public static byte ToByte(DateTime value)
801         {
802             return ((IConvertible)value).ToByte(null);
803         }
804
805
806         // Disallowed conversions to Byte
807         // public static byte ToByte(TimeSpan value)
808
809         // Conversions to Int16
810
811         public static short ToInt16(object value) {
812             return value == null? (short)0: ((IConvertible)value).ToInt16(null);
813         }
814
815         public static short ToInt16(object value, IFormatProvider provider) {
816             return value == null? (short)0: ((IConvertible)value).ToInt16(provider);
817         }
818
819         public static short ToInt16(bool value) {
820             return value? (short)Boolean.True: (short)Boolean.False;
821         }
822
823         public static short ToInt16(char value) {
824             if (value > Int16.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_Int16"));
825             Contract.EndContractBlock();
826             return (short)value;
827         }
828
829         [CLSCompliant(false)]
830         public static short ToInt16(sbyte value) {
831             return value;
832         }
833
834         public static short ToInt16(byte value) {
835             return value;
836         }
837
838         [CLSCompliant(false)]   
839         public static short ToInt16(ushort value) {
840             if (value > Int16.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_Int16"));
841             Contract.EndContractBlock();
842             return (short)value;
843         }
844
845         public static short ToInt16(int value) {
846             if (value < Int16.MinValue || value > Int16.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_Int16"));
847             Contract.EndContractBlock();
848             return (short)value;
849         }
850
851         [CLSCompliant(false)]   
852         public static short ToInt16(uint value) {
853             if (value > Int16.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_Int16"));
854             Contract.EndContractBlock();
855             return (short)value;
856         }
857
858         public static short ToInt16(short value) {
859             return value;
860         }
861
862         public static short ToInt16(long value) {
863             if (value < Int16.MinValue || value > Int16.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_Int16"));
864             Contract.EndContractBlock();
865             return (short)value;
866         }
867
868         [CLSCompliant(false)]   
869         public static short ToInt16(ulong value) {
870             if (value > (ulong)Int16.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_Int16"));
871             Contract.EndContractBlock();
872             return (short)value;
873         }
874
875         public static short ToInt16(float value) {
876             return ToInt16((double)value);
877         }
878
879         public static short ToInt16(double value) {
880             return ToInt16(ToInt32(value));
881         }
882
883         public static short ToInt16(decimal value) {
884             return Decimal.ToInt16(Decimal.Round(value, 0));
885         }
886
887         public static short ToInt16(String value) {
888             if (value == null)
889                 return 0;
890             return Int16.Parse(value, CultureInfo.CurrentCulture);
891         }
892
893         public static short ToInt16(String value, IFormatProvider provider) {
894             if (value == null)
895                 return 0;
896             return Int16.Parse(value, NumberStyles.Integer, provider);
897         }
898
899         public static short ToInt16(DateTime value)
900         {
901             return ((IConvertible)value).ToInt16(null);
902         }
903
904
905         // Disallowed conversions to Int16
906         // public static short ToInt16(TimeSpan value)
907
908         // Conversions to UInt16
909
910         [CLSCompliant(false)]   
911         public static ushort ToUInt16(object value) {
912             return value == null? (ushort)0: ((IConvertible)value).ToUInt16(null);
913         }
914
915         [CLSCompliant(false)]   
916         public static ushort ToUInt16(object value, IFormatProvider provider) {
917             return value == null? (ushort)0: ((IConvertible)value).ToUInt16(provider);
918         }
919
920
921         [CLSCompliant(false)]   
922         public static ushort ToUInt16(bool value) {
923             return value? (ushort)Boolean.True: (ushort)Boolean.False;
924         }
925
926         [CLSCompliant(false)]   
927         public static ushort ToUInt16(char value) {
928             return value;
929         }
930
931         [CLSCompliant(false)]   
932         public static ushort ToUInt16(sbyte value) {
933             if (value < 0) throw new OverflowException(Environment.GetResourceString("Overflow_UInt16"));
934             Contract.EndContractBlock();
935             return (ushort)value;
936         }
937
938         [CLSCompliant(false)]   
939         public static ushort ToUInt16(byte value) {
940             return value;
941         }
942
943         [CLSCompliant(false)]   
944         public static ushort ToUInt16(short value) {
945             if (value < 0) throw new OverflowException(Environment.GetResourceString("Overflow_UInt16"));
946             Contract.EndContractBlock();
947             return (ushort)value;
948         }
949
950         [CLSCompliant(false)]   
951         public static ushort ToUInt16(int value) {
952             if (value < 0 || value > UInt16.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_UInt16"));
953             Contract.EndContractBlock();
954             return (ushort)value;
955         }
956
957         [CLSCompliant(false)] 
958         public static ushort ToUInt16(ushort value) {
959             return value;
960         }
961
962         [CLSCompliant(false)]   
963         public static ushort ToUInt16(uint value) {
964             if (value > UInt16.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_UInt16"));
965             Contract.EndContractBlock();
966             return (ushort)value;
967         }
968
969         
970         [CLSCompliant(false)]   
971         public static ushort ToUInt16(long value) {
972             if (value < 0 || value > UInt16.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_UInt16"));
973             Contract.EndContractBlock();
974             return (ushort)value;
975         }
976
977         [CLSCompliant(false)]   
978         public static ushort ToUInt16(ulong value) {
979             if (value > UInt16.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_UInt16"));
980             Contract.EndContractBlock();
981             return (ushort)value;
982         }
983
984         [CLSCompliant(false)]   
985         public static ushort ToUInt16(float value) {
986             return ToUInt16((double)value);
987         }
988
989         [CLSCompliant(false)]   
990         public static ushort ToUInt16(double value) {
991             return ToUInt16(ToInt32(value));
992         }
993
994         [CLSCompliant(false)]   
995         public static ushort ToUInt16(decimal value) {
996             return Decimal.ToUInt16(Decimal.Round(value, 0));
997         }
998
999         [CLSCompliant(false)]   
1000         public static ushort ToUInt16(String value) {
1001             if (value == null)
1002                 return 0;
1003             return UInt16.Parse(value, CultureInfo.CurrentCulture);
1004         }
1005
1006         [CLSCompliant(false)]
1007         public static ushort ToUInt16(String value, IFormatProvider provider) {
1008             if (value == null)
1009                 return 0;
1010             return UInt16.Parse(value, NumberStyles.Integer, provider);
1011         }
1012
1013         [CLSCompliant(false)]
1014         public static ushort ToUInt16(DateTime value)
1015         {
1016             return ((IConvertible)value).ToUInt16(null);
1017         }
1018
1019         // Disallowed conversions to UInt16
1020         // public static ushort ToUInt16(TimeSpan value)
1021
1022         // Conversions to Int32
1023
1024         public static int ToInt32(object value) {
1025             return value == null? 0: ((IConvertible)value).ToInt32(null);
1026         }
1027
1028         public static int ToInt32(object value, IFormatProvider provider) {
1029             return value == null? 0: ((IConvertible)value).ToInt32(provider);
1030         }
1031
1032
1033         public static int ToInt32(bool value) {
1034             return value? Boolean.True: Boolean.False;
1035         }
1036
1037         public static int ToInt32(char value) {
1038             return value;
1039         }
1040
1041         [CLSCompliant(false)]
1042         public static int ToInt32(sbyte value) {
1043             return value;
1044         }
1045
1046         public static int ToInt32(byte value) {
1047             return value;
1048         }
1049
1050         public static int ToInt32(short value) {
1051             return value;
1052         }
1053
1054         [CLSCompliant(false)]   
1055         public static int ToInt32(ushort value) {
1056             return value;
1057         }
1058
1059         [CLSCompliant(false)]   
1060         public static int ToInt32(uint value) {
1061             if (value > Int32.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_Int32"));
1062             Contract.EndContractBlock();
1063             return (int)value;
1064         }
1065
1066         public static int ToInt32(int value) {
1067             return value;
1068         }
1069
1070         public static int ToInt32(long value) {
1071             if (value < Int32.MinValue || value > Int32.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_Int32"));
1072             Contract.EndContractBlock();
1073             return (int)value;
1074         }
1075
1076         [CLSCompliant(false)]   
1077         public static int ToInt32(ulong value) {
1078             if (value > Int32.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_Int32"));
1079             Contract.EndContractBlock();
1080             return (int)value;
1081         }
1082
1083         public static int ToInt32(float value) {
1084             return ToInt32((double)value);
1085         }
1086
1087         public static int ToInt32(double value) {
1088             if (value >= 0) {
1089                 if (value < 2147483647.5) {
1090                     int result = (int)value;
1091                     double dif = value - result;
1092                     if (dif > 0.5 || dif == 0.5 && (result & 1) != 0) result++;
1093                     return result;
1094                 }
1095             }
1096             else {
1097                 if (value >= -2147483648.5) {
1098                     int result = (int)value;
1099                     double dif = value - result;
1100                     if (dif < -0.5 || dif == -0.5 && (result & 1) != 0) result--;
1101                     return result;
1102                 }
1103             }
1104             throw new OverflowException(Environment.GetResourceString("Overflow_Int32"));
1105         }
1106
1107         [System.Security.SecuritySafeCritical]  // auto-generated
1108         public static int ToInt32(decimal value) {
1109             return Decimal.FCallToInt32(value);
1110         }
1111
1112         public static int ToInt32(String value) {
1113             if (value == null)
1114                 return 0;
1115             return Int32.Parse(value, CultureInfo.CurrentCulture);
1116         }
1117
1118         public static int ToInt32(String value, IFormatProvider provider) {
1119             if (value == null)
1120                 return 0;
1121             return Int32.Parse(value, NumberStyles.Integer, provider);
1122         }
1123
1124         public static int ToInt32(DateTime value)
1125         {
1126             return ((IConvertible)value).ToInt32(null);
1127         }
1128
1129
1130         // Disallowed conversions to Int32
1131         // public static int ToInt32(TimeSpan value)
1132
1133         // Conversions to UInt32
1134
1135         [CLSCompliant(false)]   
1136         public static uint ToUInt32(object value) {
1137             return value == null? 0: ((IConvertible)value).ToUInt32(null);
1138         }
1139
1140         [CLSCompliant(false)]   
1141         public static uint ToUInt32(object value, IFormatProvider provider) {
1142             return value == null? 0: ((IConvertible)value).ToUInt32(provider);
1143         }
1144
1145
1146         [CLSCompliant(false)]   
1147         public static uint ToUInt32(bool value) {
1148             return value? (uint)Boolean.True: (uint)Boolean.False;
1149         }
1150
1151         [CLSCompliant(false)]   
1152         public static uint ToUInt32(char value) {
1153             return value;
1154         }
1155
1156         [CLSCompliant(false)]   
1157         public static uint ToUInt32(sbyte value) {
1158             if (value < 0) throw new OverflowException(Environment.GetResourceString("Overflow_UInt32"));
1159             Contract.EndContractBlock();
1160             return (uint)value;
1161         }
1162
1163         [CLSCompliant(false)]   
1164         public static uint ToUInt32(byte value) {
1165             return value;
1166         }
1167
1168         [CLSCompliant(false)]   
1169         public static uint ToUInt32(short value) {
1170             if (value < 0) throw new OverflowException(Environment.GetResourceString("Overflow_UInt32"));
1171             Contract.EndContractBlock();
1172             return (uint)value;
1173         }
1174
1175         [CLSCompliant(false)]   
1176         public static uint ToUInt32(ushort value) {
1177             return value;
1178         }
1179
1180         [CLSCompliant(false)]   
1181         public static uint ToUInt32(int value) {
1182             if (value < 0) throw new OverflowException(Environment.GetResourceString("Overflow_UInt32"));
1183             Contract.EndContractBlock();
1184             return (uint)value;
1185         }
1186
1187         [CLSCompliant(false)] 
1188         public static uint ToUInt32(uint value) {
1189             return value;
1190         }
1191
1192         [CLSCompliant(false)]   
1193         public static uint ToUInt32(long value) {
1194             if (value < 0 || value > UInt32.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_UInt32"));
1195             Contract.EndContractBlock();
1196             return (uint)value;
1197         }
1198
1199         [CLSCompliant(false)]   
1200         public static uint ToUInt32(ulong value) {
1201             if (value > UInt32.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_UInt32"));
1202             Contract.EndContractBlock();
1203             return (uint)value;
1204         }
1205
1206         [CLSCompliant(false)]   
1207         public static uint ToUInt32(float value) {
1208             return ToUInt32((double)value);
1209         }
1210
1211         [CLSCompliant(false)]   
1212         public static uint ToUInt32(double value) {
1213             if (value >= -0.5 && value < 4294967295.5) {
1214                 uint result = (uint)value;
1215                 double dif = value - result;
1216                 if (dif > 0.5 || dif == 0.5 && (result & 1) != 0) result++;
1217                 return result;
1218             }
1219             throw new OverflowException(Environment.GetResourceString("Overflow_UInt32"));
1220         }
1221
1222         [CLSCompliant(false)]   
1223         public static uint ToUInt32(decimal value) {
1224             return Decimal.ToUInt32(Decimal.Round(value, 0));
1225         }
1226
1227         [CLSCompliant(false)]   
1228         public static uint ToUInt32(String value) {
1229             if (value == null)
1230                 return 0;
1231             return UInt32.Parse(value, CultureInfo.CurrentCulture);
1232         }
1233
1234         [CLSCompliant(false)]
1235         public static uint ToUInt32(String value, IFormatProvider provider) {
1236             if (value == null)
1237                 return 0;
1238             return UInt32.Parse(value, NumberStyles.Integer, provider);
1239         }
1240
1241         [CLSCompliant(false)]
1242         public static uint ToUInt32(DateTime value)
1243         {
1244             return ((IConvertible)value).ToUInt32(null);
1245         }
1246
1247         // Disallowed conversions to UInt32
1248         // public static uint ToUInt32(TimeSpan value)
1249
1250         // Conversions to Int64
1251
1252         public static long ToInt64(object value) {
1253             return value == null? 0: ((IConvertible)value).ToInt64(null);
1254         }
1255
1256         public static long ToInt64(object value, IFormatProvider provider) {
1257             return value == null? 0: ((IConvertible)value).ToInt64(provider);
1258         }
1259
1260
1261         public static long ToInt64(bool value) {
1262             return value? Boolean.True: Boolean.False;
1263         }
1264
1265         public static long ToInt64(char value) {
1266             return value;
1267         }
1268
1269         [CLSCompliant(false)]
1270         public static long ToInt64(sbyte value) {
1271             return value;
1272         }
1273
1274         public static long ToInt64(byte value) {
1275             return value;
1276         }
1277
1278         public static long ToInt64(short value) {
1279             return value;
1280         }
1281
1282         [CLSCompliant(false)]   
1283         public static long ToInt64(ushort value) {
1284             return value;
1285         }
1286
1287         public static long ToInt64(int value) {
1288             return value;
1289         }
1290
1291         [CLSCompliant(false)]   
1292         public static long ToInt64(uint value) {
1293             return value;
1294         }
1295
1296         [CLSCompliant(false)]   
1297         public static long ToInt64(ulong value) {
1298             if (value > Int64.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_Int64"));
1299             Contract.EndContractBlock();
1300             return (long)value;
1301         }
1302
1303         public static long ToInt64(long value) {
1304             return value;
1305         }
1306
1307
1308         public static long ToInt64(float value) {
1309             return ToInt64((double)value);
1310         }
1311
1312         public static long ToInt64(double value) {
1313             return checked((long)Math.Round(value));
1314         }
1315
1316         public static long ToInt64(decimal value) {
1317             return Decimal.ToInt64(Decimal.Round(value, 0));
1318         }
1319
1320         public static long ToInt64(string value) {
1321             if (value == null)
1322                 return 0;
1323             return Int64.Parse(value, CultureInfo.CurrentCulture);
1324         }
1325
1326         public static long ToInt64(String value, IFormatProvider provider) {
1327             if (value == null)
1328                 return 0;
1329             return Int64.Parse(value, NumberStyles.Integer, provider);
1330         }
1331
1332         public static long ToInt64(DateTime value)
1333         {
1334             return ((IConvertible)value).ToInt64(null);
1335         }
1336
1337         // Disallowed conversions to Int64
1338         // public static long ToInt64(TimeSpan value)
1339
1340         // Conversions to UInt64
1341
1342         [CLSCompliant(false)]   
1343         public static ulong ToUInt64(object value) {
1344             return value == null? 0: ((IConvertible)value).ToUInt64(null);
1345         }
1346
1347         [CLSCompliant(false)]   
1348         public static ulong ToUInt64(object value, IFormatProvider provider) {
1349             return value == null? 0: ((IConvertible)value).ToUInt64(provider);
1350         }
1351
1352         [CLSCompliant(false)]   
1353         public static ulong ToUInt64(bool value) {
1354             return value? (ulong)Boolean.True: (ulong)Boolean.False;
1355         }
1356
1357         [CLSCompliant(false)]   
1358         public static ulong ToUInt64(char value) {
1359             return value;
1360         }
1361
1362     
1363         [CLSCompliant(false)]   
1364         public static ulong ToUInt64(sbyte value) {
1365             if (value < 0) throw new OverflowException(Environment.GetResourceString("Overflow_UInt64"));
1366             Contract.EndContractBlock();
1367             return (ulong)value;
1368         }
1369
1370         [CLSCompliant(false)]   
1371         public static ulong ToUInt64(byte value) {
1372             return value;
1373         }
1374
1375         [CLSCompliant(false)]   
1376         public static ulong ToUInt64(short value) {
1377             if (value < 0) throw new OverflowException(Environment.GetResourceString("Overflow_UInt64"));
1378             Contract.EndContractBlock();
1379             return (ulong)value;
1380         }
1381
1382         [CLSCompliant(false)]   
1383         public static ulong ToUInt64(ushort value) {
1384             return value;
1385         }
1386
1387         [CLSCompliant(false)]   
1388         public static ulong ToUInt64(int value) {
1389             if (value < 0) throw new OverflowException(Environment.GetResourceString("Overflow_UInt64"));
1390             Contract.EndContractBlock();
1391             return (ulong)value;
1392         }
1393
1394         [CLSCompliant(false)]   
1395         public static ulong ToUInt64(uint value) {
1396             return value;
1397         }
1398
1399         [CLSCompliant(false)]   
1400         public static ulong ToUInt64(long value) {
1401             if (value < 0) throw new OverflowException(Environment.GetResourceString("Overflow_UInt64"));
1402             Contract.EndContractBlock();
1403             return (ulong)value;
1404         }
1405
1406         [CLSCompliant(false)]   
1407         public static ulong ToUInt64(UInt64 value) {
1408             return value;
1409         }
1410
1411         [CLSCompliant(false)]   
1412         public static ulong ToUInt64(float value) {
1413             return ToUInt64((double)value);
1414         }
1415
1416         [CLSCompliant(false)]   
1417         public static ulong ToUInt64(double value) {
1418             return checked((ulong)Math.Round(value));
1419         }
1420
1421         [CLSCompliant(false)]   
1422         public static ulong ToUInt64(decimal value) {
1423             return Decimal.ToUInt64(Decimal.Round(value, 0));
1424         }
1425
1426         [CLSCompliant(false)]   
1427         public static ulong ToUInt64(String value) {
1428             if (value == null)
1429                 return 0;
1430             return UInt64.Parse(value, CultureInfo.CurrentCulture);
1431         }
1432
1433         [CLSCompliant(false)]
1434         public static ulong ToUInt64(String value, IFormatProvider provider) {
1435             if (value == null)
1436                 return 0;
1437             return UInt64.Parse(value, NumberStyles.Integer, provider);
1438         }
1439
1440         [CLSCompliant(false)]
1441         public static ulong ToUInt64(DateTime value)
1442         {
1443             return ((IConvertible)value).ToUInt64(null);
1444         }
1445
1446         // Disallowed conversions to UInt64
1447         // public static ulong ToUInt64(TimeSpan value)
1448
1449         // Conversions to Single
1450
1451         public static float ToSingle(object value) {
1452             return value == null? 0: ((IConvertible)value).ToSingle(null);
1453         }
1454
1455         public static float ToSingle(object value, IFormatProvider provider) {
1456             return value == null? 0: ((IConvertible)value).ToSingle(provider);
1457         }
1458
1459         [CLSCompliant(false)]
1460         public static float ToSingle(sbyte value) {
1461             return value;
1462         }
1463
1464         public static float ToSingle(byte value) {
1465             return value;
1466         }
1467
1468         public static float ToSingle(char value) {
1469             return ((IConvertible)value).ToSingle(null);
1470         }
1471
1472         public static float ToSingle(short value) {
1473             return value;
1474         }
1475
1476         [CLSCompliant(false)]   
1477         public static float ToSingle(ushort value) {
1478             return value;
1479         }
1480
1481         public static float ToSingle(int value) {
1482             return value;
1483         }
1484
1485         [CLSCompliant(false)]   
1486         public static float ToSingle(uint value) {
1487             return value;
1488         }
1489
1490         public static float ToSingle(long value) {
1491             return value;
1492         }
1493
1494         [CLSCompliant(false)]   
1495         public static float ToSingle(ulong value) {
1496             return value;
1497         }
1498
1499         public static float ToSingle(float value) {
1500             return value;
1501         }
1502
1503         public static float ToSingle(double value) {
1504             return (float)value;
1505         }
1506
1507         public static float ToSingle(decimal value) {
1508             return (float)value;
1509         }
1510
1511         public static float ToSingle(String value) {
1512             if (value == null)
1513                 return 0;
1514             return Single.Parse(value, CultureInfo.CurrentCulture);
1515         }
1516
1517         public static float ToSingle(String value, IFormatProvider provider) {
1518             if (value == null)
1519                 return 0;
1520             return Single.Parse(value, NumberStyles.Float | NumberStyles.AllowThousands, provider);
1521         }
1522
1523
1524         public static float ToSingle(bool value)
1525         {
1526             return value? Boolean.True: Boolean.False;
1527         }
1528
1529         public static float ToSingle(DateTime value)
1530         {
1531             return ((IConvertible)value).ToSingle(null);
1532         }
1533
1534         // Disallowed conversions to Single
1535         // public static float ToSingle(TimeSpan value)
1536
1537         // Conversions to Double
1538
1539         public static double ToDouble(object value) {
1540             return value == null? 0: ((IConvertible)value).ToDouble(null);
1541         }
1542
1543         public static double ToDouble(object value, IFormatProvider provider) {
1544             return value == null? 0: ((IConvertible)value).ToDouble(provider);
1545         }
1546
1547
1548         [CLSCompliant(false)]
1549         public static double ToDouble(sbyte value) {
1550             return value;
1551         }
1552
1553         public static double ToDouble(byte value) {
1554             return value;
1555         }
1556
1557         public static double ToDouble(short value) {
1558             return value;
1559         }
1560
1561         public static double ToDouble(char value) {
1562             return ((IConvertible)value).ToDouble(null);
1563         }
1564
1565         [CLSCompliant(false)]   
1566         public static double ToDouble(ushort value) {
1567             return value;
1568         }
1569
1570         public static double ToDouble(int value) {
1571             return value;
1572         }
1573
1574         [CLSCompliant(false)]   
1575         public static double ToDouble(uint value) {
1576             return value;
1577         }
1578
1579         public static double ToDouble(long value) {
1580             return value;
1581         }
1582
1583         [CLSCompliant(false)]   
1584         public static double ToDouble(ulong value) {
1585             return value;
1586         }
1587
1588         public static double ToDouble(float value) {
1589             return value;
1590         }
1591
1592         public static double ToDouble(double value) {
1593             return value;
1594         }
1595
1596         public static double ToDouble(decimal value) {
1597             return (double)value;
1598         }
1599
1600         public static double ToDouble(String value) {
1601             if (value == null)
1602                 return 0;
1603             return Double.Parse(value, CultureInfo.CurrentCulture);
1604         }
1605
1606         public static double ToDouble(String value, IFormatProvider provider) {
1607             if (value == null)
1608                 return 0;
1609             return Double.Parse(value, NumberStyles.Float | NumberStyles.AllowThousands, provider);
1610         }
1611
1612         public static double ToDouble(bool value) {
1613             return value? Boolean.True: Boolean.False;
1614         }
1615
1616         public static double ToDouble(DateTime value)
1617         {
1618             return ((IConvertible)value).ToDouble(null);
1619         }
1620
1621         // Disallowed conversions to Double
1622         // public static double ToDouble(TimeSpan value)
1623
1624         // Conversions to Decimal
1625
1626         public static decimal ToDecimal(object value) {
1627             return value == null? 0: ((IConvertible)value).ToDecimal(null);
1628         }
1629
1630         public static decimal ToDecimal(object value, IFormatProvider provider) {
1631             return value == null? 0: ((IConvertible)value).ToDecimal(provider);
1632         }
1633
1634         [CLSCompliant(false)]
1635         public static decimal ToDecimal(sbyte value) {
1636             return value;
1637         }
1638
1639         public static decimal ToDecimal(byte value) {
1640             return value;
1641         }
1642
1643         public static decimal ToDecimal(char value) {
1644             return ((IConvertible)value).ToDecimal(null);
1645         }
1646
1647         public static decimal ToDecimal(short value) {
1648             return value;
1649         }
1650
1651         [CLSCompliant(false)]   
1652         public static decimal ToDecimal(ushort value) {
1653             return value;
1654         }
1655
1656         public static decimal ToDecimal(int value) {
1657             return value;
1658         }
1659
1660         [CLSCompliant(false)]   
1661         public static decimal ToDecimal(uint value) {
1662             return value;
1663         }
1664
1665         public static decimal ToDecimal(long value) {
1666             return value;
1667         }
1668
1669         [CLSCompliant(false)]   
1670         public static decimal ToDecimal(ulong value) {
1671             return value;
1672         }
1673
1674         public static decimal ToDecimal(float value) {
1675             return (decimal)value;
1676         }
1677
1678         public static decimal ToDecimal(double value) {
1679             return (decimal)value;
1680         }
1681
1682         public static decimal ToDecimal(String value) {
1683             if (value == null)
1684                 return 0m;
1685             return Decimal.Parse(value, CultureInfo.CurrentCulture);
1686         }
1687
1688         public static Decimal ToDecimal(String value, IFormatProvider provider) {
1689             if (value == null)
1690                 return 0m;
1691             return Decimal.Parse(value, NumberStyles.Number, provider);
1692         }
1693
1694         public static decimal ToDecimal(decimal value) {
1695             return value;
1696         }
1697
1698         public static decimal ToDecimal(bool value) {
1699             return value? Boolean.True: Boolean.False;
1700         }
1701
1702         public static decimal ToDecimal(DateTime value)
1703         {
1704             return ((IConvertible)value).ToDecimal(null);
1705         }
1706
1707         // Disallowed conversions to Decimal
1708         // public static decimal ToDecimal(TimeSpan value)
1709
1710         // Conversions to DateTime
1711
1712         public static DateTime ToDateTime(DateTime value) {
1713             return value;
1714         }
1715
1716         public static DateTime ToDateTime(object value) {
1717             return value == null? DateTime.MinValue: ((IConvertible)value).ToDateTime(null);
1718         }
1719
1720         public static DateTime ToDateTime(object value, IFormatProvider provider) {
1721             return value == null? DateTime.MinValue: ((IConvertible)value).ToDateTime(provider);
1722         }
1723
1724         public static DateTime ToDateTime(String value) {
1725             if (value == null)
1726                 return new DateTime(0);
1727             return DateTime.Parse(value, CultureInfo.CurrentCulture);
1728         }
1729
1730         public static DateTime ToDateTime(String value, IFormatProvider provider) {
1731             if (value == null)
1732                 return new DateTime(0);
1733             return DateTime.Parse(value, provider);
1734         }
1735
1736          [CLSCompliant(false)]
1737         public static DateTime ToDateTime(sbyte value) {
1738             return ((IConvertible)value).ToDateTime(null);
1739         }
1740
1741         public static DateTime ToDateTime(byte value) {
1742             return ((IConvertible)value).ToDateTime(null);
1743         }
1744
1745         public static DateTime ToDateTime(short value) {
1746             return ((IConvertible)value).ToDateTime(null);
1747         }
1748
1749         [CLSCompliant(false)]
1750         public static DateTime ToDateTime(ushort value) {
1751             return ((IConvertible)value).ToDateTime(null);
1752         }
1753
1754         public static DateTime ToDateTime(int value) {
1755             return ((IConvertible)value).ToDateTime(null);
1756         }
1757
1758         [CLSCompliant(false)]
1759         public static DateTime ToDateTime(uint value) {
1760             return ((IConvertible)value).ToDateTime(null);
1761         }
1762
1763         public static DateTime ToDateTime(long value) {
1764             return ((IConvertible)value).ToDateTime(null);
1765         }
1766
1767         [CLSCompliant(false)]
1768         public static DateTime ToDateTime(ulong value) {
1769             return ((IConvertible)value).ToDateTime(null);
1770         }
1771
1772         public static DateTime ToDateTime(bool value) {
1773             return ((IConvertible)value).ToDateTime(null);
1774         }
1775
1776         public static DateTime ToDateTime(char value) {
1777             return ((IConvertible)value).ToDateTime(null);
1778         }
1779
1780         public static DateTime ToDateTime(float value) {
1781             return ((IConvertible)value).ToDateTime(null);
1782         }
1783
1784         public static DateTime ToDateTime(double value) {
1785             return ((IConvertible)value).ToDateTime(null);
1786         }
1787
1788         public static DateTime ToDateTime(decimal value) {
1789             return ((IConvertible)value).ToDateTime(null);
1790         }
1791
1792         // Disallowed conversions to DateTime
1793         // public static DateTime ToDateTime(TimeSpan value)
1794
1795         // Conversions to String
1796
1797         public static string ToString(Object value) {
1798             return ToString(value,null);
1799         }
1800
1801         public static string ToString(Object value, IFormatProvider provider) {
1802             IConvertible ic = value as IConvertible;
1803             if (ic != null) 
1804                 return ic.ToString(provider);
1805             IFormattable formattable = value as IFormattable;
1806             if (formattable != null) 
1807                 return formattable.ToString(null, provider);
1808             return value == null? String.Empty: value.ToString();
1809         }
1810
1811         public static string ToString(bool value) {
1812             Contract.Ensures(Contract.Result<string>() != null);
1813             return value.ToString();
1814         }
1815
1816         public static string ToString(bool value, IFormatProvider provider) {
1817             Contract.Ensures(Contract.Result<string>() != null);
1818             return value.ToString(provider);
1819         }
1820
1821         public static string ToString(char value) {
1822             Contract.Ensures(Contract.Result<string>() != null);
1823             return Char.ToString(value);
1824         }
1825
1826         public static string ToString(char value, IFormatProvider provider) {
1827             Contract.Ensures(Contract.Result<string>() != null);
1828             return value.ToString(provider);
1829         }
1830
1831         [CLSCompliant(false)]
1832         public static string ToString(sbyte value) {
1833             Contract.Ensures(Contract.Result<string>() != null);
1834             return value.ToString(CultureInfo.CurrentCulture);
1835         }
1836
1837         [CLSCompliant(false)]
1838         public static string ToString(sbyte value, IFormatProvider provider) {
1839             Contract.Ensures(Contract.Result<string>() != null);
1840             return value.ToString(provider);
1841         }
1842
1843         public static string ToString(byte value) {
1844             Contract.Ensures(Contract.Result<string>() != null);
1845             return value.ToString(CultureInfo.CurrentCulture);
1846         }
1847
1848         public static string ToString(byte value, IFormatProvider provider) {
1849             Contract.Ensures(Contract.Result<string>() != null);
1850             return value.ToString(provider);
1851         }
1852
1853         public static string ToString(short value) {
1854             Contract.Ensures(Contract.Result<string>() != null);
1855             return value.ToString(CultureInfo.CurrentCulture);
1856         }
1857
1858         public static string ToString(short value, IFormatProvider provider) {
1859             Contract.Ensures(Contract.Result<string>() != null);
1860             return value.ToString(provider);
1861         }
1862
1863         [CLSCompliant(false)]   
1864         public static string ToString(ushort value) {
1865             Contract.Ensures(Contract.Result<string>() != null);
1866             return value.ToString(CultureInfo.CurrentCulture);
1867         }
1868
1869         [CLSCompliant(false)]
1870         public static string ToString(ushort value, IFormatProvider provider) {
1871             Contract.Ensures(Contract.Result<string>() != null);
1872             return value.ToString(provider);
1873         }
1874
1875         public static string ToString(int value) {
1876             Contract.Ensures(Contract.Result<string>() != null);
1877             return value.ToString(CultureInfo.CurrentCulture);
1878         }
1879
1880         public static string ToString(int value, IFormatProvider provider) {
1881             Contract.Ensures(Contract.Result<string>() != null);
1882             return value.ToString(provider);
1883         }
1884
1885         [CLSCompliant(false)]   
1886         public static string ToString(uint value) {
1887             Contract.Ensures(Contract.Result<string>() != null);
1888             return value.ToString(CultureInfo.CurrentCulture);
1889         }
1890
1891         [CLSCompliant(false)]
1892         public static string ToString(uint value, IFormatProvider provider) {
1893             Contract.Ensures(Contract.Result<string>() != null);
1894             return value.ToString(provider);
1895         }
1896
1897         public static string ToString(long value) {
1898             Contract.Ensures(Contract.Result<string>() != null);
1899             return value.ToString(CultureInfo.CurrentCulture);
1900         }
1901
1902         public static string ToString(long value, IFormatProvider provider) {
1903             Contract.Ensures(Contract.Result<string>() != null);
1904             return value.ToString(provider);
1905         }
1906
1907         [CLSCompliant(false)]   
1908         public static string ToString(ulong value) {
1909             Contract.Ensures(Contract.Result<string>() != null);
1910             return value.ToString(CultureInfo.CurrentCulture);
1911         }
1912
1913         [CLSCompliant(false)]
1914         public static string ToString(ulong value, IFormatProvider provider) {
1915             Contract.Ensures(Contract.Result<string>() != null);
1916             return value.ToString(provider);
1917         }
1918
1919         public static string ToString(float value) {
1920             Contract.Ensures(Contract.Result<string>() != null);
1921             return value.ToString(CultureInfo.CurrentCulture);
1922         }
1923
1924         public static string ToString(float value, IFormatProvider provider) {
1925             Contract.Ensures(Contract.Result<string>() != null);
1926             return value.ToString(provider);
1927         }
1928
1929         public static string ToString(double value) {
1930             Contract.Ensures(Contract.Result<string>() != null);
1931             return value.ToString(CultureInfo.CurrentCulture);
1932         }
1933
1934         public static string ToString(double value, IFormatProvider provider) {
1935             Contract.Ensures(Contract.Result<string>() != null);
1936             return value.ToString(provider);
1937         }
1938
1939         public static string ToString(decimal value) {
1940             Contract.Ensures(Contract.Result<string>() != null);
1941             return value.ToString(CultureInfo.CurrentCulture);
1942         }
1943
1944         public static string ToString(Decimal value, IFormatProvider provider) {
1945             Contract.Ensures(Contract.Result<string>() != null);
1946             return value.ToString(provider);
1947         }
1948
1949         public static string ToString(DateTime value) {
1950             Contract.Ensures(Contract.Result<string>() != null);
1951             return value.ToString();
1952         }
1953
1954         public static string ToString(DateTime value, IFormatProvider provider) {
1955             Contract.Ensures(Contract.Result<string>() != null);
1956             return value.ToString(provider);
1957         }
1958
1959         public static String ToString(String value) {
1960             Contract.Ensures(Contract.Result<string>() == value);  // We were always skipping the null check here.
1961             return value;
1962         }
1963
1964         public static String ToString(String value,IFormatProvider provider) {
1965             Contract.Ensures(Contract.Result<string>() == value);  // We were always skipping the null check here.
1966             return value; // avoid the null check
1967         }
1968
1969
1970         //
1971         // Conversions which understand Base XXX numbers.
1972         //
1973         // Parses value in base base.  base can only
1974         // be 2, 8, 10, or 16.  If base is 16, the number may be preceded
1975         // by 0x; any other leading or trailing characters cause an error.
1976         //
1977         public static byte ToByte (String value, int fromBase) {
1978             if (fromBase!=2 && fromBase!=8 && fromBase!=10 && fromBase!=16) {
1979                 throw new ArgumentException(Environment.GetResourceString("Arg_InvalidBase"));
1980             }
1981             Contract.EndContractBlock();
1982             int r = ParseNumbers.StringToInt(value,fromBase,ParseNumbers.IsTight | ParseNumbers.TreatAsUnsigned);
1983             if (r < Byte.MinValue || r > Byte.MaxValue)
1984                 throw new OverflowException(Environment.GetResourceString("Overflow_Byte"));
1985             return (byte) r;
1986         }
1987
1988         // Parses value in base fromBase.  fromBase can only
1989         // be 2, 8, 10, or 16.  If fromBase is 16, the number may be preceded
1990         // by 0x; any other leading or trailing characters cause an error.
1991         //
1992         [CLSCompliant(false)]
1993         public static sbyte ToSByte (String value, int fromBase) {
1994             if (fromBase!=2 && fromBase!=8 && fromBase!=10 && fromBase!=16) {
1995                 throw new ArgumentException(Environment.GetResourceString("Arg_InvalidBase"));
1996             }
1997             Contract.EndContractBlock();
1998             int r = ParseNumbers.StringToInt(value,fromBase,ParseNumbers.IsTight | ParseNumbers.TreatAsI1);
1999             if (fromBase != 10 && r <= Byte.MaxValue)
2000                 return (sbyte)r;
2001
2002             if (r < SByte.MinValue || r > SByte.MaxValue)
2003                 throw new OverflowException(Environment.GetResourceString("Overflow_SByte"));
2004             return (sbyte) r;
2005         }
2006
2007         // Parses value in base fromBase.  fromBase can only
2008         // be 2, 8, 10, or 16.  If fromBase is 16, the number may be preceded
2009         // by 0x; any other leading or trailing characters cause an error.
2010         //
2011         public static short ToInt16 (String value, int fromBase) {
2012             if (fromBase!=2 && fromBase!=8 && fromBase!=10 && fromBase!=16) {
2013                 throw new ArgumentException(Environment.GetResourceString("Arg_InvalidBase"));
2014             }
2015             Contract.EndContractBlock();
2016             int r = ParseNumbers.StringToInt(value,fromBase,ParseNumbers.IsTight | ParseNumbers.TreatAsI2);
2017             if (fromBase != 10 && r <= UInt16.MaxValue)
2018                 return (short)r;
2019
2020             if (r < Int16.MinValue || r > Int16.MaxValue)
2021                 throw new OverflowException(Environment.GetResourceString("Overflow_Int16"));
2022             return (short) r;
2023         }
2024
2025         // Parses value in base fromBase.  fromBase can only
2026         // be 2, 8, 10, or 16.  If fromBase is 16, the number may be preceded
2027         // by 0x; any other leading or trailing characters cause an error.
2028         //
2029         [CLSCompliant(false)]
2030         public static ushort ToUInt16 (String value, int fromBase) {
2031             if (fromBase!=2 && fromBase!=8 && fromBase!=10 && fromBase!=16) {
2032                 throw new ArgumentException(Environment.GetResourceString("Arg_InvalidBase"));
2033             }
2034             Contract.EndContractBlock();
2035             int r = ParseNumbers.StringToInt(value,fromBase,ParseNumbers.IsTight | ParseNumbers.TreatAsUnsigned);
2036             if (r < UInt16.MinValue || r > UInt16.MaxValue)
2037                 throw new OverflowException(Environment.GetResourceString("Overflow_UInt16"));
2038             return (ushort) r;
2039         }
2040
2041         // Parses value in base fromBase.  fromBase can only
2042         // be 2, 8, 10, or 16.  If fromBase is 16, the number may be preceded
2043         // by 0x; any other leading or trailing characters cause an error.
2044         //
2045         public static int ToInt32 (String value, int fromBase) {
2046             if (fromBase!=2 && fromBase!=8 && fromBase!=10 && fromBase!=16) {
2047                 throw new ArgumentException(Environment.GetResourceString("Arg_InvalidBase"));
2048             }
2049             Contract.EndContractBlock();
2050             return ParseNumbers.StringToInt(value,fromBase,ParseNumbers.IsTight);
2051         }
2052
2053         // Parses value in base fromBase.  fromBase can only
2054         // be 2, 8, 10, or 16.  If fromBase is 16, the number may be preceded
2055         // by 0x; any other leading or trailing characters cause an error.
2056         //
2057         [CLSCompliant(false)]
2058         public static uint ToUInt32 (String value, int fromBase) {
2059             if (fromBase!=2 && fromBase!=8 && fromBase!=10 && fromBase!=16) {
2060                 throw new ArgumentException(Environment.GetResourceString("Arg_InvalidBase"));
2061             }
2062             Contract.EndContractBlock();
2063             return (uint) ParseNumbers.StringToInt(value,fromBase, ParseNumbers.TreatAsUnsigned | ParseNumbers.IsTight);
2064         }
2065
2066         // Parses value in base fromBase.  fromBase can only
2067         // be 2, 8, 10, or 16.  If fromBase is 16, the number may be preceded
2068         // by 0x; any other leading or trailing characters cause an error.
2069         //
2070         public static long ToInt64 (String value, int fromBase) {
2071             if (fromBase!=2 && fromBase!=8 && fromBase!=10 && fromBase!=16) {
2072                 throw new ArgumentException(Environment.GetResourceString("Arg_InvalidBase"));
2073             }
2074             Contract.EndContractBlock();
2075             return ParseNumbers.StringToLong(value,fromBase,ParseNumbers.IsTight);
2076         }
2077
2078         // Parses value in base fromBase.  fromBase can only
2079         // be 2, 8, 10, or 16.  If fromBase is 16, the number may be preceded
2080         // by 0x; any other leading or trailing characters cause an error.
2081         //
2082         [CLSCompliant(false)]
2083         public static ulong ToUInt64 (String value, int fromBase) {
2084             if (fromBase!=2 && fromBase!=8 && fromBase!=10 && fromBase!=16) {
2085                 throw new ArgumentException(Environment.GetResourceString("Arg_InvalidBase"));
2086             }
2087             Contract.EndContractBlock();
2088             return (ulong) ParseNumbers.StringToLong(value,fromBase, ParseNumbers.TreatAsUnsigned | ParseNumbers.IsTight);
2089         }
2090
2091         // Convert the byte value to a string in base fromBase
2092         [System.Security.SecuritySafeCritical]  // auto-generated
2093         public static String ToString (byte value, int toBase) {
2094             if (toBase!=2 && toBase!=8 && toBase!=10 && toBase!=16) {
2095                 throw new ArgumentException(Environment.GetResourceString("Arg_InvalidBase"));
2096             }
2097             Contract.EndContractBlock();
2098             return ParseNumbers.IntToString((int)value, toBase, -1, ' ', ParseNumbers.PrintAsI1);
2099         }
2100
2101         // Convert the Int16 value to a string in base fromBase
2102         [System.Security.SecuritySafeCritical]  // auto-generated
2103         public static String ToString (short value, int toBase) {
2104             if (toBase!=2 && toBase!=8 && toBase!=10 && toBase!=16) {
2105                 throw new ArgumentException(Environment.GetResourceString("Arg_InvalidBase"));
2106             }
2107             Contract.EndContractBlock();
2108             return ParseNumbers.IntToString((int)value, toBase, -1, ' ', ParseNumbers.PrintAsI2);
2109         }
2110
2111         // Convert the Int32 value to a string in base toBase
2112         [System.Security.SecuritySafeCritical]  // auto-generated
2113         public static String ToString (int value, int toBase) {
2114             if (toBase!=2 && toBase!=8 && toBase!=10 && toBase!=16) {
2115                 throw new ArgumentException(Environment.GetResourceString("Arg_InvalidBase"));
2116             }
2117             Contract.EndContractBlock();
2118             return ParseNumbers.IntToString(value, toBase, -1, ' ', 0);
2119         }
2120
2121         // Convert the Int64 value to a string in base toBase
2122         [System.Security.SecuritySafeCritical]  // auto-generated
2123         public static String ToString (long value, int toBase) {
2124             if (toBase!=2 && toBase!=8 && toBase!=10 && toBase!=16) {
2125                 throw new ArgumentException(Environment.GetResourceString("Arg_InvalidBase"));
2126             }
2127             Contract.EndContractBlock();
2128             return ParseNumbers.LongToString(value, toBase, -1, ' ', 0);
2129         }
2130
2131         public static String ToBase64String(byte[] inArray) {
2132             if (inArray==null) {
2133                 throw new ArgumentNullException("inArray");
2134             }
2135             Contract.Ensures(Contract.Result<string>() != null);
2136             Contract.EndContractBlock();
2137             return ToBase64String(inArray, 0, inArray.Length, Base64FormattingOptions.None);
2138         }
2139
2140         [System.Runtime.InteropServices.ComVisible(false)]
2141         public static String ToBase64String(byte[] inArray, Base64FormattingOptions options) {
2142             if (inArray==null) {
2143                 throw new ArgumentNullException("inArray");
2144             }
2145             Contract.Ensures(Contract.Result<string>() != null);
2146             Contract.EndContractBlock();
2147             return ToBase64String(inArray, 0, inArray.Length, options);
2148         }
2149
2150         public static String ToBase64String(byte[] inArray, int offset, int length) {
2151             return ToBase64String(inArray, offset, length, Base64FormattingOptions.None);
2152         }
2153
2154         [System.Security.SecuritySafeCritical]  // auto-generated
2155         [System.Runtime.InteropServices.ComVisible(false)]
2156         public static unsafe String ToBase64String(byte[] inArray, int offset, int length, Base64FormattingOptions options) {
2157             //Do data verfication
2158             if (inArray==null) 
2159                 throw new ArgumentNullException("inArray");
2160             if (length<0)
2161                 throw new ArgumentOutOfRangeException("length", Environment.GetResourceString("ArgumentOutOfRange_Index"));
2162             if (offset<0)
2163                 throw new ArgumentOutOfRangeException("offset", Environment.GetResourceString("ArgumentOutOfRange_GenericPositive"));
2164             if (options < Base64FormattingOptions.None || options > Base64FormattingOptions.InsertLineBreaks)
2165                 throw new ArgumentException(Environment.GetResourceString("Arg_EnumIllegalVal", (int)options));
2166             Contract.Ensures(Contract.Result<string>() != null);
2167             Contract.EndContractBlock();
2168
2169             int inArrayLength;
2170             int stringLength;
2171
2172             inArrayLength = inArray.Length;
2173             if (offset > (inArrayLength - length))
2174                 throw new ArgumentOutOfRangeException("offset", Environment.GetResourceString("ArgumentOutOfRange_OffsetLength"));
2175            
2176             if (inArrayLength == 0)
2177                 return String.Empty;
2178
2179             bool insertLineBreaks = (options == Base64FormattingOptions.InsertLineBreaks);
2180             //Create the new string.  This is the maximally required length.
2181             stringLength = ToBase64_CalculateAndValidateOutputLength(length, insertLineBreaks);
2182
2183             string returnString = string.FastAllocateString(stringLength);
2184             fixed (char* outChars = returnString){
2185                 fixed (byte* inData = inArray) {
2186                     int j = ConvertToBase64Array(outChars,inData,offset,length, insertLineBreaks);
2187                     BCLDebug.Assert(returnString.Length == j, "returnString.Length == j");
2188                     return returnString;
2189                 }
2190             }
2191         }
2192
2193         public static int ToBase64CharArray(byte[] inArray, int offsetIn, int length, char[] outArray, int offsetOut) {
2194             Contract.Ensures(Contract.Result<int>() >= 0);
2195             Contract.Ensures(Contract.Result<int>() <= outArray.Length);
2196             Contract.EndContractBlock();
2197
2198             return ToBase64CharArray(inArray, offsetIn, length, outArray, offsetOut, Base64FormattingOptions.None);
2199         }
2200         
2201         [System.Security.SecuritySafeCritical]  // auto-generated
2202         [System.Runtime.InteropServices.ComVisible(false)]
2203         public static unsafe int ToBase64CharArray(byte[] inArray, int offsetIn, int length, char[] outArray, int offsetOut, Base64FormattingOptions options) {
2204             //Do data verfication
2205             if (inArray==null) 
2206                 throw new ArgumentNullException("inArray");
2207             if (outArray==null)
2208                 throw new ArgumentNullException("outArray");
2209             if (length<0)
2210                 throw new ArgumentOutOfRangeException("length", Environment.GetResourceString("ArgumentOutOfRange_Index"));
2211             if (offsetIn<0)
2212                 throw new ArgumentOutOfRangeException("offsetIn", Environment.GetResourceString("ArgumentOutOfRange_GenericPositive"));
2213             if (offsetOut<0)
2214                 throw new ArgumentOutOfRangeException("offsetOut", Environment.GetResourceString("ArgumentOutOfRange_GenericPositive"));
2215
2216             if( options < Base64FormattingOptions.None || options > Base64FormattingOptions.InsertLineBreaks) {
2217                 throw new ArgumentException(Environment.GetResourceString("Arg_EnumIllegalVal", (int)options));
2218             }
2219             Contract.Ensures(Contract.Result<int>() >= 0);
2220             Contract.Ensures(Contract.Result<int>() <= outArray.Length);
2221             Contract.EndContractBlock();
2222
2223
2224             int retVal;
2225
2226             int inArrayLength;
2227             int outArrayLength;
2228             int numElementsToCopy;
2229
2230             inArrayLength = inArray.Length;
2231
2232             if (offsetIn > (int)(inArrayLength - length))
2233                 throw new ArgumentOutOfRangeException("offsetIn", Environment.GetResourceString("ArgumentOutOfRange_OffsetLength"));
2234
2235             if (inArrayLength == 0)
2236                 return 0;
2237
2238             bool insertLineBreaks = (options == Base64FormattingOptions.InsertLineBreaks);
2239             //This is the maximally required length that must be available in the char array
2240             outArrayLength = outArray.Length;
2241
2242             // Length of the char buffer required
2243             numElementsToCopy = ToBase64_CalculateAndValidateOutputLength(length, insertLineBreaks);
2244     
2245             if (offsetOut > (int)(outArrayLength -  numElementsToCopy))
2246                 throw new ArgumentOutOfRangeException("offsetOut", Environment.GetResourceString("ArgumentOutOfRange_OffsetOut"));
2247
2248             fixed (char* outChars = &outArray[offsetOut]) {
2249                 fixed (byte* inData = inArray) { 
2250                    retVal = ConvertToBase64Array(outChars,inData,offsetIn,length, insertLineBreaks);
2251                 }
2252             }
2253
2254             return retVal;
2255         }
2256
2257         [System.Security.SecurityCritical]  // auto-generated
2258         private static unsafe int ConvertToBase64Array(char* outChars, byte* inData, int offset, int length, bool insertLineBreaks) {
2259             int lengthmod3 = length%3;
2260             int calcLength = offset + (length - lengthmod3);
2261             int j=0;
2262             int charcount = 0;
2263             //Convert three bytes at a time to base64 notation.  This will consume 4 chars.
2264             int i;
2265
2266             // get a pointer to the base64Table to avoid unnecessary range checking
2267             fixed(char* base64 = base64Table) {
2268                 for (i=offset; i<calcLength; i+=3)
2269                 {
2270                     if (insertLineBreaks) {
2271                         if (charcount == base64LineBreakPosition) {
2272                             outChars[j++] = '\r';
2273                             outChars[j++] = '\n';
2274                             charcount = 0;
2275                         }
2276                         charcount += 4;
2277                     }
2278                     outChars[j] = base64[(inData[i]&0xfc)>>2];
2279                     outChars[j+1] = base64[((inData[i]&0x03)<<4) | ((inData[i+1]&0xf0)>>4)];
2280                     outChars[j+2] = base64[((inData[i+1]&0x0f)<<2) | ((inData[i+2]&0xc0)>>6)];
2281                     outChars[j+3] = base64[(inData[i+2]&0x3f)];
2282                     j += 4;
2283                 }
2284
2285                 //Where we left off before
2286                 i =  calcLength;
2287
2288                 if (insertLineBreaks && (lengthmod3 !=0) && (charcount == base64LineBreakPosition)) {
2289                     outChars[j++] = '\r';
2290                     outChars[j++] = '\n';
2291                 }
2292                     
2293                 switch(lengthmod3)
2294                 {
2295                 case 2: //One character padding needed
2296                     outChars[j] = base64[(inData[i]&0xfc)>>2];
2297                     outChars[j+1] = base64[((inData[i]&0x03)<<4)|((inData[i+1]&0xf0)>>4)];
2298                     outChars[j+2] = base64[(inData[i+1]&0x0f)<<2];
2299                     outChars[j+3] = base64[64]; //Pad
2300                     j+=4;
2301                     break;
2302                 case 1: // Two character padding needed
2303                     outChars[j] = base64[(inData[i]&0xfc)>>2];
2304                     outChars[j+1] = base64[(inData[i]&0x03)<<4];
2305                     outChars[j+2] = base64[64]; //Pad
2306                     outChars[j+3] = base64[64]; //Pad
2307                     j+=4;
2308                     break;
2309                 }
2310             }
2311             
2312             return j;
2313         }
2314
2315         private static int ToBase64_CalculateAndValidateOutputLength(int inputLength, bool insertLineBreaks) {
2316             long outlen = ((long)inputLength) / 3 * 4;          // the base length - we want integer division here. 
2317             outlen += ((inputLength % 3) != 0) ? 4 : 0;         // at most 4 more chars for the remainder
2318
2319             if (outlen == 0)
2320                 return 0;
2321
2322             if (insertLineBreaks) {
2323                 long newLines = outlen / base64LineBreakPosition;
2324                 if ((outlen % base64LineBreakPosition) == 0) {
2325                     --newLines;    
2326                 }
2327                 outlen += newLines * 2;              // the number of line break chars we'll add, "\r\n"
2328             }
2329
2330             // If we overflow an int then we cannot allocate enough
2331             // memory to output the value so throw
2332             if (outlen > int.MaxValue)
2333                 throw new OutOfMemoryException();
2334
2335             return (int)outlen;
2336         }
2337         
2338
2339         /// <summary>
2340         /// Converts the specified string, which encodes binary data as Base64 digits, to the equivalent byte array.
2341         /// </summary>
2342         /// <param name="s">The string to convert</param>
2343         /// <returns>The array of bytes represented by the specifed Base64 string.</returns>
2344         [SecuritySafeCritical] 
2345         public static Byte[] FromBase64String(String s) {
2346
2347             // "s" is an unfortunate parameter name, but we need to keep it for backward compat.
2348
2349             if (s == null)
2350                 throw new ArgumentNullException("s");
2351
2352             Contract.EndContractBlock();
2353
2354             unsafe {
2355                 fixed (Char* sPtr = s) {
2356
2357                     return FromBase64CharPtr(sPtr, s.Length);
2358                 }
2359             }
2360         }
2361
2362
2363         /// <summary>
2364         /// Converts the specified range of a Char array, which encodes binary data as Base64 digits, to the equivalent byte array.     
2365         /// </summary>
2366         /// <param name="inArray">Chars representing Base64 encoding characters</param>
2367         /// <param name="offset">A position within the input array.</param>
2368         /// <param name="length">Number of element to convert.</param>
2369         /// <returns>The array of bytes represented by the specified Base64 encoding characters.</returns>
2370         [SecuritySafeCritical]
2371         public static Byte[] FromBase64CharArray(Char[] inArray, Int32 offset, Int32 length) {
2372
2373             if (inArray == null)
2374                 throw new ArgumentNullException("inArray");
2375
2376 #if FEATURE_LEGACYNETCF
2377             Contract.EndContractBlock();
2378
2379             // throw FormatException, to ensure compatibility with Mango Apps.
2380             if (CompatibilitySwitches.IsAppEarlierThanWindowsPhone8) {
2381                 if(inArray.Length == 0) {
2382                      throw new FormatException();
2383                 }
2384             }
2385 #endif
2386
2387             if (length < 0)
2388                 throw new ArgumentOutOfRangeException("length", Environment.GetResourceString("ArgumentOutOfRange_Index"));
2389
2390             if (offset < 0)
2391                 throw new ArgumentOutOfRangeException("offset", Environment.GetResourceString("ArgumentOutOfRange_GenericPositive"));
2392
2393             if (offset > inArray.Length - length)
2394                 throw new ArgumentOutOfRangeException("offset", Environment.GetResourceString("ArgumentOutOfRange_OffsetLength"));
2395
2396 #if !FEATURE_LEGACYNETCF  // Our compat hack above breaks CCRewrite's rules on valid contracts.
2397             Contract.EndContractBlock();
2398 #endif
2399
2400             unsafe {
2401                 fixed (Char* inArrayPtr = inArray) {
2402
2403                     return FromBase64CharPtr(inArrayPtr + offset, length);
2404                 }
2405             }
2406         }            
2407
2408
2409
2410         /// <summary>
2411         /// Convert Base64 encoding characters to bytes:
2412         ///  - Compute result length exactly by actually walking the input;
2413         ///  - Allocate new result array based on computation;
2414         ///  - Decode input into the new array;
2415         /// </summary>
2416         /// <param name="inputPtr">Pointer to the first input char</param>
2417         /// <param name="inputLength">Number of input chars</param>
2418         /// <returns></returns>
2419         [SecurityCritical] 
2420         private static unsafe Byte[] FromBase64CharPtr(Char* inputPtr, Int32 inputLength) {
2421
2422             // The validity of parameters much be checked by callers, thus we are Critical here.
2423
2424             Contract.Assert(0 <= inputLength);
2425
2426             // We need to get rid of any trailing white spaces.
2427             // Otherwise we would be rejecting input such as "abc= ":
2428             while (inputLength > 0)
2429             {
2430                  Int32 lastChar = inputPtr[inputLength - 1];
2431                  if (lastChar != (Int32) ' ' && lastChar != (Int32) '\n' && lastChar != (Int32) '\r' && lastChar != (Int32) '\t')
2432                        break;
2433                 inputLength--;
2434             }
2435
2436             // Compute the output length:
2437             Int32 resultLength = FromBase64_ComputeResultLength(inputPtr, inputLength);
2438
2439             Contract.Assert(0 <= resultLength);
2440
2441             // resultLength can be zero. We will still enter FromBase64_Decode and process the input.
2442             // It may either simply write no bytes (e.g. input = " ") or throw (e.g. input = "ab").
2443             
2444             // Create result byte blob:
2445             Byte[] decodedBytes = new Byte[resultLength];
2446
2447             // Convert Base64 chars into bytes:
2448             fixed (Byte* decodedBytesPtr = decodedBytes)
2449                 FromBase64_Decode(inputPtr, inputLength, decodedBytesPtr, resultLength);
2450
2451             // Note that actualResultLength can differ from resultLength if the caller is modifying the array
2452             // as it is being converted. Silently ignore the failure.
2453             // Consider throwing exception in an non in-place release.
2454
2455             // We are done:
2456             return decodedBytes;
2457         }
2458
2459
2460         /// <summary>
2461         /// Decode characters representing a Base64 encoding into bytes:
2462         /// Walk the input. Every time 4 chars are read, convert them to the 3 corresponding output bytes.
2463         /// This method is a bit lengthy on purpose. We are trying to avoid jumps to helpers in the loop
2464         /// to aid performance.
2465         /// </summary>
2466         /// <param name="inputPtr">Pointer to first input char</param>
2467         /// <param name="inputLength">Number of input chars</param>
2468         /// <param name="destPtr">Pointer to location for teh first result byte</param>
2469         /// <param name="destLength">Max length of the preallocated result buffer</param>
2470         /// <returns>If the result buffer was not large enough to write all result bytes, return -1;
2471         /// Otherwise return the number of result bytes actually produced.</returns>
2472         [SecurityCritical]
2473         private static unsafe Int32 FromBase64_Decode(Char* startInputPtr, Int32 inputLength, Byte* startDestPtr, Int32 destLength) {
2474
2475             // You may find this method weird to look at. It\92s written for performance, not aesthetics.
2476             // You will find unrolled loops label jumps and bit manipulations.
2477
2478             const UInt32 intA =     (UInt32) 'A';            
2479             const UInt32 inta =     (UInt32) 'a';            
2480             const UInt32 int0 =     (UInt32) '0';         
2481             const UInt32 intEq =    (UInt32) '=';
2482             const UInt32 intPlus =  (UInt32) '+';
2483             const UInt32 intSlash = (UInt32) '/';
2484             const UInt32 intSpace = (UInt32) ' ';
2485             const UInt32 intTab =   (UInt32) '\t';
2486             const UInt32 intNLn =   (UInt32) '\n';
2487             const UInt32 intCRt =   (UInt32) '\r';
2488             const UInt32 intAtoZ =  (UInt32) ('Z' - 'A');  // = ('z' - 'a')
2489             const UInt32 int0to9 =  (UInt32) ('9' - '0');
2490
2491             Char* inputPtr = startInputPtr;
2492             Byte* destPtr = startDestPtr;
2493
2494             // Pointers to the end of input and output:
2495             Char* endInputPtr = inputPtr + inputLength;
2496             Byte* endDestPtr = destPtr + destLength;
2497
2498             // Current char code/value:
2499             UInt32 currCode;
2500              
2501             // This 4-byte integer will contain the 4 codes of the current 4-char group.
2502             // Eeach char codes for 6 bits = 24 bits.
2503             // The remaining byte will be FF, we use it as a marker when 4 chars have been processed.            
2504             UInt32 currBlockCodes = 0x000000FFu;
2505                             
2506             unchecked { while (true) {
2507
2508                 // break when done:
2509                 if (inputPtr >= endInputPtr)
2510                     goto _AllInputConsumed;
2511
2512                 // Get current char:
2513                 currCode = (UInt32) (*inputPtr);
2514                 inputPtr++;
2515
2516                 // Determine current char code:
2517
2518                 if (currCode - intA <= intAtoZ)
2519                     currCode -= intA;                
2520
2521                 else if (currCode - inta <= intAtoZ)
2522                     currCode -= (inta - 26u);                
2523
2524                 else if (currCode - int0 <= int0to9)
2525                     currCode -= (int0 - 52u);
2526
2527                 else {
2528                     // Use the slower switch for less common cases:
2529                     switch(currCode) {
2530
2531                         // Significant chars:
2532                         case intPlus:  currCode = 62u;
2533                             break;
2534
2535                         case intSlash: currCode = 63u;
2536                             break;
2537
2538                         // Legal no-value chars (we ignore these):
2539                         case intCRt:  case intNLn:  case intSpace:  case intTab:                             
2540                             continue;
2541
2542                         // The equality char is only legal at the end of the input.
2543                         // Jump after the loop to make it easier for the JIT register predictor to do a good job for the loop itself:
2544                         case intEq:
2545                             goto _EqualityCharEncountered; 
2546
2547                         // Other chars are illegal:
2548                         default:
2549                             throw new FormatException(Environment.GetResourceString("Format_BadBase64Char"));
2550                     }
2551                 }
2552
2553                 // Ok, we got the code. Save it:
2554                 currBlockCodes = (currBlockCodes << 6) | currCode;
2555
2556                 // Last bit in currBlockCodes will be on after in shifted right 4 times:
2557                 if ((currBlockCodes & 0x80000000u) != 0u) {
2558
2559                     if ((Int32) (endDestPtr - destPtr) < 3)
2560                          return -1;
2561
2562                     *(destPtr)     = (Byte) (currBlockCodes >> 16);
2563                     *(destPtr + 1) = (Byte) (currBlockCodes >> 8);
2564                     *(destPtr + 2) = (Byte) (currBlockCodes);
2565                     destPtr += 3;
2566
2567                     currBlockCodes = 0x000000FFu;
2568                 }
2569
2570             }}  // unchecked while
2571
2572             // 'd be nice to have an assert that we never get here, but CS0162: Unreachable code detected.
2573             // Contract.Assert(false, "We only leave the above loop by jumping; should never get here.");
2574
2575             // We jump here out of the loop if we hit an '=':
2576             _EqualityCharEncountered:
2577
2578             Contract.Assert(currCode == intEq);
2579
2580             // Recall that inputPtr is now one position past where '=' was read.
2581             // '=' can only be at the last input pos:
2582             if (inputPtr == endInputPtr) {
2583
2584                 // Code is zero for trailing '=':
2585                 currBlockCodes <<= 6;
2586
2587                 // The '=' did not complete a 4-group. The input must be bad:
2588                 if ((currBlockCodes & 0x80000000u) == 0u)
2589                     throw new FormatException(Environment.GetResourceString("Format_BadBase64CharArrayLength"));
2590
2591                 if ((int)(endDestPtr - destPtr) < 2)  // Autch! We underestimated the output length!
2592                     return -1;
2593
2594                 // We are good, store bytes form this past group. We had a single "=", so we take two bytes:
2595                 *(destPtr++) = (Byte) (currBlockCodes >> 16);
2596                 *(destPtr++) = (Byte) (currBlockCodes >> 8);
2597
2598                 currBlockCodes = 0x000000FFu;                
2599
2600             } else { // '=' can also be at the pre-last position iff the last is also a '=' excluding the white spaces:
2601                                 
2602                 // We need to get rid of any intermediate white spaces.
2603                 // Otherwise we would be rejecting input such as "abc= =":
2604                 while (inputPtr < (endInputPtr - 1))
2605                 {
2606                      Int32 lastChar = *(inputPtr);
2607                      if (lastChar != (Int32) ' ' && lastChar != (Int32) '\n' && lastChar != (Int32) '\r' && lastChar != (Int32) '\t')
2608                            break;
2609                     inputPtr++;
2610                 }
2611
2612                 if (inputPtr == (endInputPtr - 1) && *(inputPtr) == '=') {
2613                 
2614                     // Code is zero for each of the two '=':
2615                     currBlockCodes <<= 12;
2616
2617                     // The '=' did not complete a 4-group. The input must be bad:
2618                     if ((currBlockCodes & 0x80000000u) == 0u)
2619                         throw new FormatException(Environment.GetResourceString("Format_BadBase64CharArrayLength"));
2620
2621                     if ((Int32) (endDestPtr - destPtr) < 1)  // Autch! We underestimated the output length!
2622                         return -1;
2623
2624                     // We are good, store bytes form this past group. We had a "==", so we take only one byte:
2625                     *(destPtr++) = (Byte) (currBlockCodes >> 16);
2626
2627                     currBlockCodes = 0x000000FFu;
2628                     
2629                 } else  // '=' is not ok at places other than the end:
2630                     throw new FormatException(Environment.GetResourceString("Format_BadBase64Char"));
2631                             
2632             }
2633             
2634             // We get here either from above or by jumping out of the loop:
2635             _AllInputConsumed:
2636
2637             // The last block of chars has less than 4 items
2638             if (currBlockCodes != 0x000000FFu)
2639                 throw new FormatException(Environment.GetResourceString("Format_BadBase64CharArrayLength"));
2640
2641             // Return how many bytes were actually recovered:
2642             return (Int32) (destPtr - startDestPtr);
2643
2644         } // Int32 FromBase64_Decode(...)
2645
2646
2647         /// <summary>
2648         /// Compute the number of bytes encoded in the specified Base 64 char array:
2649         /// Walk the entire input counting white spaces and padding chars, then compute result length
2650         /// based on 3 bytes per 4 chars.
2651         /// </summary>
2652         [SecurityCritical]
2653         private static unsafe Int32 FromBase64_ComputeResultLength(Char* inputPtr, Int32 inputLength) {
2654
2655             const UInt32 intEq =    (UInt32) '=';            
2656             const UInt32 intSpace = (UInt32) ' ';
2657
2658             Contract.Assert(0 <= inputLength);
2659
2660             Char* inputEndPtr = inputPtr + inputLength;
2661             Int32 usefulInputLength = inputLength;
2662             Int32 padding = 0;
2663
2664             while (inputPtr < inputEndPtr) {
2665
2666                 UInt32 c = (UInt32) (*inputPtr);
2667                 inputPtr++;
2668
2669                 // We want to be as fast as possible and filter out spaces with as few comparisons as possible.
2670                 // We end up accepting a number of illegal chars as legal white-space chars.
2671                 // This is ok: as soon as we hit them during actual decode we will recognise them as illegal and throw.
2672                 if (c <= intSpace)
2673                     usefulInputLength--;
2674
2675                 else if (c == intEq) {
2676                     usefulInputLength--;
2677                     padding++;
2678                 }
2679             }
2680
2681             Contract.Assert(0 <= usefulInputLength);
2682             
2683             // For legal input, we can assume that 0 <= padding < 3. But it may be more for illegal input.
2684             // We will notice it at decode when we see a '=' at the wrong place.
2685             Contract.Assert(0 <= padding);
2686
2687             // Perf: reuse the variable that stored the number of '=' to store the number of bytes encoded by the
2688             // last group that contains the '=':
2689             if (padding != 0) {
2690
2691                 if (padding == 1)
2692                     padding = 2;
2693                 else if (padding == 2)
2694                     padding = 1;
2695                 else
2696                     throw new FormatException(Environment.GetResourceString("Format_BadBase64Char"));  
2697             }
2698
2699             // Done:
2700             return (usefulInputLength / 4) * 3 + padding;
2701         }
2702
2703     }  // class Convert
2704 }  // namespace
2705