C#常用类型转换总结

C#中所有的类型都是一个类,下面是一些常用的类型的类名和对应的类型:

类名 System中相对应的类型
bool System.Boolean (布尔型,其值为 true 或者 false)
char System.Char (字符型,占有两个字节,表示 1 个 Unicode 字符)
byte System.Byte (字节型,占 1 字节,表示 8 位正整数,范围 0 ~ 255)
sbyte System.SByte (带符号字节型,占 1 字节,表示 8 位整数,范围 -128 ~ 127)
ushort System.UInt16 (无符号短整型,占 2 字节,表示 16 位正整数,范围 0 ~ 65,535)
uint System.UInt32 (无符号整型,占 4 字节,表示 32 位正整数,范围 0 ~ 4,294,967,295)
ulong System.UInt64 (无符号长整型,占 8 字节,表示 64 位正整数,范围 0 ~ 大约 10 的 20 次方)
short System.Int16 (短整型,占 2 字节,表示 16 位整数,范围 -32,768 ~ 32,767)
int System.Int32 (整型,占 4 字节,表示 32 位整数,范围 -2,147,483,648 到 2,147,483,647)
long System.Int64 (长整型,占 8 字节,表示 64 位整数,范围大约 -(10 的 19) 次方到 10 的 19 次方)
float System.Single (单精度浮点型,占 4 个字节)
double System.Double (双精度浮点型,占 8 个字节)

1、其他类型转为字符串

bool、byte、char等只需要在变量后面加上ToString(),即type.ToString()

2、强制转换

在变量前面加上类型,例如:

short g = 1;
byte h = (byte) g; // 将 short 型的 g 的值强制转换成byte型后再赋给变量 h

注意:不同类型表示的范围不同,注意转化时候的溢出。

3、int和string的互换(字符串转化为整数:int.Parse(string) )

float f = 12.3f;

string str = "258";

this.textBox1.Text = "";

this.textBox1.AppendText("f = " + f.ToString() + ""n");//float->string

if (int.Parse(str) == 258)   this.textBox1.AppendText("str convert to int successfully.");

else  this.textBox1.AppendText("str convert to int failed.");

4、String和char[]的互换(str.ToCharArray()和string sname = new String(name))

string str = "quzhixun";

char[] chars = str.ToCharArray();//string->char[]

this.textBox1.Text = "";

this.textBox1.AppendText("Length of ""quzhixun"" is " + str.Length + ""n");

this.textBox1.AppendText("Length of char array is " + chars.Length + ""n");

this.textBox1.AppendText("char[2] = " + chars[2] + ""n");

char[] name = { ‘q’, ‘u’, ‘z’, ‘h’, ‘i’, ‘x’, ‘u’,’n’ };

string sname = new String(name);//char[]->string

this.textBox1.AppendText("sname = """ + sname + """"n");

5、tring和byte[]的互换

string s = "hi,屈志勋";

byte[] b1 = System.Text.Encoding.Default.GetBytes(s);//sting->byte[],半个英文1个字节,汉字2 个字节。

byte[] b2 = System.Text.Encoding.Unicode.GetBytes(s); //sting->byte[],都是两个字节。

string t1 = "", t2 = "";

foreach (byte b in b1)   t1 += b.ToString("") + " ";

foreach (byte b in b2)  t2 += b.ToString("") + " ";

this.textBox1.Text = "";

this.textBox1.AppendText("b1.Length = " + b1.Length + ""n");

this.textBox1.AppendText(t1 + ""n");

this.textBox1.AppendText("b2.Length = " + b2.Length + ""n");

this.textBox1.AppendText(t2 + ""n");

byte[] b = { 65, 66, 67 };

string s = System.Text.Encoding.ASCII.GetString(b);//byte[]->string

this.textBox1.AppendText("The string is: " + s + ""n");

6、转换十六进制( a.ToString("x6")

int a = 159357;

this.textBox1.Text = "";

this.textBox1.AppendText("a(10) = " + a.ToString() + ""n");

this.textBox1.AppendText("a(16) = " + a.ToString("x6") + ""n");

this.textBox1.AppendText("a(16) = " + a.ToString("X6") + ""n");

7、DateTime和long类型的互换(DateTime.FromOADate(doubleDate))

double doubleDate = DateTime.Now.ToOADate();//按原来的double值输出,DateTime->long

DateTime theDate = DateTime.FromOADate(doubleDate);//从原来的的double值获得System.DateTime对象,long->DateTime

this.textBox1.Text = "";

this.textBox1.AppendText("Double value of now: " + doubleDate.ToString() + ""n");

this.textBox1.AppendText("DateTime from double value: " + theDate.ToString() + ""n");

//

8、form DateTime

DateTime now = DateTime.Now;

string format;

this.textBox1.Text = "";

format = """year"":yyyy,""month"":MM,""day"":dd HH:mm:ss";

this.textBox1.AppendText(format + ": " + now.ToString(format) + ""n");

format = "yy年M日d日";

this.textBox1.AppendText(format + ": " + now.ToString(format) + ""n");
 

1 评论

发表评论

电子邮件地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据