vc常用数据类型使用转换详解.htm

来自「VC常用数据类型」· HTM 代码 · 共 664 行 · 第 1/3 页

HTM
664
字号
      str("aaaaaa");<BR>strncpy(a,(LPCTSTR)str,sizeof(a));<BR>或者如下:<BR>strncpy(a,str,sizeof(a));<BR>以上两种用法都是正确地. 
      因为strncpy的第二个参数类型为const char *.所以编译器会自动将CString类转换成const char *.</P>
      <P><FONT face=Verdana size=3><STRONG>CString转LPCTSTR (const char 
      *)<BR></STRONG></FONT>CString cStr;<BR>const char 
      *lpctStr=(LPCTSTR)cStr;</P>
      <P><STRONG><FONT face=Verdana 
      size=3>LPCTSTR转CString</FONT></STRONG><BR>LPCTSTR lpctStr;<BR>CString 
      cStr=lpctStr;</P>
      <P><STRONG><FONT face=Verdana 
      size=3>将char*类型的变量赋给CString型的变量</FONT></STRONG><BR>可以直接赋值,如: <BR>CString 
      myString = "This is a test"; <BR>也可以利用构造函数,如: <BR>CString s1("Tom");</P>
      <P><FONT face=Verdana size=3><STRONG>将CString类型的变量赋给char 
      []类型(字符串)的变量<BR></STRONG></FONT>1、sprintf()函数<BR>CString str = 
      "good";<BR>char tmp[200] ;<BR>sprintf(tmp, 
      "%s",(LPCSTR)str);&nbsp;&nbsp;<BR>(LPCSTR)str这种强制转换相当于(LPTSTR)(LPCTSTR)str 
      <BR>CString类的变量需要转换为(char*)的时,使用(LPTSTR)(LPCTSTR)str </P>
      <P>然而,LPCTSTR是const char 
      *,也就是说,得到的字符串是不可写的!将其强制转换成LPTSTR去掉const,是极为危险的!<BR>一不留神就会完蛋!要得到char 
      *,应该用GetBuffer()或GetBufferSetLength(),用完后再调用ReleaseBuffer()。</P>
      <P>2、strcpy()函数<BR>CString str;<BR>char c[256];<BR>strcpy(c, str); </P>
      <P>char mychar[1024];<BR>CString 
      source="Hello";<BR>strcpy((char*)&amp;mychar,(LPCTSTR)source); </P>
      <P><BR><STRONG><FONT face=Verdana 
      size=3>关于CString的使用<BR></FONT></STRONG>1、指定 CString 
      形参<BR>&nbsp;&nbsp;&nbsp;&nbsp; 对于大多数需要字符串参数的函数,最好将函数原型中的形参指定为一个指向字符 
      (LPCTSTR) 而非 CString 的 const 指针。<BR>当将形参指定为指向字符的 const 指针时,可将指针传递到 TCHAR 
      数组(如字符串 ["hi there"])或传递到 CString 对象。<BR>CString 对象将自动转换成 LPCTSTR。任何能够使用 
      LPCTSTR 的地方也能够使用 CString 对象。</P>
      <P>2、如果某个形参将不会被修改,则也将该参数指定为常数字符串引用(即 const 
      CString&amp;)。如果函数要修改该字符串,<BR>则删除 const 修饰符。如果需要默认为空值,则将其初始化为空字符串 
      [""],如下所示:<BR>void AddCustomer( const CString&amp; name, const 
      CString&amp; address, const CString&amp; comment = "" ); </P>
      <P>3、对于大多数函数结果,按值返回 CString 对象即可。</P>
      <P><BR><STRONG><FONT face=Verdana 
      size=4>串的基本运算</FONT></STRONG><BR>&nbsp;&nbsp;&nbsp;&nbsp; 
      对于串的基本运算,很多高级语言均提供了相应的运算符或标准的库函数来实现。<BR>为叙述方便,先定义几个相关的变量:<BR>&nbsp;&nbsp;&nbsp;&nbsp; 
      char 
      s1[20]="dir/bin/appl",s2[20]="file.asm",s3[30],*p;<BR>&nbsp;&nbsp;&nbsp;&nbsp; 
      int result;<BR>&nbsp;&nbsp;&nbsp;&nbsp; 下面以C语言中串运算介绍串的基本运算 
      <BR>1、求串长<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int 
      strlen(char *s);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      //求串s的长度<BR>&nbsp;&nbsp;&nbsp;&nbsp; 
      【例】printf("%d",strlen(s1));&nbsp;&nbsp;&nbsp;&nbsp; //输出s1的串长12</P>
      <P>2、串复制<BR>&nbsp;&nbsp;&nbsp;&nbsp; char *strcpy(char 
      *to,*from);//将from串复制到to串中,并返回to开始处指针<BR>&nbsp;&nbsp;&nbsp;&nbsp; 
      【例】strcpy(s3,s1);&nbsp;&nbsp; //s3="dir/bin/appl",s1串不变</P>
      <P><BR>3、联接<BR>&nbsp;&nbsp;&nbsp;&nbsp; char *strcat(char *to,char 
      *from);//将from串复制到to串的末尾,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      //并返回to串开始处的指针<BR>&nbsp;&nbsp;&nbsp;&nbsp; 
      【例】strcat(s3,"/");&nbsp;&nbsp;&nbsp;&nbsp; 
      //s3="dir/bin/appl/"<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      strcat(s3,s2);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      //s3="dir/bin/appl/file.asm"</P>
      <P>4、串比较<BR>&nbsp;&nbsp;&nbsp;&nbsp; int strcmp(char *s1,char 
      *s2);//比较s1和s2的大小,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      //当s1&lt;s2、s1&gt;s2和s1=s2时,分别返回小于0、大于0和等于0的值 <BR>&nbsp;&nbsp;&nbsp;&nbsp; 
      【例】result=strcmp("baker","Baker");&nbsp;&nbsp;&nbsp;&nbsp; 
      //result&gt;0<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      result=strcmp("12","12");&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      //result=0<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      result=strcmp("Joe","joseph")&nbsp;&nbsp;&nbsp; //result&lt;0</P>
      <P>5、字符定位<BR>&nbsp;&nbsp;&nbsp;&nbsp; char *strchr(char *s,char 
      c);//找c在字符串s中第一次出现的位置,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      //若找到,则返回该位置,否则返回NULL<BR>&nbsp;&nbsp;&nbsp;&nbsp; 
      【例】p=strchr(s2,'.');&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      //p指向"file"之后的位置<BR>     if(p) 
      strcpy(p,".cpp");&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //s2="file.cpp" </P>
      <P>&nbsp;&nbsp; 注意:<BR>&nbsp;&nbsp;&nbsp;&nbsp;  ①上述操作是最基本的,其中后 
      4个操作还有变种形式:strncpy,strncath和strnchr。<BR>&nbsp;&nbsp;&nbsp;&nbsp; 
       ②其它的串操作见C的&lt;string.h&gt;。在不同的高级语言中,对串运算的种类及符号都不尽相同<BR>&nbsp;&nbsp;&nbsp;&nbsp; 
       ③其余的串操作一般可由这些基本操作组合而成</P>
      <P>&nbsp;&nbsp;&nbsp;&nbsp; 【例】求子串的操作可如下实现:<BR>&nbsp;&nbsp;&nbsp;&nbsp; 
      void substr(char *sub,char *s,int pos,int 
      len){<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      //s和sub是字符数组,用sub返回串s的第pos个字符起长度为len的子串<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      //其中0&lt;=pos&lt;=strlen(s)-1,且数组sub至少可容纳len+1个字符。<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      if 
      (pos&lt;0||pos&gt;strlen(s)-1||len&lt;0)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      Error("parameter 
      error!");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      strncpy(sub,&amp;s[pos],len);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      //从s[pos]起复制至多len个字符到sub<BR></P></DIV></TD></TR></TBODY></TABLE><BR>
<DIV class=opt><A title=查看该分类中所有文章 
href="http://hi.baidu.com/superdbs/blog/category/vc%20&Ntilde;§&Iuml;°">类别:vc 学习</A> | <A 
title=将此文章添加到百度搜藏 
onclick="window.open('http://cang.baidu.com/do/add?it='+encodeURIComponent('VC常用数据类型使用转换详解[转]'+'_百度空间')+'&amp;iu='+encodeURIComponent(location.href)+'&amp;fr=sp#nw=1','_s','scrollbars=no,width=600,height=450,right=75,top=20,status=no,resizable=yes'); return false;" 
href="http://cang.baidu.com/do/add" target=_blank>添加到搜藏</A> | 浏览(<SPAN 
id=result></SPAN>) </DIV>
<DIV class=line></DIV>
<SCRIPT language=JavaScript>
allkey=allkey+"2f96e7115aa476c6a7ef3ff3_18500ef499893e6fddc474cd_";
</SCRIPT>

<DIV id=in_comment><A name=comment></A>
<DIV class=tit>网友评论:</DIV>
<SCRIPT>
function writecmt(type,id,cmtname,cmturl,cmttime){
	var html1="";
	if(type==1){
		if(cmturl==""){
			html1="<a name='"+id+"'>"+cmtname+"</a> - <span class='date'>"+cmttime+"</span> ";
		}else{
			html1="<a name='"+id+"' href='"+cmturl+"' target='_blank' title='"+cmturl+"'>"+cmtname+"</a> - <span class='date'>"+cmttime+"</span> ";
		}
	}else{
		if(cmtname=="匿名网友"){
			if(cmturl==""){
				html1="<a name='"+id+"'>"+cmtname+"</a> - <span class='date'>"+cmttime+"</span> ";
			}else{
				html1="<a name='"+id+"' href='"+cmturl+"' target='_blank' title='"+cmturl+"'>"+cmtname+"</a> - <span class='date'>"+cmttime+"</span>";
			}
		}else{
			if(cmturl==""){
				html1="<div class='f14' style='display:inline'>网友:<a name='"+id+"'>"+cmtname+"</a> - <span class=\"date\">"+cmttime+"</span></div>";
			}else{
				html1="<div class='f14' style='display:inline'>网友:<a name='"+id+"' href='"+cmturl+"' target='_blank' title='"+cmturl+"'>"+cmtname+"</a> - <span class=\"date\">"+cmttime+"</span></div>";
			}
		}
	}
	document.write(html1);
}

</SCRIPT>

<DIV id=page></DIV></DIV>
<DIV id=in_send>
<FORM id=popFormSubmit name=form1 onsubmit="return checkcmtform()" 
action=/superdbs/commit method=post><INPUT type=hidden value=8 name=ct> <INPUT 
type=hidden value=1 name=cm> <INPUT type=hidden value=18500ef499893e6fddc474cd 
name=spBlogID>
<SCRIPT language=JavaScript>
	document.write("<input type='hidden' name='spRefURL' value='"+window.location.href+"'>");
</SCRIPT>
 
<DIV class=tit>发表评论:</DIV>
<TABLE cellSpacing=5 cellPadding=0 width=620 border=0>
  <TBODY>
  <TR>
    <TD class=f14>姓 名:</TD>
    <TD><INPUT id=spBlogCmtor style="WIDTH: 220px" onfocus=hidErr(1); 
      maxLength=49 onchange="checkname('spBlogCmtor')" name=spBlogCmtor>
      <DIV id=nmerror style="DISPLAY: none">*姓名最长为50字节</DIV></TD></TR>
  <TR id=1_err style="DISPLAY: none">
    <TD>&nbsp;</TD>
    <TD>
      <DIV class=error id=1_err_con></DIV></TD></TR>
  <TR>
    <TD class=f14>网址或邮箱:</TD>
    <TD><INPUT id=spBlogCmtURL style="WIDTH: 360px" onfocus=hidErr(2); 
      maxLength=128 onchange="checkeandu('spBlogCmtURL')" name=spBlogCmtURL> 
    (选填)</TD>
    <SCRIPT>
G("spBlogCmtor").value="";
G("spBlogCmtURL").value="";
</SCRIPT>
  </TR>
  <TR id=2_err style="DISPLAY: none">
    <TD>&nbsp;</TD>
    <TD>
      <DIV class=error id=2_err_con></DIV></TD></TR>
  <TR>
    <TD class=f14 vAlign=top>内 容:</TD>
    <TD><TEXTAREA id=spBlogCmtText style="WIDTH: 520px; HEIGHT: 155px" onfocus=showverkey();hidErr(3); name=spBlogCmtText></TEXTAREA>
      <SCRIPT>
G("spBlogCmtor").value=G("spBlogCmtor").defaultValue;
G("spBlogCmtText").value="";
</SCRIPT>
       </TD></TR>
  <TR id=3_err style="DISPLAY: none">
    <TD>&nbsp;</TD>
    <TD>
      <DIV class=error id=3_err_con></DIV></TD></TR>
  <TR id=vercode>
    <TD class=f14 vAlign=top>验证码:</TD>
    <TD vAlign=top><INPUT type=hidden 
      value=182CCA9B71170FA003C6A3448026C670E89C45413C204E516D423910DE5179EEC635DF1FF7062EFECDD961CC8644A3ADAB5FBB74F5782BCD71B7AD30373F53F2 
      name=spVcode> <INPUT maxLength=4 size=6 name=spVerifyKey 
      autocomplete="off">请输入下图中的四位验证码,字母不区分大小写。<BR>
      <SCRIPT language=JavaScript>

			var imgsrc="http://post.baidu.com/cgi-bin/genimg?182CCA9B71170FA003C6A3448026C670E89C45413C204E516D423910DE5179EEC635DF1FF7062EFECDD961CC8644A3ADAB5FBB74F5782BCD71B7AD30373F53F2";
			document.write("<img id='verifypic' src='"+imgsrc+"' width='120' height='40'>");

			function newverifypic(){
				document.getElementById("verifypic").src = imgsrc +"&t="+ Math.random();
			}
		</SCRIPT>
      <A title=看不清左边的字符 href="javascript:newverifypic();">看不清?</A> </TD></TR>
  <TR>
    <TD class=f14 vAlign=top>&nbsp;</TD>
    <TD class=f14 
  vAlign=top><INPUT type=submit value=发表评论 name=btn_ok></TD></TR></TBODY></TABLE></FORM></DIV><BR></DIV>
<TABLE height=8 cellSpacing=0 cellPadding=0 width="100%" border=0>
  <TBODY>
  <TR>
    <TD class=modbl width=7>&nbsp;</TD>
    <TD class=modbc>&nbsp;</TD>
    <TD class=modbr width=7>&nbsp;</TD></TR></TBODY></TABLE></DIV></DIV></DIV></DIV>
<SCRIPT language=javascript>
<!--
var hstr="/superdbs/brwstat?key1=1";
document.write("<script src='"+hstr+"&key2="+allkey+"'><\/script>");
//-->
</SCRIPT>
<BR>
<CENTER>
<DIV id=ft>&copy;2007 Baidu</DIV></CENTER>
<SCRIPT>
if(document.getElementById("m_blog"))
{
	var imgarray = document.getElementById("m_blog").getElementsByTagName('img');
	var imgw = document.getElementById("m_blog").offsetWidth;
	imgw =imgw-40;
	for(var i=0; i<imgarray.length; i++){
	if(imgarray[i].className=="blogimg" && imgarray[i].width>=imgw) imgarray[i].width=imgw;
	}
}
</SCRIPT>
</CENTER><IMG style="DISPLAY: none" src=""> </BODY></HTML>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?