C#转换类介绍和示例

2021年5月9日16:44:06 发表评论 686 次浏览

Convert类提供了将基本数据类型转换为另一种基本数据类型的不同方法。 Convert类支持的基本类型为Boolean, Char, SByte, Byte, Int16, Int32, Int64, UInt16, UInt32, UInt64, Single, Double, Decimal, DateTime和String。它还提供了支持其他转换的方法。该类的定义如下系统命名空间。

转换类的特征:

  • 它提供了用于将每个基本类型转换为每个其他基本类型的方法。
  • 它提供了用于将整数值转换为非十进制字符串表示形式的方法, 还提供了将表示非十进制数字的字符串转换为整数值的方法。
  • 它提供了用于将任何自定义对象转换为任何基本类型的方法。
  • 它提供了一组支持base64编码的方法。
  • 如果缩小的转换导致数据丢失, 则可能发生OverFlowException。

领域:

  • DBNull:它是一个常量, 表示缺少数据的数据库列, 即数据库null。

方法

方法 描述
ChangeType() 它返回指定类型的对象, 该对象的值等于指定的对象。
FromBase64CharArray(Char [], Int32, Int32) 将Unicode字符数组的子集转换为等效的8位无符号整数数组, 该子集将二进制数据编码为64位基数。参数指定输入数组中的子集以及要转换的元素数。
FromBase64String(String) 将指定的字符串转换为等效的8位无符号整数数组, 该字符串将二进制数据编码为基数64位。
GetTypeCode(对象) 返回指定对象的TypeCode。
IsDBNull(对象) 返回指定对象是否为DBNull类型的指示。
ToBase64CharArray() 将8位无符号整数数组的子集转换为以64位为底的数字编码的Unicode字符数组的等效子集。
ToBase64String() 将8位无符号整数数组的值转换为以base-64数字编码的等效字符串表示形式。
ToBoolean() 将指定的值转换为等效的布尔值。
ToByte() 将指定的值转换为8位无符号整数。
ToChar() 将指定的值转换为Unicode字符。
ToDateTime() 将指定的值转换为DateTime值。
ToDecimal() 将指定的值转换为十进制数。
ToDouble() 将指定的值转换为双精度浮点数。
ToInt16() 将指定的值转换为16位有符号整数。
ToInt32() 将指定的值转换为32位有符号整数。
ToInt64() 将指定的值转换为64位有符号整数。
ToSByte() 将指定值转换为8位带符号整数。
ToSingle() 将指定的值转换为单精度浮点数。
ToUInt16() 将指定的值转换为16位无符号整数。
ToUInt32() 将指定的值转换为32位无符号整数。
ToUInt64() 将指定的值转换为64位无符号整数。

示例1:

//C# program to illustrate the
//use of ToBase64String(Byte[])
//method
using System;
  
class GFG {
  
     //Main method
     static public void Main()
     {
  
         //Creating and initializing 
         //Byte array
         byte [] B = { 2, 4, 8, 16, 32 };
  
         //Display the elements
         Console.WriteLine( "BArray is :{0}" , BitConverter.ToString(B));
  
         Console.WriteLine();
  
         //Convert the given array
         //into a base 64 string.
         String str = Convert.ToBase64String(B);
  
         //Display the string
         Console.WriteLine( "Base 64 string is :{0}" , str);
     }
}

输出如下:

BArray is :02-04-08-10-20

Base 64 string is :AgQIECA=

示例2:

//C# program to illustrate the
//use of ToDecimal(Int16) method
using System;
  
class GFG {
  
     //Main method
     static public void Main()
     {
  
         //Creating and initializing 
         //an array
         short [] ele = {1, Int16.MinValue, -00, 106, -32 };
         decimal sol;
  
         //Display the elements
         Console.WriteLine( "Elements are:" );
         foreach ( short i in ele)
         {
             Console.WriteLine(i);
         }
  
         foreach ( short num in ele)
         {
  
             //Convert the given Int16
             //values into decimal values
             //using ToDecimal(Int16) method
             sol = Convert.ToDecimal(num);
  
             //Display the elements
             Console.WriteLine( "convert value is: {0}" , sol);
         }
     }
}

输出如下:

Elements are:
1
-32768
0
106
-32
convert value is: 1
convert value is: -32768
convert value is: 0
convert value is: 106
convert value is: -32

参考:

  • https://docs.microsoft.com/en-us/dotnet/api/system.convert?view=netframework-4.7.2

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: