云南天气免费注册登陆发布文章  
网页教程   |   平面教程   |   网络编程   |   数据库   |   服务器
您所在的位置:云港首页 >> 站长学院 >> 网络编程 >> 正文
解析C#数据类型之间转换问题
文章来源:本站收集 责任编辑:yn47.com 加入时间:2008-4-6 字体显示:
  在装箱,拆箱过程中,首先涉及到的是数据的转换。发下午几个小时,研究了C#中数据类型之间的转换涉及的一些问题,希望大家给予意见!
  1、数据类型的类名
  这里讲的数据的类名指的是:Sytem.data.DbType对应的类型,我是这样理解的。
  类名System中相对应的类型
  boolSystem.Boolean(布尔型,其值为true或者false)
  charSystem.Char(字符型,占有两个字节,表示1个Unicode字符)
  byteSystem.Byte(字节型,占1字节,表示8位正整数,范围0~255)
  sbyteSystem.SByte(带符号字节型,占1字节,表示8位整数,范围-128~127)
  ushortSystem.UInt16(无符号短整型,占2字节,表示16位正整数,范围0~65,535)
  uintSystem.UInt32(无符号整型,占4字节,表示32位正整数,范围0~4,294,967,295)
  ulongSystem.UInt64(无符号长整型,占8字节,表示64位正整数,范围0~大约10的20次方)
  shortSystem.Int16(短整型,占2字节,表示16位整数,范围-32,768~32,767)
  intSystem.Int32(整型,占4字节,表示32位整数,范围-2,147,483,648到2,147,483,647)
  longSystem.Int64(长整型,占8字节,表示64位整数,范围大约-(10的19)次方到10的19次方)
  floatSystem.Single(单精度浮点型,占4个字节)
  doubleSystem.Double(双精度浮点型,占8个字节)
  代码如下:
  usingSystem;
  usingSystem.Collections.Generic;
  usingSystem.ComponentModel;
  usingSystem.Data;
  usingSystem.Drawing;
  usingSystem.Text;
  usingSystem.Windows.Forms;
  namespaceTypeTest
  {
  publicpartialclassForm1:Form
  {
  publicForm1()
  {
  InitializeComponent();
  }
  privatevoidForm1_Load(objectsender,EventArgse)
  {
  boolbo=true;
  byteb=9;
  charc='a';
  sbytesb=8;
  shorts=8;
  inti=7;
  uintu=6;
  longl=5;
  this.textBox1.Text="typetest";
  this.textBox1.AppendText("bool->"+bo.GetType().FullName+""n");
  this.textBox1.AppendText("byte->"+b.GetType().FullName+""n");
  this.textBox1.AppendText("char->"+c.GetType().FullName+""n");
  this.textBox1.AppendText("sbyte->"+sb.GetType().FullName+""n");
  this.textBox1.AppendText("short->"+s.GetType().FullName+""n");
  this.textBox1.AppendText("int->"+i.GetType().FullName+""n");
  this.textBox1.AppendText("uint->"+u.GetType().FullName+""n");
  this.textBox1.AppendText("long->"+l.GetType().FullName+""n");
  //其实类型就不写上,自己可以真接加上去!
  }
  }
  }
  结果可看到:
  typetestbool->System.Boolean
  byte->System.Byte
  char->System.Char
  sbyte->System.SByte
  short->System.Int16
  int->System.Int32
  uint->System.UInt32
  long->System.Int64
  //
  说明,以后所编的代码都是写在privatevoidForm1_Load(objectsender,EventArgse)中的
  2、ValueType间的转换。
  boolbo=true;
  byteb=9;
  charc='a';
  sbytesb=8;
  shorts=8;
  inti=7;
  uintu=6;
  longl=5;
  this.textBox1.Text="datatype";
  this.textBox1.AppendText("boolbo="+bo.ToString()+""n");
  this.textBox1.AppendText("byteb="+b.ToString()+""n");
  this.textBox1.AppendText("charc="+c.ToString()+""n");
  this.textBox1.AppendText("sbytesb="+sb.ToString()+""n");
  this.textBox1.AppendText("shorts="+s.ToString()+""n");
  this.textBox1.AppendText("inti="+i.ToString()+""n");
  this.textBox1.AppendText("uintu="+u.ToString()+""n");
  this.textBox1.AppendText("longl="+l.ToString()+""n");
  此段代码并没有转换数据类型,只说明它们的类型公别还是System.bool型…System.long型。
  追加一行:intg=1;
  shorth=g;
  this.textBox1.AppendText("h="+h.ToString()+""n");
  结果编译报错:
  G:"Projects"VisualC#"Convert"Form1.cs(118):无法将类型“int”隐式转换为“short”
  数据要进行强制转换。
  如上例修改如下:
  shortg=1;
  byteh=(byte)g;//将short型的g的值强制转换成byte型后再赋给变量h
  this.textBox1.AppendText("h="+h.ToString()+""n");
  就可以了!
  Short->byte
  shortg=265;//265=255+10
  byteh=(byte)g;
  this.textBox1.AppendText("h="+h.ToString()+""n");
  注意:溢出问题!
  3、ASCII<->Unicode
  charch='a';
  shortii=65;
  this.textBox1.Text="";
  this.textBox1.AppendText("TheASCIIcodeof"'"+ch+""'is:"+(short)ch+""n");
  this.textBox1.AppendText("ASCIIis"+ii.ToString()+",thecharis:"+(char)ii+""n");
  charname1='屈';
  charname2='志';
  shortname3=21195;
  this.textBox1.AppendText("TheUnicodeof"'"+name1+""'is:"+(short)name1+""n");
  this.textBox1.AppendText("TheUnicodeof"'"+name2+""'is:"+(short)name2+""n");
  this.textBox1.AppendText("Unicodeis"+name3.ToString()+",thename3is:"+(char)name3+""n");
  它的运行结果是
  TheASCIIcodeof'a'is:97
  ASCIIis65,thecharis:A
  TheUnicodeof'屈'is:23624
  TheUnicodeof'志'is:24535
  Unicodeis21195,thename3is:勋
  4、int<->string
  floatf=12.3f;
  stringstr="258";
  this.textBox1.Text="";
  this.textBox1.AppendText("f="+f.ToString()+""n");//float->string
  if(int.Parse(str)==258)//string->int
  {
  this.textBox1.AppendText("strconverttointsuccessfully.");
  }
  else
  {
  this.textBox1.AppendText("strconverttointfailed.");
  5、String<->char[]
  stringstr="quzhixun";
  char[]chars=str.ToCharArray();//string->char[]
  this.textBox1.Text="";
  this.textBox1.AppendText("Lengthof""quzhixun""is"+str.Length+""n");
  this.textBox1.AppendText("Lengthofchararrayis"+chars.Length+""n");
  this.textBox1.AppendText("char[2]="+chars[2]+""n");
  char[]name={'q','u','z','h','i','x','u','n'};
  stringsname=newString(name);//char[]->string
  this.textBox1.AppendText("sname="""+sname+""""n");
  6、tring<->byte[]
  strings="hi,屈志勋";
  byte[]b1=System.Text.Encoding.Default.GetBytes(s);//sting->byte[],半个英文1个字节,汉字2个字节。
  byte[]b2=System.Text.Encoding.Unicode.GetBytes(s);//sting->byte[],都是两个字节。
  stringt1="",t2="";
  foreach(bytebinb1)
  {
  t1+=b.ToString("")+"";
  }
  foreach(bytebinb2)
  {
  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};
  strings=System.Text.Encoding.ASCII.GetString(b);//byte[]->string
  this.textBox1.AppendText("Thestringis:"+s+""n");
  //
  7、转换十六进制
  inta=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");
  8、DateTime<->long
  doubledoubleDate=DateTime.Now.ToOADate();//按原来的double值输出,DateTime->long
  DateTimetheDate=DateTime.FromOADate(doubleDate);//从原来的的double值获得System.DateTime对象,long->DateTime
  this.textBox1.Text="";
  this.textBox1.AppendText("Doublevalueofnow:"+doubleDate.ToString()+""n");
  this.textBox1.AppendText("DateTimefromdoublevalue:"+theDate.ToString()+""n");
  //
  9、formDateTime
  DateTimenow=DateTime.Now;
  stringformat;
  this.textBox1.Text="";
  format="""year"":yyyy,""month"":MM,""day"":ddHH:mm:ss";
  this.textBox1.AppendText(format+":"+now.ToString(format)+""n");
  format="yy年M日d日";
  this.textBox1.AppendText(format+":"+now.ToString(format)+""n");
返回栏目首页】 【打印此页】 【发表评论】 【发布文章】 【关闭此页
返回云南设计港首页
发表评论 查看评论
您的昵称: 1、遵守中华人民共和国有关法律、法规,尊重网上道德,承担一切因您的行为而直接或间接引起的法律责任。
2、本网拥有管理笔名和留言的一切权力。
3、您在本站留言板发表的言论,本网有权在网站内转载或引用。
4、如您对管理有意见请向留言板管理员或本网反映。
评论内容:
验 证 码: 看不清,请刷新验证码
相关文章
热点推荐
站长在线
网站运营
站长休闲
版权所有 云南设计港 © all Rights Reserved.  为了更好的浏览,建议使用分辨率:1024×768和iE6.0以上的浏览器浏览本网站
  滇icP备06002874号