5 // Derek Holden (dholden@draper.com)
\r
6 // Duncan Mak (duncan@ximian.com)
\r
8 // (C) Ximian, Inc. http://www.ximian.com
\r
11 // System.Convert class. This was written word for word off the
\r
12 // Library specification for System.Convert in the ECMA TC39 TG2
\r
13 // and TG3 working documents. The first page of which has a table
\r
14 // for all legal conversion scenerios.
\r
16 // This header and the one above it can be formatted however, just trying
\r
17 // to keep it consistent w/ the existing mcs headers.
\r
19 // This Convert class could be written another way, with each type
\r
20 // implementing IConvertible and defining their own conversion functions,
\r
21 // and this class just calling the type's implementation. Or, they can
\r
22 // be defined here and the implementing type can use these functions when
\r
23 // defining their IConvertible interface. Byte's ToBoolean() calls
\r
24 // Convert.ToBoolean(byte), or Convert.ToBoolean(byte) calls
\r
25 // byte.ToBoolean(). The first case is what is done here.
\r
27 // See http://lists.ximian.com/archives/public/mono-list/2001-July/000525.html
\r
29 // There are also conversion functions that are not defined in
\r
30 // the ECMA draft, such as there is no bool ToBoolean(DateTime value),
\r
31 // and placing that somewhere won't compile w/ this Convert since the
\r
32 // function doesn't exist. However calling that when using Microsoft's
\r
33 // System.Convert doesn't produce any compiler errors, it just throws
\r
34 // an InvalidCastException at runtime.
\r
36 // Whenever a decimal, double, or single is converted to an integer
\r
37 // based type, it is even rounded. This uses Math.Round which only
\r
38 // has Round(decimal) and Round(double), so in the Convert from
\r
39 // single cases the value is passed to Math as a double. This
\r
40 // may not be completely necessary.
\r
42 // The .NET Framework SDK lists DBNull as a member of this class
\r
43 // as 'public static readonly object DBNull;'.
\r
45 // It should also be decided if all the cast return values should be
\r
46 // returned as unchecked or not.
\r
48 // All the XML function comments were auto generated which is why they
\r
49 // sound someone redundant.
\r
51 // TYPE | BOOL BYTE CHAR DT DEC DBL I16 I32 I64 SBYT SNGL STR UI16 UI32 UI64
\r
52 // -----+--------------------------------------------------------------------
\r
53 // BOOL | X X X X X X X X X X X X X
\r
54 // BYTE | X X X X X X X X X X X X X X
\r
55 // CHAR | X X X X X X X X X X
\r
57 // DEC | X X X X X X X X X X X X X
\r
58 // DBL | X X X X X X X X X X X X X
\r
59 // I16 | X X X X X X X X X X X X X X
\r
60 // I32 | X X X X X X X X X X X X X X
\r
61 // I64 | X X X X X X X X X X X X X X
\r
62 // SBYT | X X X X X X X X X X X X X X
\r
63 // SNGL | X X X X X X X X X X X X X
\r
64 // STR | X X X X X X X X X X X X X X X
\r
65 // UI16 | X X X X X X X X X X X X X X
\r
66 // UI32 | X X X X X X X X X X X X X X
\r
67 // UI64 | X X X X X X X X X X X X X X
\r
71 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
\r
73 // Permission is hereby granted, free of charge, to any person obtaining
\r
74 // a copy of this software and associated documentation files (the
\r
75 // "Software"), to deal in the Software without restriction, including
\r
76 // without limitation the rights to use, copy, modify, merge, publish,
\r
77 // distribute, sublicense, and/or sell copies of the Software, and to
\r
78 // permit persons to whom the Software is furnished to do so, subject to
\r
79 // the following conditions:
\r
81 // The above copyright notice and this permission notice shall be
\r
82 // included in all copies or substantial portions of the Software.
\r
84 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
\r
85 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
\r
86 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
\r
87 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
\r
88 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
\r
89 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
\r
90 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
93 using System.Globalization;
\r
95 using System.Security.Cryptography;
\r
97 using System.Runtime.CompilerServices;
\r
101 // [CLSCompliant(false)]
\r
102 public sealed class Convert {
\r
105 public static readonly object DBNull = System.DBNull.Value;
\r
106 static ToBase64Transform toBase64Transform = new ToBase64Transform();
\r
112 [MethodImplAttribute (MethodImplOptions.InternalCall)]
\r
113 extern static byte [] InternalFromBase64String (string str, bool allowWhitespaceOnly);
\r
115 [MethodImplAttribute (MethodImplOptions.InternalCall)]
\r
116 extern static byte [] InternalFromBase64CharArray (char [] arr, int offset, int length);
\r
118 public static byte[] FromBase64CharArray (char[] inArray, int offset, int length)
\r
120 if (inArray == null)
\r
121 throw new ArgumentNullException ("inArray");
\r
123 throw new ArgumentOutOfRangeException ("offset < 0");
\r
125 throw new ArgumentOutOfRangeException ("length < 0");
\r
126 // avoid integer overflow
\r
127 if (offset > inArray.Length - length)
\r
128 throw new ArgumentOutOfRangeException ("offset + length > array.Length");
\r
130 return InternalFromBase64CharArray (inArray, offset, length);
\r
133 public static byte[] FromBase64String (string s)
\r
136 throw new ArgumentNullException ("s");
\r
138 if (s.Length == 0) {
\r
139 return new byte[0];
\r
143 return InternalFromBase64String (s, true);
\r
145 return InternalFromBase64String (s, false);
\r
149 public static TypeCode GetTypeCode (object value)
\r
152 return TypeCode.Empty;
\r
154 return Type.GetTypeCode (value.GetType ());
\r
157 public static bool IsDBNull (object value)
\r
159 if (value is DBNull)
\r
165 public static int ToBase64CharArray (byte[] inArray, int offsetIn, int length,
\r
166 char[] outArray, int offsetOut)
\r
168 if (inArray == null)
\r
169 throw new ArgumentNullException ("inArray");
\r
170 if (outArray == null)
\r
171 throw new ArgumentNullException ("outArray");
\r
172 if (offsetIn < 0 || length < 0 || offsetOut < 0)
\r
173 throw new ArgumentOutOfRangeException ("offsetIn, length, offsetOut < 0");
\r
174 // avoid integer overflow
\r
175 if (offsetIn > inArray.Length - length)
\r
176 throw new ArgumentOutOfRangeException ("offsetIn + length > array.Length");
\r
178 // note: normally ToBase64Transform doesn't support multiple block processing
\r
179 byte[] outArr = toBase64Transform.InternalTransformFinalBlock (inArray, offsetIn, length);
\r
181 char[] cOutArr = new ASCIIEncoding ().GetChars (outArr);
\r
183 // avoid integer overflow
\r
184 if (offsetOut > outArray.Length - cOutArr.Length)
\r
185 throw new ArgumentOutOfRangeException ("offsetOut + cOutArr.Length > outArray.Length");
\r
187 Array.Copy (cOutArr, 0, outArray, offsetOut, cOutArr.Length);
\r
189 return cOutArr.Length;
\r
192 public static string ToBase64String (byte[] inArray)
\r
194 if (inArray == null)
\r
195 throw new ArgumentNullException ("inArray");
\r
197 return ToBase64String (inArray, 0, inArray.Length);
\r
200 public static string ToBase64String (byte[] inArray, int offset, int length)
\r
202 if (inArray == null)
\r
203 throw new ArgumentNullException ("inArray");
\r
204 if (offset < 0 || length < 0)
\r
205 throw new ArgumentOutOfRangeException ("offset < 0 || length < 0");
\r
206 // avoid integer overflow
\r
207 if (offset > inArray.Length - length)
\r
208 throw new ArgumentOutOfRangeException ("offset + length > array.Length");
\r
210 // note: normally ToBase64Transform doesn't support multiple block processing
\r
211 byte[] outArr = toBase64Transform.InternalTransformFinalBlock (inArray, offset, length);
\r
213 return (new ASCIIEncoding ().GetString (outArr));
\r
217 public static string ToBase64String (byte[] inArray, Base64FormattingOptions options)
\r
219 if (inArray == null)
\r
220 throw new ArgumentNullException ("inArray");
\r
221 return ToBase64String (inArray, 0, inArray.Length, options);
\r
224 public static string ToBase64String (byte[] inArray, int offset, int length, Base64FormattingOptions options)
\r
226 if (inArray == null)
\r
227 throw new ArgumentNullException ("inArray");
\r
228 if (offset < 0 || length < 0)
\r
229 throw new ArgumentOutOfRangeException ("offset < 0 || length < 0");
\r
230 // avoid integer overflow
\r
231 if (offset > inArray.Length - length)
\r
232 throw new ArgumentOutOfRangeException ("offset + length > array.Length");
\r
234 Encoding encoding = new ASCIIEncoding ();
\r
235 BinaryReader reader = new BinaryReader (new MemoryStream (inArray, offset, length));
\r
238 if (options == Base64FormattingOptions.InsertLineBreaks) {
\r
239 StringBuilder sb = new StringBuilder ();
\r
241 // 54 bytes of input makes for 72 bytes of output.
\r
242 b = reader.ReadBytes (54);
\r
244 sb.AppendLine (encoding.GetString (toBase64Transform.InternalTransformFinalBlock (b, 0, b.Length)));
245 } while (b.Length > 0);
246 return sb.ToString ();
\r
248 return encoding.GetString (toBase64Transform.InternalTransformFinalBlock (inArray, offset, length));
255 // ========== Boolean Conversions ========== //
\r
257 public static bool ToBoolean (bool value)
\r
262 public static bool ToBoolean (byte value)
\r
264 return (value != 0);
\r
267 public static bool ToBoolean (char value)
\r
269 throw new InvalidCastException (Locale.GetText ("Can't convert char to bool"));
\r
272 public static bool ToBoolean (DateTime value)
\r
274 throw new InvalidCastException (Locale.GetText ("Can't convert date to bool"));
\r
277 public static bool ToBoolean (decimal value)
\r
279 return (value != 0M);
\r
282 public static bool ToBoolean (double value)
\r
284 return (value != 0);
\r
287 public static bool ToBoolean (float value)
\r
289 return (value != 0f);
\r
292 public static bool ToBoolean (int value)
\r
294 return (value != 0);
\r
297 public static bool ToBoolean (long value)
\r
299 return (value != 0);
\r
302 [CLSCompliant (false)]
\r
303 public static bool ToBoolean (sbyte value)
\r
305 return (value != 0);
\r
308 public static bool ToBoolean (short value)
\r
310 return (value != 0);
\r
313 public static bool ToBoolean (string value)
\r
316 return false; // LAMESPEC: Spec says throw ArgumentNullException
\r
317 return Boolean.Parse (value);
\r
320 public static bool ToBoolean (string value, IFormatProvider provider)
\r
323 return false; // LAMESPEC: Spec says throw ArgumentNullException
\r
324 return Boolean.Parse (value); // provider is ignored.
\r
327 [CLSCompliant (false)]
\r
328 public static bool ToBoolean (uint value)
\r
330 return (value != 0);
\r
333 [CLSCompliant (false)]
\r
334 public static bool ToBoolean (ulong value)
\r
336 return (value != 0);
\r
339 [CLSCompliant (false)]
\r
340 public static bool ToBoolean (ushort value)
\r
342 //if (value == null)
\r
344 return (value != 0);
\r
347 public static bool ToBoolean (object value)
\r
351 return ToBoolean (value, null);
\r
354 public static bool ToBoolean (object value, IFormatProvider provider)
\r
358 return ((IConvertible) value).ToBoolean (provider);
\r
361 // ========== Byte Conversions ========== //
\r
363 public static byte ToByte (bool value)
\r
365 return (byte)(value ? 1 : 0);
\r
368 public static byte ToByte (byte value)
\r
373 public static byte ToByte (char value)
\r
375 if (value > Byte.MaxValue)
\r
376 throw new OverflowException (Locale.GetText (
\r
377 "Value is greater than Byte.MaxValue"));
\r
379 return (byte)value;
\r
382 public static byte ToByte (DateTime value)
\r
384 throw new InvalidCastException ("This conversion is not supported.");
\r
387 public static byte ToByte (decimal value)
\r
389 if (value > Byte.MaxValue || value < Byte.MinValue)
\r
390 throw new OverflowException (Locale.GetText (
\r
391 "Value is greater than Byte.MaxValue or less than Byte.MinValue"));
\r
393 // Returned Even-Rounded
\r
394 return (byte)(Math.Round (value));
\r
397 public static byte ToByte (double value)
\r
399 if (value > Byte.MaxValue || value < Byte.MinValue)
\r
400 throw new OverflowException (Locale.GetText (
\r
401 "Value is greater than Byte.MaxValue or less than Byte.MinValue"));
\r
403 // This and the float version of ToByte are the only ones
\r
404 // the spec listed as checking for .NaN and Infinity overflow
\r
405 if (Double.IsNaN(value) || Double.IsInfinity(value))
\r
406 throw new OverflowException (Locale.GetText (
\r
407 "Value is equal to Double.NaN, Double.PositiveInfinity, or Double.NegativeInfinity"));
\r
409 // Returned Even-Rounded
\r
410 return (byte)(Math.Round (value));
\r
413 public static byte ToByte (float value)
\r
415 if (value > Byte.MaxValue || value < Byte.MinValue)
\r
416 throw new OverflowException (Locale.GetText (
\r
417 "Value is greater than Byte.MaxValue or less than Byte.Minalue"));
\r
419 // This and the double version of ToByte are the only ones
\r
420 // the spec listed as checking for .NaN and Infinity overflow
\r
421 if (Single.IsNaN(value) || Single.IsInfinity(value))
\r
422 throw new OverflowException (Locale.GetText (
\r
423 "Value is equal to Single.NaN, Single.PositiveInfinity, or Single.NegativeInfinity"));
\r
425 // Returned Even-Rounded, pass it as a double, could have this
\r
426 // method just call Convert.ToByte ( (double)value)
\r
427 return (byte)(Math.Round ( (double)value));
\r
430 public static byte ToByte (int value)
\r
432 if (value > Byte.MaxValue || value < Byte.MinValue)
\r
433 throw new OverflowException (Locale.GetText (
\r
434 "Value is greater than Byte.MaxValue or less than Byte.MinValue"));
\r
436 return (byte)value;
\r
439 public static byte ToByte (long value)
\r
441 if (value > Byte.MaxValue || value < Byte.MinValue)
\r
442 throw new OverflowException (Locale.GetText (
\r
443 "Value is greater than Byte.MaxValue or less than Byte.MinValue"));
\r
445 return (byte)value;
\r
448 [CLSCompliant (false)]
\r
449 public static byte ToByte (sbyte value)
\r
451 if (value < Byte.MinValue)
\r
452 throw new OverflowException (Locale.GetText (
\r
453 "Value is less than Byte.MinValue"));
\r
455 return (byte)value;
\r
458 public static byte ToByte (short value)
\r
460 if (value > Byte.MaxValue || value < Byte.MinValue)
\r
461 throw new OverflowException (Locale.GetText (
\r
462 "Value is greater than Byte.MaxValue or less than Byte.MinValue"));
\r
464 return (byte)value;
\r
467 public static byte ToByte (string value)
\r
470 return 0; // LAMESPEC: Spec says throw ArgumentNullException
\r
471 return Byte.Parse (value);
\r
474 public static byte ToByte (string value, IFormatProvider provider)
\r
477 return 0; // LAMESPEC: Spec says throw ArgumentNullException
\r
478 return Byte.Parse (value, provider);
\r
481 public static byte ToByte (string value, int fromBase)
\r
483 int retVal = ConvertFromBase (value, fromBase, true);
\r
485 if (retVal < (int) Byte.MinValue || retVal > (int) Byte.MaxValue)
\r
486 throw new OverflowException ();
\r
488 return (byte) retVal;
\r
491 [CLSCompliant (false)]
\r
492 public static byte ToByte (uint value)
\r
494 if (value > Byte.MaxValue)
\r
495 throw new OverflowException (Locale.GetText (
\r
496 "Value is greater than Byte.MaxValue"));
\r
498 return (byte)value;
\r
501 [CLSCompliant (false)]
\r
502 public static byte ToByte (ulong value)
\r
504 if (value > Byte.MaxValue)
\r
505 throw new OverflowException (Locale.GetText (
\r
506 "Value is greater than Byte.MaxValue"));
\r
508 return (byte)value;
\r
511 [CLSCompliant (false)]
\r
512 public static byte ToByte (ushort value)
\r
514 if (value > Byte.MaxValue)
\r
515 throw new OverflowException (Locale.GetText (
\r
516 "Value is greater than Byte.MaxValue"));
\r
518 return (byte)value;
\r
521 public static byte ToByte (object value)
\r
525 return ToByte (value, null);
\r
528 public static byte ToByte (object value, IFormatProvider provider)
\r
532 return ((IConvertible) value).ToByte (provider);
\r
535 // ========== Char Conversions ========== //
\r
537 public static char ToChar (bool value)
\r
539 throw new InvalidCastException ("This conversion is not supported.");
\r
542 public static char ToChar (byte value)
\r
544 return (char)value;
\r
547 public static char ToChar (char value)
\r
552 public static char ToChar (DateTime value)
\r
554 throw new InvalidCastException ("This conversion is not supported.");
\r
557 public static char ToChar (decimal value)
\r
559 throw new InvalidCastException ("This conversion is not supported.");
\r
562 public static char ToChar (double value)
\r
564 throw new InvalidCastException ("This conversion is not supported.");
\r
567 public static char ToChar (int value)
\r
569 if (value > Char.MaxValue || value < Char.MinValue)
\r
570 throw new OverflowException (Locale.GetText (
\r
571 "Value is greater than Char.MaxValue or less than Char.MinValue"));
\r
573 return (char)value;
\r
576 public static char ToChar (long value)
\r
578 if (value > Char.MaxValue || value < Char.MinValue)
\r
579 throw new OverflowException (Locale.GetText (
\r
580 "Value is greater than Char.MaxValue or less than Char.MinValue"));
\r
582 return (char)value;
\r
585 public static char ToChar (float value)
\r
587 throw new InvalidCastException ("This conversion is not supported.");
\r
590 [CLSCompliant (false)]
\r
591 public static char ToChar (sbyte value)
\r
593 if (value < Char.MinValue)
\r
594 throw new OverflowException (Locale.GetText (
\r
595 "Value is less than Char.MinValue"));
\r
597 return (char)value;
\r
600 public static char ToChar (short value)
\r
602 if (value < Char.MinValue)
\r
603 throw new OverflowException (Locale.GetText (
\r
604 "Value is less than Char.MinValue"));
\r
606 return (char)value;
\r
609 public static char ToChar (string value)
\r
611 return Char.Parse (value);
\r
614 public static char ToChar (string value, IFormatProvider provider)
\r
616 return Char.Parse (value); // provider is ignored.
\r
619 [CLSCompliant (false)]
\r
620 public static char ToChar (uint value)
\r
622 if (value > Char.MaxValue)
\r
623 throw new OverflowException (Locale.GetText (
\r
624 "Value is greater than Char.MaxValue"));
\r
626 return (char)value;
\r
629 [CLSCompliant (false)]
\r
630 public static char ToChar (ulong value)
\r
632 if (value > Char.MaxValue)
\r
633 throw new OverflowException (Locale.GetText (
\r
634 "Value is greater than Char.MaxValue"));
\r
636 return (char)value;
\r
639 [CLSCompliant (false)]
\r
640 public static char ToChar (ushort value)
\r
642 if (value > Char.MaxValue)
\r
643 throw new OverflowException (Locale.GetText (
\r
644 "Value is greater than Char.MaxValue"));
\r
646 return (char)value;
\r
649 public static char ToChar (object value)
\r
653 return ToChar (value, null);
\r
656 public static char ToChar (object value, IFormatProvider provider)
\r
660 return ((IConvertible) value).ToChar (provider);
\r
663 // ========== DateTime Conversions ========== //
\r
665 public static DateTime ToDateTime (string value)
\r
668 return DateTime.MinValue; // LAMESPEC: Spec says throw ArgumentNullException
\r
669 return DateTime.Parse (value);
\r
672 public static DateTime ToDateTime (string value, IFormatProvider provider)
\r
675 return DateTime.MinValue; // LAMESPEC: Spec says throw ArgumentNullException
\r
676 return DateTime.Parse (value, provider);
\r
679 public static DateTime ToDateTime (bool value)
\r
681 throw new InvalidCastException ("This conversion is not supported.");
\r
684 public static DateTime ToDateTime (byte value)
\r
686 throw new InvalidCastException ("This conversion is not supported.");
\r
689 public static DateTime ToDateTime (char value)
\r
691 throw new InvalidCastException ("This conversion is not supported.");
\r
694 public static DateTime ToDateTime (DateTime value)
\r
699 public static DateTime ToDateTime (decimal value)
\r
701 throw new InvalidCastException ("This conversion is not supported.");
\r
704 public static DateTime ToDateTime (double value)
\r
706 throw new InvalidCastException ("This conversion is not supported.");
\r
709 public static DateTime ToDateTime (short value)
\r
711 throw new InvalidCastException ("This conversion is not supported.");
\r
714 public static DateTime ToDateTime (int value)
\r
716 throw new InvalidCastException ("This conversion is not supported.");
\r
719 public static DateTime ToDateTime (long value)
\r
721 throw new InvalidCastException ("This conversion is not supported.");
\r
724 public static DateTime ToDateTime (float value)
\r
726 throw new InvalidCastException ("This conversion is not supported.");
\r
729 public static DateTime ToDateTime (object value)
\r
732 return DateTime.MinValue;
\r
733 return ToDateTime (value, null);
\r
736 public static DateTime ToDateTime (object value, IFormatProvider provider)
\r
739 return DateTime.MinValue;
\r
740 return ((IConvertible) value).ToDateTime (provider);
\r
743 [CLSCompliant (false)]
\r
744 public static DateTime ToDateTime (sbyte value)
\r
746 throw new InvalidCastException ("This conversion is not supported.");
\r
748 [CLSCompliant (false)]
\r
749 public static DateTime ToDateTime (ushort value)
\r
751 throw new InvalidCastException ("This conversion is not supported.");
\r
754 [CLSCompliant (false)]
\r
755 public static DateTime ToDateTime (uint value)
\r
757 throw new InvalidCastException ("This conversion is not supported.");
\r
760 [CLSCompliant (false)]
\r
761 public static DateTime ToDateTime (ulong value)
\r
763 throw new InvalidCastException ("This conversion is not supported.");
\r
766 // ========== Decimal Conversions ========== //
\r
768 public static decimal ToDecimal (bool value)
\r
770 return value ? 1 : 0;
\r
773 public static decimal ToDecimal (byte value)
\r
775 return (decimal)value;
\r
778 public static decimal ToDecimal (char value)
\r
780 throw new InvalidCastException ("This conversion is not supported.");
\r
783 public static decimal ToDecimal (DateTime value)
\r
785 throw new InvalidCastException ("This conversion is not supported.");
\r
788 public static decimal ToDecimal (decimal value)
\r
793 public static decimal ToDecimal (double value)
\r
795 if (value > (double)Decimal.MaxValue || value < (double)Decimal.MinValue)
\r
796 throw new OverflowException (Locale.GetText (
\r
797 "Value is greater than Decimal.MaxValue or less than Decimal.MinValue"));
\r
799 return (decimal)value;
\r
802 public static decimal ToDecimal (float value)
\r
804 return (decimal) value;
\r
807 public static decimal ToDecimal (int value)
\r
809 return (decimal)value;
\r
812 public static decimal ToDecimal (long value)
\r
814 return (decimal)value;
\r
817 [CLSCompliant (false)]
\r
818 public static decimal ToDecimal (sbyte value)
\r
820 return (decimal)value;
\r
823 public static decimal ToDecimal (short value)
\r
825 return (decimal)value;
\r
828 public static decimal ToDecimal (string value)
\r
831 return new Decimal (0); // LAMESPEC: Spec says throw ArgumentNullException
\r
832 return Decimal.Parse (value);
\r
835 public static decimal ToDecimal (string value, IFormatProvider provider)
\r
838 return new Decimal (0); // LAMESPEC: Spec says throw ArgumentNullException
\r
839 return Decimal.Parse (value, provider);
\r
842 [CLSCompliant (false)]
\r
843 public static decimal ToDecimal (uint value)
\r
845 return (decimal)value;
\r
848 [CLSCompliant (false)]
\r
849 public static decimal ToDecimal (ulong value)
\r
851 return (decimal)value;
\r
854 [CLSCompliant (false)]
\r
855 public static decimal ToDecimal (ushort value)
\r
857 return (decimal)value;
\r
860 public static decimal ToDecimal (object value)
\r
863 return new Decimal (0);
\r
864 return ToDecimal (value, null);
\r
867 public static decimal ToDecimal (object value, IFormatProvider provider)
\r
870 return new Decimal (0);
\r
871 return ((IConvertible) value).ToDecimal (provider);
\r
875 // ========== Double Conversions ========== //
\r
877 public static double ToDouble (bool value)
\r
879 return value ? 1 : 0;
\r
882 public static double ToDouble (byte value)
\r
884 return (double) value;
\r
887 public static double ToDouble (char value)
\r
889 throw new InvalidCastException ("This conversion is not supported.");
\r
892 public static double ToDouble (DateTime value)
\r
894 throw new InvalidCastException ("This conversion is not supported.");
\r
897 public static double ToDouble (decimal value)
\r
899 return (double)value;
\r
902 public static double ToDouble (double value)
\r
907 public static double ToDouble (float value)
\r
909 return (double) value;
\r
912 public static double ToDouble (int value)
\r
914 return (double)value;
\r
917 public static double ToDouble (long value)
\r
919 return (double)value;
\r
922 [CLSCompliant (false)]
\r
923 public static double ToDouble (sbyte value)
\r
925 return (double)value;
\r
928 public static double ToDouble (short value)
\r
930 return (double)value;
\r
933 public static double ToDouble (string value)
\r
936 return 0.0; // LAMESPEC: Spec says throw ArgumentNullException
\r
937 return Double.Parse (value);
\r
940 public static double ToDouble (string value, IFormatProvider provider)
\r
943 return 0.0; // LAMESPEC: Spec says throw ArgumentNullException
\r
944 return Double.Parse (value, provider);
\r
947 [CLSCompliant (false)]
\r
948 public static double ToDouble (uint value)
\r
950 return (double)value;
\r
953 [CLSCompliant (false)]
\r
954 public static double ToDouble (ulong value)
\r
956 return (double)value;
\r
959 [CLSCompliant (false)]
\r
960 public static double ToDouble (ushort value)
\r
962 return (double)value;
\r
965 public static double ToDouble (object value)
\r
969 return ToDouble (value, null);
\r
972 public static double ToDouble (object value, IFormatProvider provider)
\r
976 return ((IConvertible) value).ToDouble (provider);
\r
979 // ========== Int16 Conversions ========== //
\r
981 public static short ToInt16 (bool value)
\r
983 return (short)(value ? 1 : 0);
\r
986 public static short ToInt16 (byte value)
\r
988 return (short)value;
\r
991 public static short ToInt16 (char value)
\r
993 if (value > Int16.MaxValue)
\r
994 throw new OverflowException (Locale.GetText (
\r
995 "Value is greater than Int16.MaxValue"));
\r
997 return (short)value;
\r
1000 public static short ToInt16 (DateTime value)
\r
1002 throw new InvalidCastException ("This conversion is not supported.");
\r
1005 public static short ToInt16 (decimal value)
\r
1007 if (value > Int16.MaxValue || value < Int16.MinValue)
\r
1008 throw new OverflowException (Locale.GetText (
\r
1009 "Value is greater than Int16.MaxValue or less than Int16.MinValue"));
\r
1011 // Returned Even-Rounded
\r
1012 return (short)(Math.Round (value));
\r
1015 public static short ToInt16 (double value)
\r
1017 if (value > Int16.MaxValue || value < Int16.MinValue)
\r
1018 throw new OverflowException (Locale.GetText (
\r
1019 "Value is greater than Int16.MaxValue or less than Int16.MinValue"));
\r
1021 // Returned Even-Rounded
\r
1022 return (short)(Math.Round (value));
\r
1025 public static short ToInt16 (float value)
\r
1027 if (value > Int16.MaxValue || value < Int16.MinValue)
\r
1028 throw new OverflowException (Locale.GetText (
\r
1029 "Value is greater than Int16.MaxValue or less than Int16.MinValue"));
\r
1031 // Returned Even-Rounded, use Math.Round pass as a double.
\r
1032 return (short)Math.Round ( (double)value);
\r
1035 public static short ToInt16 (int value)
\r
1037 if (value > Int16.MaxValue || value < Int16.MinValue)
\r
1038 throw new OverflowException (Locale.GetText (
\r
1039 "Value is greater than Int16.MaxValue or less than Int16.MinValue"));
\r
1041 return (short)value;
\r
1044 public static short ToInt16 (long value)
\r
1046 if (value > Int16.MaxValue || value < Int16.MinValue)
\r
1047 throw new OverflowException (Locale.GetText (
\r
1048 "Value is greater than Int16.MaxValue or less than Int16.MinValue"));
\r
1050 return (short)value;
\r
1053 [CLSCompliant (false)]
\r
1054 public static short ToInt16 (sbyte value)
\r
1056 return (short)value;
\r
1059 public static short ToInt16 (short value)
\r
1064 public static short ToInt16 (string value)
\r
1066 if (value == null)
\r
1067 return 0; // LAMESPEC: Spec says throw ArgumentNullException
\r
1068 return Int16.Parse (value);
\r
1071 public static short ToInt16 (string value, IFormatProvider provider)
\r
1073 if (value == null)
\r
1074 return 0; // LAMESPEC: Spec says throw ArgumentNullException
\r
1075 return Int16.Parse (value, provider);
\r
1078 public static short ToInt16 (string value, int fromBase)
\r
1080 int result = ConvertFromBase (value, fromBase, false);
\r
1081 if (fromBase != 10) {
\r
1082 if (result > ushort.MaxValue) {
\r
1083 throw new OverflowException ("Value was either too large or too small for an Int16.");
\r
1086 // note: no sign are available to detect negatives
\r
1087 if (result > Int16.MaxValue) {
\r
1088 // return negative 2's complement
\r
1089 return Convert.ToInt16 (-(65536 - result));
\r
1092 return Convert.ToInt16 (result);
\r
1095 [CLSCompliant (false)]
\r
1096 public static short ToInt16 (uint value)
\r
1098 if (value > Int16.MaxValue)
\r
1099 throw new OverflowException (Locale.GetText (
\r
1100 "Value is greater than Int16.MaxValue"));
\r
1102 return (short)value;
\r
1105 [CLSCompliant (false)]
\r
1106 public static short ToInt16 (ulong value)
\r
1108 if (value > (ulong)Int16.MaxValue)
\r
1109 throw new OverflowException (Locale.GetText (
\r
1110 "Value is greater than Int16.MaxValue"));
\r
1111 return (short)value;
\r
1114 [CLSCompliant (false)]
\r
1115 public static short ToInt16 (ushort value)
\r
1117 if (value > Int16.MaxValue)
\r
1118 throw new OverflowException (Locale.GetText (
\r
1119 "Value is greater than Int16.MaxValue"));
\r
1121 return (short)value;
\r
1124 public static short ToInt16 (object value)
\r
1126 if (value == null)
\r
1128 return ToInt16 (value, null);
\r
1131 public static short ToInt16 (object value, IFormatProvider provider)
\r
1133 if (value == null)
\r
1135 return ((IConvertible) value).ToInt16 (provider);
\r
1138 // ========== Int32 Conversions ========== //
\r
1140 public static int ToInt32 (bool value)
\r
1142 return value ? 1 : 0;
\r
1145 public static int ToInt32 (byte value)
\r
1147 return (int)value;
\r
1150 public static int ToInt32 (char value)
\r
1152 return (int)value;
\r
1155 public static int ToInt32 (DateTime value)
\r
1157 throw new InvalidCastException ("This conversion is not supported.");
\r
1160 public static int ToInt32 (decimal value)
\r
1162 if (value > Int32.MaxValue || value < Int32.MinValue)
\r
1163 throw new OverflowException (Locale.GetText (
\r
1164 "Value is greater than Int32.MaxValue or less than Int32.MinValue"));
\r
1166 // Returned Even-Rounded
\r
1167 return (int)(Math.Round (value));
\r
1170 public static int ToInt32 (double value)
\r
1172 if (value > Int32.MaxValue || value < Int32.MinValue)
\r
1173 throw new OverflowException (Locale.GetText (
\r
1174 "Value is greater than Int32.MaxValue or less than Int32.MinValue"));
\r
1176 // Returned Even-Rounded
\r
1177 return (int)(Math.Round (value));
\r
1180 public static int ToInt32 (float value)
\r
1182 if (value > Int32.MaxValue || value < Int32.MinValue)
\r
1183 throw new OverflowException (Locale.GetText (
\r
1184 "Value is greater than Int32.MaxValue or less than Int32.MinValue"));
\r
1186 // Returned Even-Rounded, pass as a double, could just call
\r
1187 // Convert.ToInt32 ( (double)value);
\r
1188 return (int)(Math.Round ( (double)value));
\r
1191 public static int ToInt32 (int value)
\r
1196 public static int ToInt32 (long value)
\r
1198 if (value > Int32.MaxValue || value < Int32.MinValue)
\r
1199 throw new OverflowException (Locale.GetText (
\r
1200 "Value is greater than Int32.MaxValue or less than Int32.MinValue"));
\r
1202 return (int)value;
\r
1205 [CLSCompliant (false)]
\r
1206 public static int ToInt32 (sbyte value)
\r
1208 return (int)value;
\r
1211 public static int ToInt32 (short value)
\r
1213 return (int)value;
\r
1216 public static int ToInt32 (string value)
\r
1218 if (value == null)
\r
1219 return 0; // LAMESPEC: Spec says throw ArgumentNullException
\r
1220 return Int32.Parse (value);
\r
1223 public static int ToInt32 (string value, IFormatProvider provider)
\r
1225 if (value == null)
\r
1226 return 0; // LAMESPEC: Spec says throw ArgumentNullException
\r
1227 return Int32.Parse (value, provider);
\r
1231 public static int ToInt32 (string value, int fromBase)
\r
1233 return ConvertFromBase (value, fromBase, false);
\r
1236 [CLSCompliant (false)]
\r
1237 public static int ToInt32 (uint value)
\r
1239 if (value > Int32.MaxValue)
\r
1240 throw new OverflowException (Locale.GetText (
\r
1241 "Value is greater than Int32.MaxValue"));
\r
1243 return (int)value;
\r
1246 [CLSCompliant (false)]
\r
1247 public static int ToInt32 (ulong value)
\r
1249 if (value > Int32.MaxValue)
\r
1250 throw new OverflowException (Locale.GetText (
\r
1251 "Value is greater than Int32.MaxValue"));
\r
1253 return (int)value;
\r
1256 [CLSCompliant (false)]
\r
1257 public static int ToInt32 (ushort value)
\r
1259 return (int)value;
\r
1262 public static int ToInt32 (object value)
\r
1264 if (value == null)
\r
1266 return ToInt32 (value, null);
\r
1269 public static int ToInt32 (object value, IFormatProvider provider)
\r
1271 if (value == null)
\r
1273 return ((IConvertible) value).ToInt32 (provider);
\r
1276 // ========== Int64 Conversions ========== //
\r
1278 public static long ToInt64 (bool value)
\r
1280 return value ? 1 : 0;
\r
1283 public static long ToInt64 (byte value)
\r
1285 return (long)(ulong)value;
\r
1288 public static long ToInt64 (char value)
\r
1290 return (long)value;
\r
1293 public static long ToInt64 (DateTime value)
\r
1295 throw new InvalidCastException ("This conversion is not supported.");
\r
1298 public static long ToInt64 (decimal value)
\r
1300 if (value > Int64.MaxValue || value < Int64.MinValue)
\r
1301 throw new OverflowException (Locale.GetText (
\r
1302 "Value is greater than Int64.MaxValue or less than Int64.MinValue"));
\r
1304 // Returned Even-Rounded
\r
1305 return (long)(Math.Round (value));
\r
1308 public static long ToInt64 (double value)
\r
1310 if (value > Int64.MaxValue || value < Int64.MinValue)
\r
1311 throw new OverflowException (Locale.GetText (
\r
1312 "Value is greater than Int64.MaxValue or less than Int64.MinValue"));
\r
1314 // Returned Even-Rounded
\r
1315 return (long)(Math.Round (value));
\r
1318 public static long ToInt64 (float value)
\r
1320 if (value > Int64.MaxValue || value < Int64.MinValue)
\r
1321 throw new OverflowException (Locale.GetText (
\r
1322 "Value is greater than Int64.MaxValue or less than Int64.MinValue"));
\r
1324 // Returned Even-Rounded, pass to Math as a double, could
\r
1325 // just call Convert.ToInt64 ( (double)value);
\r
1326 return (long)(Math.Round ( (double)value));
\r
1329 public static long ToInt64 (int value)
\r
1331 return (long)value;
\r
1334 public static long ToInt64 (long value)
\r
1339 [CLSCompliant (false)]
\r
1340 public static long ToInt64 (sbyte value)
\r
1342 return (long)value;
\r
1345 public static long ToInt64 (short value)
\r
1347 return (long)value;
\r
1350 public static long ToInt64 (string value)
\r
1352 if (value == null)
\r
1353 return 0; // LAMESPEC: Spec says throw ArgumentNullException
\r
1354 return Int64.Parse (value);
\r
1357 public static long ToInt64 (string value, IFormatProvider provider)
\r
1359 if (value == null)
\r
1360 return 0; // LAMESPEC: Spec says throw ArgumentNullException
\r
1361 return Int64.Parse (value, provider);
\r
1364 public static long ToInt64 (string value, int fromBase)
\r
1366 return ConvertFromBase64 (value, fromBase, false);
\r
1369 [CLSCompliant (false)]
\r
1370 public static long ToInt64 (uint value)
\r
1372 return (long)(ulong)value;
\r
1375 [CLSCompliant (false)]
\r
1376 public static long ToInt64 (ulong value)
\r
1378 if (value > Int64.MaxValue)
\r
1379 throw new OverflowException (Locale.GetText (
\r
1380 "Value is greater than Int64.MaxValue"));
\r
1382 return (long)value;
\r
1385 [CLSCompliant (false)]
\r
1386 public static long ToInt64 (ushort value)
\r
1388 return (long)(ulong)value;
\r
1391 public static long ToInt64 (object value)
\r
1393 if (value == null)
\r
1395 return ToInt64 (value, null);
\r
1398 public static long ToInt64 (object value, IFormatProvider provider)
\r
1400 if (value == null)
\r
1402 return ((IConvertible) value).ToInt64 (provider);
\r
1405 // ========== SByte Conversions ========== //
\r
1407 [CLSCompliant (false)]
\r
1408 public static sbyte ToSByte (bool value)
\r
1410 return (sbyte)(value ? 1 : 0);
\r
1413 [CLSCompliant (false)]
\r
1414 public static sbyte ToSByte (byte value)
\r
1416 if (value > SByte.MaxValue)
\r
1417 throw new OverflowException (Locale.GetText (
\r
1418 "Value is greater than SByte.MaxValue"));
\r
1420 return (sbyte)value;
\r
1423 [CLSCompliant (false)]
\r
1424 public static sbyte ToSByte (char value)
\r
1426 if (value > SByte.MaxValue)
\r
1427 throw new OverflowException (Locale.GetText (
\r
1428 "Value is greater than SByte.MaxValue"));
\r
1430 return (sbyte)value;
\r
1433 [CLSCompliant (false)]
\r
1434 public static sbyte ToSByte (DateTime value)
\r
1436 throw new InvalidCastException ("This conversion is not supported.");
\r
1439 [CLSCompliant (false)]
\r
1440 public static sbyte ToSByte (decimal value)
\r
1442 if (value > SByte.MaxValue || value < SByte.MinValue)
\r
1443 throw new OverflowException (Locale.GetText (
\r
1444 "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
\r
1446 // Returned Even-Rounded
\r
1447 return (sbyte)(Math.Round (value));
\r
1450 [CLSCompliant (false)]
\r
1451 public static sbyte ToSByte (double value)
\r
1453 if (value > SByte.MaxValue || value < SByte.MinValue)
\r
1454 throw new OverflowException (Locale.GetText (
\r
1455 "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
\r
1457 // Returned Even-Rounded
\r
1458 return (sbyte)(Math.Round (value));
\r
1461 [CLSCompliant (false)]
\r
1462 public static sbyte ToSByte (float value)
\r
1464 if (value > SByte.MaxValue || value < SByte.MinValue)
\r
1465 throw new OverflowException (Locale.GetText (
\r
1466 "Value is greater than SByte.MaxValue or less than SByte.Minalue"));
\r
1468 // Returned Even-Rounded, pass as double to Math
\r
1469 return (sbyte)(Math.Round ( (double)value));
\r
1472 [CLSCompliant (false)]
\r
1473 public static sbyte ToSByte (int value)
\r
1475 if (value > SByte.MaxValue || value < SByte.MinValue)
\r
1476 throw new OverflowException (Locale.GetText (
\r
1477 "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
\r
1479 return (sbyte)value;
\r
1482 [CLSCompliant (false)]
\r
1483 public static sbyte ToSByte (long value)
\r
1485 if (value > SByte.MaxValue || value < SByte.MinValue)
\r
1486 throw new OverflowException (Locale.GetText (
\r
1487 "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
\r
1489 return (sbyte)value;
\r
1492 [CLSCompliant (false)]
\r
1493 public static sbyte ToSByte (sbyte value)
\r
1498 [CLSCompliant (false)]
\r
1499 public static sbyte ToSByte (short value)
\r
1501 if (value > SByte.MaxValue || value < SByte.MinValue)
\r
1502 throw new OverflowException (Locale.GetText (
\r
1503 "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
\r
1505 return (sbyte)value;
\r
1508 [CLSCompliant (false)]
\r
1509 public static sbyte ToSByte (string value)
\r
1511 if (value == null)
\r
1512 return 0; // LAMESPEC: Spec says throw ArgumentNullException
\r
1513 return SByte.Parse (value);
\r
1516 [CLSCompliant (false)]
\r
1517 public static sbyte ToSByte (string value, IFormatProvider provider)
\r
1519 if (value == null)
\r
1520 throw new ArgumentNullException ("value");
\r
1521 return SByte.Parse (value, provider);
\r
1524 [CLSCompliant (false)]
\r
1525 public static sbyte ToSByte (string value, int fromBase)
\r
1527 int result = ConvertFromBase (value, fromBase, false);
\r
1528 if (fromBase != 10) {
\r
1529 // note: no sign are available to detect negatives
\r
1530 if (result > SByte.MaxValue) {
\r
1531 // return negative 2's complement
\r
1532 return Convert.ToSByte (-(256 - result));
\r
1535 return Convert.ToSByte (result);
\r
1538 [CLSCompliant (false)]
\r
1539 public static sbyte ToSByte (uint value)
\r
1541 if (value > SByte.MaxValue)
\r
1542 throw new OverflowException (Locale.GetText (
\r
1543 "Value is greater than SByte.MaxValue"));
\r
1545 return (sbyte)value;
\r
1548 [CLSCompliant (false)]
\r
1549 public static sbyte ToSByte (ulong value)
\r
1551 if (value > (ulong)SByte.MaxValue)
\r
1552 throw new OverflowException (Locale.GetText (
\r
1553 "Value is greater than SByte.MaxValue"));
\r
1555 return (sbyte)value;
\r
1558 [CLSCompliant (false)]
\r
1559 public static sbyte ToSByte (ushort value)
\r
1561 if (value > SByte.MaxValue)
\r
1562 throw new OverflowException (Locale.GetText (
\r
1563 "Value is greater than SByte.MaxValue"));
\r
1565 return (sbyte)value;
\r
1568 [CLSCompliant (false)]
\r
1569 public static sbyte ToSByte (object value)
\r
1571 if (value == null)
\r
1573 return ToSByte (value, null);
\r
1576 [CLSCompliant (false)]
\r
1577 public static sbyte ToSByte (object value, IFormatProvider provider)
\r
1579 if (value == null)
\r
1581 return ((IConvertible) value).ToSByte (provider);
\r
1584 // ========== Single Conversions ========== //
\r
1586 public static float ToSingle (bool value)
\r
1588 return value ? 1 : 0;
\r
1591 public static float ToSingle (byte value)
\r
1593 return (float)value;
\r
1596 public static float ToSingle (Char value)
\r
1598 throw new InvalidCastException ("This conversion is not supported.");
\r
1601 public static float ToSingle (DateTime value)
\r
1603 throw new InvalidCastException ("This conversion is not supported.");
\r
1606 public static float ToSingle (decimal value)
\r
1608 return (float)value;
\r
1611 public static float ToSingle (double value)
\r
1613 return (float)value;
\r
1616 public static float ToSingle (float value)
\r
1621 public static float ToSingle (int value)
\r
1623 return (float)value;
\r
1626 public static float ToSingle (long value)
\r
1628 return (float)value;
\r
1631 [CLSCompliant (false)]
\r
1632 public static float ToSingle (sbyte value)
\r
1634 return (float)value;
\r
1637 public static float ToSingle (short value)
\r
1639 return (float)value;
\r
1642 public static float ToSingle (string value)
\r
1644 if (value == null)
\r
1645 return 0.0f; // LAMESPEC: Spec says throw ArgumentNullException
\r
1646 return Single.Parse (value);
\r
1649 public static float ToSingle (string value, IFormatProvider provider)
\r
1651 if (value == null)
\r
1652 return 0.0f; // LAMESPEC: Spec says throw ArgumentNullException
\r
1653 return Single.Parse (value, provider);
\r
1656 [CLSCompliant (false)]
\r
1657 public static float ToSingle (uint value)
\r
1659 return (float)value;
\r
1662 [CLSCompliant (false)]
\r
1663 public static float ToSingle (ulong value)
\r
1665 return (float)value;
\r
1668 [CLSCompliant (false)]
\r
1669 public static float ToSingle (ushort value)
\r
1671 return (float)value;
\r
1674 public static float ToSingle (object value)
\r
1676 if (value == null)
\r
1678 return ToSingle (value, null);
\r
1681 // [CLSCompliant (false)]
\r
1682 public static float ToSingle (object value, IFormatProvider provider)
\r
1684 if (value == null)
\r
1686 return ((IConvertible) value).ToSingle (provider);
\r
1689 // ========== String Conversions ========== //
\r
1691 public static string ToString (bool value)
\r
1693 return value.ToString ();
\r
1696 public static string ToString (bool value, IFormatProvider provider)
\r
1698 return value.ToString (); // the same as ToString (bool).
\r
1701 public static string ToString (byte value)
\r
1703 return value.ToString ();
\r
1706 public static string ToString (byte value, IFormatProvider provider)
\r
1708 return value.ToString (provider);
\r
1711 public static string ToString (byte value, int toBase)
\r
1716 return value.ToString ();
\r
1718 byte[] val = BitConverter.GetBytes (value);
\r
1722 return ConvertToBase2 (val);
\r
1724 return ConvertToBase8 (val);
\r
1726 return ConvertToBase16 (val);
\r
1728 throw new ArgumentException (Locale.GetText ("toBase is not valid."));
\r
1732 public static string ToString (char value)
\r
1734 return value.ToString ();
\r
1737 public static string ToString (char value, IFormatProvider provider)
\r
1739 return value.ToString (); // the same as ToString (char)
\r
1742 public static string ToString (DateTime value)
\r
1744 return value.ToString ();
\r
1747 public static string ToString (DateTime value, IFormatProvider provider)
\r
1749 return value.ToString (provider);
\r
1752 public static string ToString (decimal value)
\r
1754 return value.ToString ();
\r
1757 public static string ToString (decimal value, IFormatProvider provider)
\r
1759 return value.ToString (provider);
\r
1762 public static string ToString (double value)
\r
1764 return value.ToString ();
\r
1767 public static string ToString (double value, IFormatProvider provider)
\r
1769 return value.ToString (provider);
\r
1772 public static string ToString (float value)
\r
1774 return value.ToString ();
\r
1777 public static string ToString (float value, IFormatProvider provider)
\r
1779 return value.ToString (provider);
\r
1782 public static string ToString (int value)
\r
1784 return value.ToString ();
\r
1787 public static string ToString (int value, int toBase)
\r
1792 return value.ToString ();
\r
1794 byte[] val = BitConverter.GetBytes (value);
\r
1798 return ConvertToBase2 (val);
\r
1800 return ConvertToBase8 (val);
\r
1802 return ConvertToBase16 (val);
\r
1804 throw new ArgumentException (Locale.GetText ("toBase is not valid."));
\r
1808 public static string ToString (int value, IFormatProvider provider)
\r
1810 return value.ToString (provider);
\r
1813 public static string ToString (long value)
\r
1815 return value.ToString ();
\r
1818 public static string ToString (long value, int toBase)
\r
1823 return value.ToString ();
\r
1825 byte[] val = BitConverter.GetBytes (value);
\r
1829 return ConvertToBase2 (val);
\r
1831 return ConvertToBase8 (val);
\r
1833 return ConvertToBase16 (val);
\r
1835 throw new ArgumentException (Locale.GetText ("toBase is not valid."));
\r
1839 public static string ToString (long value, IFormatProvider provider)
\r
1841 return value.ToString (provider);
\r
1844 public static string ToString (object value)
\r
1846 return ToString (value, null);
\r
1849 public static string ToString (object value, IFormatProvider provider)
\r
1851 if (value is IConvertible)
\r
1852 return ((IConvertible) value).ToString (provider);
\r
1853 else if (value != null)
\r
1854 return value.ToString ();
\r
1855 return String.Empty;
\r
1858 [CLSCompliant (false)]
\r
1859 public static string ToString (sbyte value)
\r
1861 return value.ToString ();
\r
1864 [CLSCompliant (false)]
\r
1865 public static string ToString (sbyte value, IFormatProvider provider)
\r
1867 return value.ToString (provider);
\r
1870 public static string ToString (short value)
\r
1872 return value.ToString ();
\r
1875 public static string ToString (short value, int toBase)
\r
1880 return value.ToString ();
\r
1882 byte[] val = BitConverter.GetBytes (value);
\r
1886 return ConvertToBase2 (val);
\r
1888 return ConvertToBase8 (val);
\r
1890 return ConvertToBase16 (val);
\r
1892 throw new ArgumentException (Locale.GetText ("toBase is not valid."));
\r
1896 public static string ToString (short value, IFormatProvider provider)
\r
1898 return value.ToString (provider);
\r
1901 public static string ToString (string value)
\r
1906 public static string ToString (string value, IFormatProvider provider)
\r
1908 return value; // provider is ignored.
\r
1911 [CLSCompliant (false)]
\r
1912 public static string ToString (uint value)
\r
1914 return value.ToString ();
\r
1917 [CLSCompliant (false)]
\r
1918 public static string ToString (uint value, IFormatProvider provider)
\r
1920 return value.ToString (provider);
\r
1923 [CLSCompliant (false)]
\r
1924 public static string ToString (ulong value)
\r
1926 return value.ToString ();
\r
1929 [CLSCompliant (false)]
\r
1930 public static string ToString (ulong value, IFormatProvider provider)
\r
1932 return value.ToString (provider);
\r
1935 [CLSCompliant (false)]
\r
1936 public static string ToString (ushort value)
\r
1938 return value.ToString ();
\r
1941 [CLSCompliant (false)]
\r
1942 public static string ToString (ushort value, IFormatProvider provider)
\r
1944 return value.ToString (provider);
\r
1947 // ========== UInt16 Conversions ========== //
\r
1949 [CLSCompliant (false)]
\r
1950 public static ushort ToUInt16 (bool value)
\r
1952 return (ushort)(value ? 1 : 0);
\r
1955 [CLSCompliant (false)]
\r
1956 public static ushort ToUInt16 (byte value)
\r
1958 return (ushort)value;
\r
1961 [CLSCompliant (false)]
\r
1962 public static ushort ToUInt16 (char value)
\r
1964 return (ushort)value;
\r
1967 [CLSCompliant (false)]
\r
1968 public static ushort ToUInt16 (DateTime value)
\r
1970 throw new InvalidCastException ("This conversion is not supported.");
\r
1973 [CLSCompliant (false)]
\r
1974 public static ushort ToUInt16 (decimal value)
\r
1976 if (value > UInt16.MaxValue || value < UInt16.MinValue)
\r
1977 throw new OverflowException (Locale.GetText (
\r
1978 "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
\r
1980 // Returned Even-Rounded
\r
1981 return (ushort)(Math.Round (value));
\r
1984 [CLSCompliant (false)]
\r
1985 public static ushort ToUInt16 (double value)
\r
1987 if (value > UInt16.MaxValue || value < UInt16.MinValue)
\r
1988 throw new OverflowException (Locale.GetText (
\r
1989 "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
\r
1991 // Returned Even-Rounded
\r
1992 return (ushort)(Math.Round (value));
\r
1995 [CLSCompliant (false)]
\r
1996 public static ushort ToUInt16 (float value)
\r
1998 if (value > UInt16.MaxValue || value < UInt16.MinValue)
\r
1999 throw new OverflowException (Locale.GetText (
\r
2000 "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
\r
2002 // Returned Even-Rounded, pass as double to Math
\r
2003 return (ushort)(Math.Round ( (double)value));
\r
2006 [CLSCompliant (false)]
\r
2007 public static ushort ToUInt16 (int value)
\r
2009 if (value > UInt16.MaxValue || value < UInt16.MinValue)
\r
2010 throw new OverflowException (Locale.GetText (
\r
2011 "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
\r
2013 return (ushort)value;
\r
2016 [CLSCompliant (false)]
\r
2017 public static ushort ToUInt16 (long value)
\r
2019 if (value > UInt16.MaxValue || value < UInt16.MinValue)
\r
2020 throw new OverflowException (Locale.GetText (
\r
2021 "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
\r
2023 return (ushort)value;
\r
2026 [CLSCompliant (false)]
\r
2027 public static ushort ToUInt16 (sbyte value)
\r
2029 if (value < UInt16.MinValue)
\r
2030 throw new OverflowException (Locale.GetText (
\r
2031 "Value is less than UInt16.MinValue"));
\r
2033 return (ushort)value;
\r
2036 [CLSCompliant (false)]
\r
2037 public static ushort ToUInt16 (short value)
\r
2039 if (value < UInt16.MinValue)
\r
2040 throw new OverflowException (Locale.GetText (
\r
2041 "Value is less than UInt16.MinValue"));
\r
2043 return (ushort)value;
\r
2046 [CLSCompliant (false)]
\r
2047 public static ushort ToUInt16 (string value)
\r
2049 if (value == null)
\r
2050 return 0; // LAMESPEC: Spec says throw ArgumentNullException
\r
2051 return UInt16.Parse (value);
\r
2054 [CLSCompliant (false)]
\r
2055 public static ushort ToUInt16 (string value, IFormatProvider provider)
\r
2057 if (value == null)
\r
2058 return 0; // LAMESPEC: Spec says throw ArgumentNullException
\r
2059 return UInt16.Parse (value, provider);
\r
2062 [CLSCompliant (false)]
\r
2063 public static ushort ToUInt16 (string value, int fromBase)
\r
2065 return ToUInt16 (ConvertFromBase (value, fromBase, true));
\r
2068 [CLSCompliant (false)]
\r
2069 public static ushort ToUInt16 (uint value)
\r
2071 if (value > UInt16.MaxValue)
\r
2072 throw new OverflowException (Locale.GetText (
\r
2073 "Value is greater than UInt16.MaxValue"));
\r
2075 return (ushort)value;
\r
2078 [CLSCompliant (false)]
\r
2079 public static ushort ToUInt16 (ulong value)
\r
2081 if (value > (ulong)UInt16.MaxValue)
\r
2082 throw new OverflowException (Locale.GetText (
\r
2083 "Value is greater than UInt16.MaxValue"));
\r
2085 return (ushort)value;
\r
2088 [CLSCompliant (false)]
\r
2089 public static ushort ToUInt16 (ushort value)
\r
2094 [CLSCompliant (false)]
\r
2095 public static ushort ToUInt16 (object value)
\r
2097 if (value == null)
\r
2099 return ToUInt16 (value, null);
\r
2102 [CLSCompliant (false)]
\r
2103 public static ushort ToUInt16 (object value, IFormatProvider provider)
\r
2105 if (value == null)
\r
2107 return ((IConvertible) value).ToUInt16 (provider);
\r
2110 // ========== UInt32 Conversions ========== //
\r
2112 [CLSCompliant (false)]
\r
2113 public static uint ToUInt32 (bool value)
\r
2115 return (uint)(value ? 1 : 0);
\r
2118 [CLSCompliant (false)]
\r
2119 public static uint ToUInt32 (byte value)
\r
2121 return (uint)value;
\r
2124 [CLSCompliant (false)]
\r
2125 public static uint ToUInt32 (char value)
\r
2127 return (uint)value;
\r
2130 [CLSCompliant (false)]
\r
2131 public static uint ToUInt32 (DateTime value)
\r
2133 throw new InvalidCastException ("This conversion is not supported.");
\r
2136 [CLSCompliant (false)]
\r
2137 public static uint ToUInt32 (decimal value)
\r
2139 if (value > UInt32.MaxValue || value < UInt32.MinValue)
\r
2140 throw new OverflowException (Locale.GetText (
\r
2141 "Value is greater than UInt32.MaxValue or less than UInt32.MinValue"));
\r
2143 // Returned Even-Rounded
\r
2144 return (uint)(Math.Round (value));
\r
2147 [CLSCompliant (false)]
\r
2148 public static uint ToUInt32 (double value)
\r
2150 if (value > UInt32.MaxValue || value < UInt32.MinValue)
\r
2151 throw new OverflowException (Locale.GetText (
\r
2152 "Value is greater than UInt32.MaxValue or less than UInt32.MinValue"));
\r
2154 // Returned Even-Rounded
\r
2155 return (uint)(Math.Round (value));
\r
2158 [CLSCompliant (false)]
\r
2159 public static uint ToUInt32 (float value)
\r
2161 if (value > UInt32.MaxValue || value < UInt32.MinValue)
\r
2162 throw new OverflowException (Locale.GetText (
\r
2163 "Value is greater than UInt32.MaxValue or less than UInt32.MinValue"));
\r
2165 // Returned Even-Rounded, pass as double to Math
\r
2166 return (uint)(Math.Round ( (double)value));
\r
2169 [CLSCompliant (false)]
\r
2170 public static uint ToUInt32 (int value)
\r
2172 if (value < UInt32.MinValue)
\r
2173 throw new OverflowException (Locale.GetText (
\r
2174 "Value is less than UInt32.MinValue"));
\r
2176 return (uint)value;
\r
2179 [CLSCompliant (false)]
\r
2180 public static uint ToUInt32 (long value)
\r
2182 if (value > UInt32.MaxValue || value < UInt32.MinValue)
\r
2183 throw new OverflowException (Locale.GetText (
\r
2184 "Value is greater than UInt32.MaxValue or less than UInt32.MinValue"));
\r
2186 return (uint)value;
\r
2189 [CLSCompliant (false)]
\r
2190 public static uint ToUInt32 (sbyte value)
\r
2192 if (value < UInt32.MinValue)
\r
2193 throw new OverflowException (Locale.GetText (
\r
2194 "Value is less than UInt32.MinValue"));
\r
2196 return (uint)value;
\r
2199 [CLSCompliant (false)]
\r
2200 public static uint ToUInt32 (short value)
\r
2202 if (value < UInt32.MinValue)
\r
2203 throw new OverflowException (Locale.GetText (
\r
2204 "Value is less than UInt32.MinValue"));
\r
2206 return (uint)value;
\r
2209 [CLSCompliant (false)]
\r
2210 public static uint ToUInt32 (string value)
\r
2212 if (value == null)
\r
2213 return 0; // LAMESPEC: Spec says throw ArgumentNullException
\r
2214 return UInt32.Parse (value);
\r
2217 [CLSCompliant (false)]
\r
2218 public static uint ToUInt32 (string value, IFormatProvider provider)
\r
2220 if (value == null)
\r
2221 return 0; // LAMESPEC: Spec says throw ArgumentNullException
\r
2222 return UInt32.Parse (value, provider);
\r
2225 [CLSCompliant (false)]
\r
2226 public static uint ToUInt32 (string value, int fromBase)
\r
2228 return (uint) ConvertFromBase (value, fromBase, true);
\r
2231 [CLSCompliant (false)]
\r
2232 public static uint ToUInt32 (uint value)
\r
2237 [CLSCompliant (false)]
\r
2238 public static uint ToUInt32 (ulong value)
\r
2240 if (value > UInt32.MaxValue)
\r
2241 throw new OverflowException (Locale.GetText (
\r
2242 "Value is greater than UInt32.MaxValue"));
\r
2244 return (uint)value;
\r
2247 [CLSCompliant (false)]
\r
2248 public static uint ToUInt32 (ushort value)
\r
2250 return (uint)value;
\r
2253 [CLSCompliant (false)]
\r
2254 public static uint ToUInt32 (object value)
\r
2256 if (value == null)
\r
2258 return ToUInt32 (value, null);
\r
2261 [CLSCompliant (false)]
\r
2262 public static uint ToUInt32 (object value, IFormatProvider provider)
\r
2264 if (value == null)
\r
2266 return ((IConvertible) value).ToUInt32 (provider);
\r
2270 // ========== UInt64 Conversions ========== //
\r
2272 [CLSCompliant (false)]
\r
2273 public static ulong ToUInt64 (bool value)
\r
2275 return (ulong)(value ? 1 : 0);
\r
2278 [CLSCompliant (false)]
\r
2279 public static ulong ToUInt64 (byte value)
\r
2281 return (ulong)value;
\r
2284 [CLSCompliant (false)]
\r
2285 public static ulong ToUInt64 (char value)
\r
2287 return (ulong)value;
\r
2290 [CLSCompliant (false)]
\r
2291 public static ulong ToUInt64 (DateTime value)
\r
2293 throw new InvalidCastException ("The conversion is not supported.");
\r
2296 [CLSCompliant (false)]
\r
2297 public static ulong ToUInt64 (decimal value)
\r
2299 if (value > UInt64.MaxValue || value < UInt64.MinValue)
\r
2300 throw new OverflowException (Locale.GetText (
\r
2301 "Value is greater than UInt64.MaxValue or less than UInt64.MinValue"));
\r
2303 // Returned Even-Rounded
\r
2304 return (ulong)(Math.Round (value));
\r
2307 [CLSCompliant (false)]
\r
2308 public static ulong ToUInt64 (double value)
\r
2310 if (value > UInt64.MaxValue || value < UInt64.MinValue)
\r
2311 throw new OverflowException (Locale.GetText (
\r
2312 "Value is greater than UInt64.MaxValue or less than UInt64.MinValue"));
\r
2314 // Returned Even-Rounded
\r
2315 return (ulong)(Math.Round (value));
\r
2318 [CLSCompliant (false)]
\r
2319 public static ulong ToUInt64 (float value)
\r
2321 if (value > UInt64.MaxValue || value < UInt64.MinValue)
\r
2322 throw new OverflowException (Locale.GetText (
\r
2323 "Value is greater than UInt64.MaxValue or less than UInt64.MinValue"));
\r
2325 // Returned Even-Rounded, pass as a double to Math
\r
2326 return (ulong)(Math.Round ( (double)value));
\r
2329 [CLSCompliant (false)]
\r
2330 public static ulong ToUInt64 (int value)
\r
2332 if (value < (int)UInt64.MinValue)
\r
2333 throw new OverflowException (Locale.GetText (
\r
2334 "Value is less than UInt64.MinValue"));
\r
2336 return (ulong)value;
\r
2339 [CLSCompliant (false)]
\r
2340 public static ulong ToUInt64 (long value)
\r
2342 if (value < (long)UInt64.MinValue)
\r
2343 throw new OverflowException (Locale.GetText (
\r
2344 "Value is less than UInt64.MinValue"));
\r
2346 return (ulong)value;
\r
2349 [CLSCompliant (false)]
\r
2350 public static ulong ToUInt64 (sbyte value)
\r
2352 if (value < (sbyte)UInt64.MinValue)
\r
2353 throw new OverflowException
\r
2354 ("Value is less than UInt64.MinValue");
\r
2356 return (ulong)value;
\r
2359 [CLSCompliant (false)]
\r
2360 public static ulong ToUInt64 (short value)
\r
2362 if (value < (short)UInt64.MinValue)
\r
2363 throw new OverflowException (Locale.GetText (
\r
2364 "Value is less than UInt64.MinValue"));
\r
2366 return (ulong)value;
\r
2369 [CLSCompliant (false)]
\r
2370 public static ulong ToUInt64 (string value)
\r
2372 if (value == null)
\r
2373 return 0; // LAMESPEC: Spec says throw ArgumentNullException
\r
2374 return UInt64.Parse (value);
\r
2377 [CLSCompliant (false)]
\r
2378 public static ulong ToUInt64 (string value, IFormatProvider provider)
\r
2380 if (value == null)
\r
2381 return 0; // LAMESPEC: Spec says throw ArgumentNullException
\r
2382 return UInt64.Parse (value, provider);
\r
2385 [CLSCompliant (false)]
\r
2386 public static ulong ToUInt64 (string value, int fromBase)
\r
2388 return (ulong) ConvertFromBase64 (value, fromBase, true);
\r
2391 [CLSCompliant (false)]
\r
2392 public static ulong ToUInt64 (uint value)
\r
2394 return (ulong)value;
\r
2397 [CLSCompliant (false)]
\r
2398 public static ulong ToUInt64 (ulong value)
\r
2403 [CLSCompliant (false)]
\r
2404 public static ulong ToUInt64 (ushort value)
\r
2406 return (ulong)value;
\r
2409 [CLSCompliant (false)]
\r
2410 public static ulong ToUInt64 (object value)
\r
2412 if (value == null)
\r
2414 return ToUInt64 (value, null);
\r
2417 [CLSCompliant (false)]
\r
2418 public static ulong ToUInt64 (object value, IFormatProvider provider)
\r
2420 if (value == null)
\r
2422 return ((IConvertible) value).ToUInt64 (provider);
\r
2426 // ========== Conversion / Helper Functions ========== //
\r
2428 public static object ChangeType (object value, Type conversionType)
\r
2430 if ((value != null) && (conversionType == null))
\r
2431 throw new ArgumentNullException ("conversionType");
\r
2432 CultureInfo ci = CultureInfo.CurrentCulture;
\r
2433 IFormatProvider provider;
\r
2434 if (conversionType == typeof(DateTime)) {
\r
2435 provider = ci.DateTimeFormat;
\r
2438 provider = ci.NumberFormat;
\r
2440 return ToType (value, conversionType, provider);
\r
2443 public static object ChangeType (object value, TypeCode typeCode)
\r
2445 CultureInfo ci = CultureInfo.CurrentCulture;
\r
2446 Type conversionType = conversionTable [(int) typeCode];
\r
2447 IFormatProvider provider;
\r
2448 if (conversionType == typeof(DateTime)) {
\r
2449 provider = ci.DateTimeFormat;
\r
2452 provider = ci.NumberFormat;
\r
2454 return ToType (value, conversionType, provider);
\r
2457 public static object ChangeType (object value, Type conversionType, IFormatProvider provider)
\r
2459 if ((value != null) && (conversionType == null))
\r
2460 throw new ArgumentNullException ("conversionType");
\r
2461 return ToType (value, conversionType, provider);
\r
2464 public static object ChangeType (object value, TypeCode typeCode, IFormatProvider provider)
\r
2466 Type conversionType = conversionTable [(int)typeCode];
\r
2467 return ToType (value, conversionType, provider);
\r
2470 private static bool NotValidBase (int value)
\r
2472 if ((value == 2) || (value == 8) ||
\r
2473 (value == 10) || (value == 16))
\r
2479 private static int ConvertFromBase (string value, int fromBase, bool unsigned)
\r
2481 if (NotValidBase (fromBase))
\r
2482 throw new ArgumentException ("fromBase is not valid.");
\r
2483 if (value == null)
\r
2491 int len = value.Length;
\r
2492 bool negative = false;
\r
2494 // special processing for some bases
\r
2495 switch (fromBase) {
\r
2497 if (value.Substring (i, 1) == "-") {
\r
2499 throw new OverflowException (
\r
2500 Locale.GetText ("The string was being parsed as"
\r
2501 + " an unsigned number and could not have a"
\r
2502 + " negative sign."));
\r
2509 if (value.Substring (i, 1) == "-") {
\r
2510 throw new ArgumentException ("String cannot contain a "
\r
2511 + "minus sign if the base is not 10.");
\r
2513 if (len >= i + 2) {
\r
2515 if ((value[i] == '0') && ((value [i+1] == 'x') || (value [i+1] == 'X'))) {
\r
2521 if (value.Substring (i, 1) == "-") {
\r
2522 throw new ArgumentException ("String cannot contain a "
\r
2523 + "minus sign if the base is not 10.");
\r
2529 throw new FormatException ("Could not find any parsable digits.");
\r
2532 if (value[i] == '+') {
\r
2537 char c = value[i++];
\r
2538 if (Char.IsNumber (c)) {
\r
2539 digitValue = c - '0';
\r
2540 } else if (Char.IsLetter (c)) {
\r
2541 digitValue = Char.ToLowerInvariant (c) - 'a' + 10;
\r
2544 throw new FormatException ("Additional unparsable "
\r
2545 + "characters are at the end of the string.");
\r
2547 throw new FormatException ("Could not find any parsable"
\r
2552 if (digitValue >= fromBase) {
\r
2554 throw new FormatException ("Additional unparsable "
\r
2555 + "characters are at the end of the string.");
\r
2557 throw new FormatException ("Could not find any parsable"
\r
2562 result = (fromBase) * result + digitValue;
\r
2567 throw new FormatException ("Could not find any parsable digits.");
\r
2575 // note: this has nothing to do with base64 encoding (just base and Int64)
\r
2576 private static long ConvertFromBase64 (string value, int fromBase, bool unsigned)
\r
2578 if (NotValidBase (fromBase))
\r
2579 throw new ArgumentException ("fromBase is not valid.");
\r
2580 if (value == null)
\r
2584 int digitValue = -1;
\r
2586 bool negative = false;
\r
2589 int len = value.Length;
\r
2591 // special processing for some bases
\r
2592 switch (fromBase) {
\r
2594 if (value.Substring(i, 1) == "-") {
\r
2596 throw new OverflowException (
\r
2597 Locale.GetText ("The string was being parsed as"
\r
2598 + " an unsigned number and could not have a"
\r
2599 + " negative sign."));
\r
2606 if (value.Substring (i, 1) == "-") {
\r
2607 throw new ArgumentException ("String cannot contain a "
\r
2608 + "minus sign if the base is not 10.");
\r
2610 if (len >= i + 2) {
\r
2612 if ((value[i] == '0') && ((value[i + 1] == 'x') || (value[i + 1] == 'X'))) {
\r
2618 if (value.Substring (i, 1) == "-") {
\r
2619 throw new ArgumentException ("String cannot contain a "
\r
2620 + "minus sign if the base is not 10.");
\r
2626 throw new FormatException ("Could not find any parsable digits.");
\r
2629 if (value[i] == '+') {
\r
2634 char c = value[i++];
\r
2635 if (Char.IsNumber (c)) {
\r
2636 digitValue = c - '0';
\r
2637 } else if (Char.IsLetter (c)) {
\r
2638 digitValue = Char.ToLowerInvariant (c) - 'a' + 10;
\r
2641 throw new FormatException ("Additional unparsable "
\r
2642 + "characters are at the end of the string.");
\r
2644 throw new FormatException ("Could not find any parsable"
\r
2649 if (digitValue >= fromBase) {
\r
2651 throw new FormatException ("Additional unparsable "
\r
2652 + "characters are at the end of the string.");
\r
2654 throw new FormatException ("Could not find any parsable"
\r
2659 result = (fromBase * result + digitValue);
\r
2664 throw new FormatException ("Could not find any parsable digits.");
\r
2667 return -1 * result;
\r
2672 private static void EndianSwap (ref byte[] value)
\r
2674 byte[] buf = new byte[value.Length];
\r
2675 for (int i = 0; i < value.Length; i++)
\r
2676 buf[i] = value[value.Length-1-i];
\r
2680 private static string ConvertToBase2 (byte[] value)
\r
2682 if (!BitConverter.IsLittleEndian)
\r
2683 EndianSwap (ref value);
\r
2684 StringBuilder sb = new StringBuilder ();
\r
2685 for (int i = value.Length - 1; i >= 0; i--) {
\r
2686 byte b = value [i];
\r
2687 for (int j = 0; j < 8; j++) {
\r
2688 if ((b & 0x80) == 0x80) {
\r
2692 if (sb.Length > 0)
\r
2698 return sb.ToString ();
\r
2701 private static string ConvertToBase8 (byte[] value)
\r
2704 switch (value.Length) {
\r
2706 l = (ulong) value [0];
\r
2709 l = (ulong) BitConverter.ToUInt16 (value, 0);
\r
2712 l = (ulong) BitConverter.ToUInt32 (value, 0);
\r
2715 l = BitConverter.ToUInt64 (value, 0);
\r
2718 throw new ArgumentException ("value");
\r
2721 StringBuilder sb = new StringBuilder ();
\r
2722 for (int i = 21; i >= 0; i--) {
\r
2723 // 3 bits at the time
\r
2724 char val = (char) ((l >> i * 3) & 0x7);
\r
2725 if ((val != 0) || (sb.Length > 0)) {
\r
2730 return sb.ToString ();
\r
2733 private static string ConvertToBase16 (byte[] value)
\r
2735 if (!BitConverter.IsLittleEndian)
\r
2736 EndianSwap (ref value);
\r
2737 StringBuilder sb = new StringBuilder ();
\r
2738 for (int i = value.Length - 1; i >= 0; i--) {
\r
2739 char high = (char)((value[i] >> 4) & 0x0f);
\r
2740 if ((high != 0) || (sb.Length > 0)) {
\r
2744 high -= (char) 10;
\r
2750 char low = (char)(value[i] & 0x0f);
\r
2751 if ((low != 0) || (sb.Length > 0)) {
\r
2761 return sb.ToString ();
\r
2764 // Lookup table for the conversion ToType method. Order
\r
2765 // is important! Used by ToType for comparing the target
\r
2766 // type, and uses hardcoded array indexes.
\r
2767 private static readonly Type[] conversionTable = {
\r
2768 // Valid ICovnertible Types
\r
2770 typeof (object), // 1 TypeCode.Object
\r
2771 typeof (DBNull), // 2 TypeCode.DBNull
\r
2772 typeof (Boolean), // 3 TypeCode.Boolean
\r
2773 typeof (Char), // 4 TypeCode.Char
\r
2774 typeof (SByte), // 5 TypeCode.SByte
\r
2775 typeof (Byte), // 6 TypeCode.Byte
\r
2776 typeof (Int16), // 7 TypeCode.Int16
\r
2777 typeof (UInt16), // 8 TypeCode.UInt16
\r
2778 typeof (Int32), // 9 TypeCode.Int32
\r
2779 typeof (UInt32), // 10 TypeCode.UInt32
\r
2780 typeof (Int64), // 11 TypeCode.Int64
\r
2781 typeof (UInt64), // 12 TypeCode.UInt64
\r
2782 typeof (Single), // 13 TypeCode.Single
\r
2783 typeof (Double), // 14 TypeCode.Double
\r
2784 typeof (Decimal), // 15 TypeCode.Decimal
\r
2785 typeof (DateTime), // 16 TypeCode.DateTime
\r
2787 typeof (String), // 18 TypeCode.String
\r
2790 // Function to convert an object to another type and return
\r
2791 // it as an object. In place for the core data types to use
\r
2792 // when implementing IConvertible. Uses hardcoded indexes in
\r
2793 // the conversionTypes array, so if modify carefully.
\r
2794 internal static object ToType (object value, Type conversionType,
\r
2795 IFormatProvider provider)
\r
2797 if (value == null) {
\r
2798 if ((conversionType != null) && conversionType.IsValueType){
\r
2800 throw new InvalidCastException ("Null object can not be converted to a value type.");
\r
2803 // Bug compatibility with 1.0
\r
2805 throw new NullReferenceException ("Null object can not be converted to a value type.");
\r
2811 if (conversionType == null)
\r
2812 throw new InvalidCastException ("Cannot cast to destination type.");
\r
2814 if (value.GetType () == conversionType)
\r
2817 if (value is IConvertible) {
\r
2818 IConvertible convertValue = (IConvertible) value;
\r
2820 if (conversionType == conversionTable[0]) // 0 Empty
\r
2821 throw new ArgumentNullException ();
\r
2823 else if (conversionType == conversionTable[1]) // 1 TypeCode.Object
\r
2824 return (object) value;
\r
2826 else if (conversionType == conversionTable[2]) // 2 TypeCode.DBNull
\r
2827 throw new InvalidCastException (
\r
2828 "Cannot cast to DBNull, it's not IConvertible");
\r
2830 else if (conversionType == conversionTable[3]) // 3 TypeCode.Boolean
\r
2831 return (object) convertValue.ToBoolean (provider);
\r
2833 else if (conversionType == conversionTable[4]) // 4 TypeCode.Char
\r
2834 return (object) convertValue.ToChar (provider);
\r
2836 else if (conversionType == conversionTable[5]) // 5 TypeCode.SByte
\r
2837 return (object) convertValue.ToSByte (provider);
\r
2839 else if (conversionType == conversionTable[6]) // 6 TypeCode.Byte
\r
2840 return (object) convertValue.ToByte (provider);
\r
2842 else if (conversionType == conversionTable[7]) // 7 TypeCode.Int16
\r
2843 return (object) convertValue.ToInt16 (provider);
\r
2845 else if (conversionType == conversionTable[8]) // 8 TypeCode.UInt16
\r
2846 return (object) convertValue.ToUInt16 (provider);
\r
2848 else if (conversionType == conversionTable[9]) // 9 TypeCode.Int32
\r
2849 return (object) convertValue.ToInt32 (provider);
\r
2851 else if (conversionType == conversionTable[10]) // 10 TypeCode.UInt32
\r
2852 return (object) convertValue.ToUInt32 (provider);
\r
2854 else if (conversionType == conversionTable[11]) // 11 TypeCode.Int64
\r
2855 return (object) convertValue.ToInt64 (provider);
\r
2857 else if (conversionType == conversionTable[12]) // 12 TypeCode.UInt64
\r
2858 return (object) convertValue.ToUInt64 (provider);
\r
2860 else if (conversionType == conversionTable[13]) // 13 TypeCode.Single
\r
2861 return (object) convertValue.ToSingle (provider);
\r
2863 else if (conversionType == conversionTable[14]) // 14 TypeCode.Double
\r
2864 return (object) convertValue.ToDouble (provider);
\r
2866 else if (conversionType == conversionTable[15]) // 15 TypeCode.Decimal
\r
2867 return (object) convertValue.ToDecimal (provider);
\r
2869 else if (conversionType == conversionTable[16]) // 16 TypeCode.DateTime
\r
2870 return (object) convertValue.ToDateTime (provider);
\r
2872 else if (conversionType == conversionTable[18]) // 18 TypeCode.String
\r
2873 return (object) convertValue.ToString (provider);
\r
2875 throw new InvalidCastException (Locale.GetText ("Unknown target conversion type"));
\r
2878 // Not in the conversion table
\r
2879 throw new InvalidCastException ((Locale.GetText (
\r
2880 "Value is not a convertible object: " + value.GetType().ToString() + " to " + conversionType.FullName)));
\r