Browse Source

2002-03-05 Duncan Mak <[email protected]>

	* Convert.cs:
	(DBNull) Added the missing field.
	(IsDBNull) Fixed typo.
	(ToByte (string, int)) Implemented.
	(ToString (byte, int)) Implemented.
	(ConvertToBase)
	(BuildConvertedString) internal functions used for converting values to
	a specific base.

svn path=/trunk/mcs/; revision=2928
Duncan Mak 24 years ago
parent
commit
cbe6d62e7e
2 changed files with 49 additions and 3 deletions
  1. 10 1
      mcs/class/corlib/System/ChangeLog
  2. 39 2
      mcs/class/corlib/System/Convert.cs

+ 10 - 1
mcs/class/corlib/System/ChangeLog

@@ -1,11 +1,20 @@
 2002-03-05  Duncan Mak  <[email protected]>
 
+	* Convert.cs:
+	(DBNull) Added the missing field.
+	(IsDBNull) Fixed typo.
+	(ToByte (string, int)) Implemented.
+	(ToString (byte, int)) Implemented.
+	(ConvertToBase)
+	(BuildConvertedString) internal functions used for converting values to
+	a specific base.
+
 	* Int16.cs: 
 	* Int32.cs:
 	* Int64.cs:
 	* Single.cs:
 	* UInt16.cs: 
-	* UInt32.cs: Implement the IConvertible interface.	
+	* UInt32.cs: Implemented the IConvertible interface.	
 
 	* CharEnumerator.cs: Renamed to variables to be clearer and
 	changed some of the tests to conform to the 1.0 spec.

+ 39 - 2
mcs/class/corlib/System/Convert.cs

@@ -75,6 +75,9 @@ namespace System {
   
 	[CLSCompliant(false)]
 	public sealed class Convert {
+
+		// Fields
+		public static readonly object DBNull;
 	
 		// ========== BASE 64 Conversions ========== //
 		// the BASE64 convert methods are using the Base64 converting methods
@@ -116,7 +119,7 @@ namespace System {
 				return Type.GetTypeCode (value.GetType ());
 		}
 
-		public static bool ISDBNull (object value)
+		public static bool IsDBNull (object value)
 		{
 			TypeCode tc = Type.GetTypeCode (value.GetType ());
 			
@@ -377,6 +380,11 @@ namespace System {
 		{
 			return Byte.Parse (value, provider);
 		}
+
+		public static byte ToByte (string value, int fromBase)
+		{
+			return (byte) ConvertFromBase (value, fromBase);
+		}
 	
 		public static byte ToByte (uint value) 
 		{ 
@@ -1511,6 +1519,14 @@ namespace System {
 			return value.ToString (provider); 
 		}
 
+		public static string ToString (byte value, int toBase)
+		{
+			if (NotValidBase (toBase))
+				throw new ArgumentException ("toBase is not valid.");
+			
+			return ConvertToBase ((int) value, int toBase);
+		}
+
 		public static string ToString (char value) 
 		{ 
 			return value.ToString (); 
@@ -1568,7 +1584,8 @@ namespace System {
 
 		public static string ToString (int value, int fromBase)
 		{
-			return (ConvertFromBase (value.ToString (), fromBase)).ToString ();
+			int retVal = ConvertFromBase (value.ToString (), fromBase);
+			return retVal.ToString ();
 		}
 
 		public static string ToString (int value, IFormatProvider provider) 
@@ -2180,6 +2197,26 @@ namespace System {
 			return result;
 		}
 
+		private static string ConvertToBase (value, toBase)
+		{
+			StringBuilder sb = new StringBuilder ();
+			BuildConvertedString (sb, value, toBase);
+			return sb.ToString ();
+		}
+
+		internal static void BuildConvertedString (StringBuilder sb, int value, int toBase)
+		{
+			int divided = value / toBase;
+			int reminder = value % toBase;		
+
+			if (divided > 0)
+				SBFromBase (sb, divided, toBase);
+		
+			if (reminder >= 10)
+				sb.Append ((char) (reminder + 'a' - 10));
+			else
+				sb.Append ((char) (reminder + '0'));
+		}
                 // Lookup table for the conversion ToType method. Order
 		// is important! Used by ToType for comparing the target
 		// type, and uses hardcoded array indexes.