﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-Jacky Loves C++</title><link>http://www.cppblog.com/jacky2019/</link><description>Next to my life, software is my passion.</description><language>zh-cn</language><lastBuildDate>Tue, 07 Oct 2008 03:42:25 GMT</lastBuildDate><pubDate>Tue, 07 Oct 2008 03:42:25 GMT</pubDate><ttl>60</ttl><item><title>存储过程 stored procedure</title><link>http://www.cppblog.com/jacky2019/archive/2007/11/06/35937.html</link><dc:creator>小熊</dc:creator><author>小熊</author><pubDate>Tue, 06 Nov 2007 02:37:00 GMT</pubDate><guid>http://www.cppblog.com/jacky2019/archive/2007/11/06/35937.html</guid><wfw:comment>http://www.cppblog.com/jacky2019/comments/35937.html</wfw:comment><comments>http://www.cppblog.com/jacky2019/archive/2007/11/06/35937.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/jacky2019/comments/commentRss/35937.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/jacky2019/services/trackbacks/35937.html</trackback:ping><description><![CDATA[<span style="COLOR: red">&nbsp;
<p><font color=#000000><font size=3><strong>定义：<br></strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 将常用的或很复杂的工作，预先用SQL语句写好并用一个指定的名称存储起来,&nbsp;&nbsp; 那么以后要叫<a onclick="javascript:tagshow(event, '%CA%FD%BE%DD%BF%E2');" href="javascript:;" target=_self><u><strong><font color=#800080>数据库</font></strong></u></a>提供与已定义好的存储过程的功能相同的服务时,只需调用execute,即可自动完成命令。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 讲到这里,可能有人要问：这么说存储过程就是一堆SQL语句而已啊？<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Microsoft公司为什么还要添加这个技术呢?<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 那么存储过程与一般的SQL语句有什么区别呢?</font></font></p>
<p><font color=#000000 size=3>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color=#000000><font size=3><strong>存储过程的优点：<br></strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1.存储过程只在创造时进行编译，以后每次执行存储过程都不需再重新编译，而一般SQL语句每执行一次就编译一次,所以使用存储过程可提高数据库执行速度。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2.当对数据库进行复杂操作时(如对多个表进行Update,Insert,Query,Delete时），可将此复杂操作用存储过程封装起来与数据库提供的事务处理结合一起使用。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 3.存储过程可以重复使用,可减少数据库开发人员的工作量<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 4.安全性高,可设定只有某此用户才具有对指定存储过程的使用权<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>存储过程的种类：</strong><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1.系统存储过程：以sp_开头,用来进行系统的各项设定.取得信息.相关管理工作,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 如&nbsp;&nbsp; sp_help就是取得指定对象的相关信息<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2.扩展存储过程&nbsp;&nbsp; 以XP_开头,用来调用操作系统提供的功能<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exec&nbsp;&nbsp; master..xp_cmdshell&nbsp;&nbsp; 'ping&nbsp;&nbsp; 10.8.16.1'<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 3.用户自定义的存储过程,这是我们所指的存储过程<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 常用格式<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Create&nbsp;&nbsp; procedure&nbsp;&nbsp; procedue_name<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [@parameter&nbsp;&nbsp; data_type][output]<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [with]{recompile|encryption}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; as<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sql_statement<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 解释:&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; output：表示此参数是可传回的<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; with&nbsp;&nbsp; {recompile|encryption}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; recompile:表示每次执行此存储过程时都重新编译一次<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; encryption:所创建的存储过程的内容会被加密<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 如:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 表book的内容如下<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 编号&nbsp;&nbsp; 书名&nbsp;&nbsp; 价格<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 001&nbsp;&nbsp; C语言入门&nbsp;&nbsp; $30<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 002&nbsp;&nbsp; PowerBuilder报表开发&nbsp;&nbsp; $52<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 实例1:查询表Book的内容的存储过程<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; create&nbsp;&nbsp; proc&nbsp;&nbsp; query_book<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; as&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; select&nbsp;&nbsp; *&nbsp;&nbsp; from&nbsp;&nbsp; book<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; go<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exec&nbsp;&nbsp; query_book<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 实例2:加入一笔记录到表book,并查询此表中所有书籍的总金额<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Create&nbsp;&nbsp; proc&nbsp;&nbsp; insert_book<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @param1&nbsp;&nbsp; char(10),@param2&nbsp;&nbsp; varchar(20),@param3&nbsp;&nbsp; money,@param4&nbsp;&nbsp; money&nbsp;&nbsp; output<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; with&nbsp;&nbsp; encryption&nbsp;&nbsp; ---------加密<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; as<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; insert&nbsp;&nbsp; book(编号,书名，价格）&nbsp;&nbsp; Values(@param1,@param2,@param3)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; select&nbsp;&nbsp; @param4=sum(价格)&nbsp;&nbsp; from&nbsp;&nbsp; book<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; go<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 执行例子:&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; declare&nbsp;&nbsp; @total_price&nbsp;&nbsp; money&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exec&nbsp;&nbsp; insert_book&nbsp;&nbsp; '003','Delphi&nbsp;&nbsp; 控件开发指南',$100,@total_price<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print&nbsp;&nbsp; '总金额为'+convert(varchar,@total_price)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; go<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font></font><font color=#000000><font size=3><strong>存储过程的3种传回值:<br></strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1.以Return传回整数<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2.以output格式传回参数<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 3.Recordset<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 传回值的区别:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; output和return都可在批次程式中用变量接收,而recordset则传回到执行批次的客户端中&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 实例3：设有两个表为Product,Order,其表内容如下：<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Product<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 产品编号&nbsp;&nbsp; 产品名称&nbsp;&nbsp; 客户订数&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 001&nbsp;&nbsp; 钢笔&nbsp;&nbsp; 30&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 002&nbsp;&nbsp; 毛笔&nbsp;&nbsp; 50&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 003&nbsp;&nbsp; 铅笔&nbsp;&nbsp; 100&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Order&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 产品编号&nbsp;&nbsp; 客户名&nbsp;&nbsp; 客户订金<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 001&nbsp;&nbsp; 南山区&nbsp;&nbsp; $30<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 002&nbsp;&nbsp; 罗湖区&nbsp;&nbsp; $50<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 003&nbsp;&nbsp; 宝安区&nbsp;&nbsp; $4<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 请实现按编号为连接条件,将两个表连接成一个临时表,该表只含编号.产品名.客户名.订金.总金额,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 总金额=订金*订数,临时表放在存储过程中<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 代码如下:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Create&nbsp;&nbsp; proc&nbsp;&nbsp; temp_sale<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; as<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; select&nbsp;&nbsp; a.产品编号,a.产品名称,b.客户名,b.客户订金,a.客户订数*&nbsp;&nbsp; b.客户订金&nbsp;&nbsp; as总金额<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; into&nbsp;&nbsp; #temptable&nbsp;&nbsp; from&nbsp;&nbsp; Product&nbsp;&nbsp; a&nbsp;&nbsp; inner&nbsp;&nbsp; join&nbsp;&nbsp; Order&nbsp;&nbsp; b&nbsp;&nbsp; on&nbsp;&nbsp; a.产品编号=b.产品编号<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if&nbsp;&nbsp; @@error=0&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print&nbsp;&nbsp; 'Good'<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print&nbsp;&nbsp; 'Fail'<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; go </font></font></p>
<p><font color=#000000 size=3>--------------------------------------------------------------------------------------------------------------------</font></p>
<p dir=ltr><font color=#000000><font size=3><strong>存储过程介绍</strong>&nbsp;&nbsp; </font></font></p>
<blockquote dir=ltr style="MARGIN-RIGHT: 0px">
<p><font color=#000000><font size=3><strong>一、先介绍一下什么是存储过程&nbsp;&nbsp; <br></strong>存储过程是利用SQL&nbsp;&nbsp; Server所提供的Tranact-SQL语言所编写的程序。Tranact-SQL语言是SQL&nbsp;&nbsp; Server提供专为设计数据库应用程序的语言，它是应用程序和SQL&nbsp;&nbsp; Server数据库间的主要程序式设计界面。它好比Oracle数据库系统中的Pro-SQL和Informix的数据库系统能够中的Informix-4GL语言一样。这类语言主要提供以下功能，让用户可以设计出符合引用需求的程序：&nbsp;&nbsp; <br>1)、变量说明&nbsp;&nbsp; <br>2)、ANSI兼容的SQL命令(如Select,Update&#8230;.)&nbsp;&nbsp; <br>3)、一般流程控制命令(if&#8230;else&#8230;、while&#8230;.)&nbsp;&nbsp; <br>4)、内部函数&nbsp;&nbsp; </font></font></p>
<p><strong><font color=#000000 size=3>二、存储过程的书写格式&nbsp;&nbsp; </font></strong></p>
<p><font color=#000000 size=3>CREATE&nbsp;&nbsp; PROCEDURE&nbsp;&nbsp; [拥有者.]存储过程名[;程序编号]&nbsp;&nbsp; <br>[(参数#1,&#8230;参数#1024)]&nbsp;&nbsp; <br>[WITH&nbsp;&nbsp; <br>{RECOMPILE&nbsp;&nbsp; |&nbsp;&nbsp; ENCRYPTION&nbsp;&nbsp; |&nbsp;&nbsp; RECOMPILE,&nbsp;&nbsp; ENCRYPTION}&nbsp;&nbsp; <br>]&nbsp;&nbsp; <br>[FOR&nbsp;&nbsp; REPLICATION]&nbsp;&nbsp; <br>AS&nbsp;&nbsp; 程序行&nbsp;&nbsp; </font></p>
<p><font color=#000000 size=3>其中存储过程名不能超过128个字。每个存储过程中最多设定1024个参数&nbsp;&nbsp; <br>(SQL&nbsp;&nbsp; Server&nbsp;&nbsp; 7.0以上版本),参数的使用方法如下:&nbsp;&nbsp; </font></p>
<p><font color=#000000 size=3>@参数名&nbsp;&nbsp; 数据类型&nbsp;&nbsp; [VARYING]&nbsp;&nbsp; [=内定值]&nbsp;&nbsp; [OUTPUT]&nbsp;&nbsp; </font></p>
<p><font color=#000000 size=3>每个参数名前要有一个&#8220;@&#8221;符号,每一个存储过程的参数仅为该程序内部使用,参数的类型除了IMAGE外，其他SQL&nbsp;&nbsp; Server所支持的数据类型都可使用。&nbsp;&nbsp; <br>[=内定值]相当于我们在建立数据库时设定一个字段的默认值，这里是为这个参数设定默认值。[OUTPUT]是用来指定该参数是既有输入又有输出值的，也就是在调用了这个存储过程时，如果所指定的参数值是我们需要输入的参数，同时也需要在结果中输出的，则该项必须为OUTPUT，而如果只是做输出参数用，可以用CURSOR，同时在使用该参数时，必须指定VARYING和OUTPUT这两个语句。&nbsp;&nbsp; </font></p>
<p><font color=#000000 size=3>例子:&nbsp;&nbsp; <br>CREATE&nbsp;&nbsp; PROCEDURE&nbsp;&nbsp; order_tot_amt&nbsp;&nbsp; @o_id&nbsp;&nbsp; int,@p_tot&nbsp;&nbsp; int&nbsp;&nbsp; output&nbsp;&nbsp; AS&nbsp;&nbsp; <br>SELECT&nbsp;&nbsp; @p_tot&nbsp;&nbsp; =&nbsp;&nbsp; sum(Unitprice*Quantity)&nbsp;&nbsp; <br>FROM&nbsp;&nbsp; orderdetails&nbsp;&nbsp; <br>WHERE&nbsp;&nbsp; ōrdered=@o_id&nbsp;&nbsp; </font></p>
<p><font color=#000000 size=3>例子说明:&nbsp;&nbsp; <br>该例子是建立一个简单的存储过程order_tot_amt,这个存储过程根据用户输入的定单ID号码(@o_id),由定单明细表(orderdetails)中计算该定单销售总额[单价(Unitprice)*数量(Quantity)],这一金额通过@p_tot这一参数输出给调用这一存储过程的程序&nbsp;&nbsp; </font></p>
<p><font color=#000000><font size=3><strong>三、在SQL&nbsp;&nbsp; Server中执行存储过程</strong>&nbsp;&nbsp; </font></font></p>
<p><font color=#000000 size=3>在SQL&nbsp;&nbsp; Server的查询分析器中，输入以下代码:&nbsp;&nbsp; <br>declare&nbsp;&nbsp; @tot_amt&nbsp;&nbsp; int&nbsp;&nbsp; <br>execute&nbsp;&nbsp; order_tot_amt&nbsp;&nbsp; 1,@tot_amt&nbsp;&nbsp; output&nbsp;&nbsp; <br>select&nbsp;&nbsp; @tot_amt&nbsp;&nbsp; </font></p>
<p><font color=#000000 size=3>以上代码是执行order_tot_amt这一存储过程，以计算出定单编号为1的定单销售金额，我们定义@tot_amt为输出参数，用来承接我们所要的结果 </font></p>
</blockquote></span>
<img src ="http://www.cppblog.com/jacky2019/aggbug/35937.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/jacky2019/" target="_blank">小熊</a> 2007-11-06 10:37 <a href="http://www.cppblog.com/jacky2019/archive/2007/11/06/35937.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>汉字编码问题</title><link>http://www.cppblog.com/jacky2019/archive/2007/11/01/35693.html</link><dc:creator>小熊</dc:creator><author>小熊</author><pubDate>Thu, 01 Nov 2007 06:53:00 GMT</pubDate><guid>http://www.cppblog.com/jacky2019/archive/2007/11/01/35693.html</guid><wfw:comment>http://www.cppblog.com/jacky2019/comments/35693.html</wfw:comment><comments>http://www.cppblog.com/jacky2019/archive/2007/11/01/35693.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/jacky2019/comments/commentRss/35693.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/jacky2019/services/trackbacks/35693.html</trackback:ping><description><![CDATA[<font face=宋体><a href="http://www.css8.cn/css8_document/gb2312.htm">http://www.css8.cn/css8_document/gb2312.htm</a><br><br>由于常常要和汉字处理打交道，因此，我常常受到汉字编码问题的困扰。在不断的打击与坚持中，也积累了一点汉字编码方面的经验，想和大家一起分享。</font>
<p class=MsoNormal><span style="COLOR: black; FONT-FAMILY: 宋体">一、汉字编码的种类</span></p>
<p class=MsoNormal><span lang=EN-US style="COLOR: black; FONT-FAMILY: 宋体">&nbsp;&nbsp;&nbsp; </span><span style="COLOR: black; FONT-FAMILY: 宋体">汉字编码中现在主要用到的有三类，包括<span lang=EN-US>GBK</span>，<span lang=EN-US>GB2312</span>和<span lang=EN-US>Big5</span>。</span></p>
<p class=MsoNormal><span lang=EN-US style="COLOR: black; FONT-FAMILY: 宋体">&nbsp;&nbsp;&nbsp; 1</span><span style="COLOR: black; FONT-FAMILY: 宋体">、<span lang=EN-US>GB2312</span>又称国标码，</span><span style="FONT-FAMILY: 宋体">由国家标准总局发布，</span><span lang=EN-US>1981</span><span style="FONT-FAMILY: 宋体">年</span><span lang=EN-US>5</span><span style="FONT-FAMILY: 宋体">月</span><span lang=EN-US>1</span><span style="FONT-FAMILY: 宋体">日实施，通行于大陆。新加坡等地也使用此编码。它是一个简化字的编码规范，当然也包括其他的符号、字母、日文假名等，共</span><span lang=EN-US>7445</span><span style="FONT-FAMILY: 宋体">个图形字符，其中汉字占</span><span lang=EN-US>6763</span><span style="FONT-FAMILY: 宋体">个。我们平时说</span><span lang=EN-US>6768</span><span style="FONT-FAMILY: 宋体">个汉字，实际上里边有</span><span lang=EN-US>5</span><span style="FONT-FAMILY: 宋体">个编码为空白，所以总共有</span><span lang=EN-US>6763</span><span style="FONT-FAMILY: 宋体">个汉字。</span></p>
<p class=MsoNormal><span lang=EN-US>&nbsp; &nbsp;&nbsp;&nbsp; GB2312</span><span style="FONT-FAMILY: 宋体">规定&#8220;对任意一个图形字符都采用两个字节表示，每个字节均采用七位编码表示&#8221;，习惯上称第一个字节为&#8220;高字节&#8221;，第二个字节为&#8220;低字节&#8221;。</span><span lang=EN-US style="COLOR: black; FONT-FAMILY: 宋体">GB2312</span><span style="COLOR: black; FONT-FAMILY: 宋体">中汉字的编码范围为，第一字节<span lang=EN-US>0xB0-0xF7(</span>对应十进制为<span lang=EN-US>176-247)</span>，第二个字节<span lang=EN-US>0xA0-0xFE</span>（对应十进制为<span lang=EN-US>160-254</span>）。</span></p>
<p class=MsoNormal><span lang=EN-US style="COLOR: black; FONT-FAMILY: 宋体">&nbsp;&nbsp;&nbsp; </span><span lang=EN-US>GB2312</span><span style="FONT-FAMILY: 宋体">将代码表分为</span><span lang=EN-US>94</span><span style="FONT-FAMILY: 宋体">个区，对应第一字节（</span><span lang=EN-US style="FONT-FAMILY: 宋体">0xa1-0xfe</span><span style="FONT-FAMILY: 宋体">）；每个区</span><span lang=EN-US>94</span><span style="FONT-FAMILY: 宋体">个位（</span><span lang=EN-US style="FONT-FAMILY: 宋体">0xa1-0xfe</span><span style="FONT-FAMILY: 宋体">），对应第二字节，两个字节的值分别为区号值和位号值加</span><span lang=EN-US>32</span><span style="FONT-FAMILY: 宋体">（</span><span lang=EN-US>2OH</span><span style="FONT-FAMILY: 宋体">），因此也称为区位码。</span><span lang=EN-US>01-09</span><span style="FONT-FAMILY: 宋体">区为符号、数字区，</span><span lang=EN-US>16-87</span><span style="FONT-FAMILY: 宋体">区为汉字区（</span><span lang=EN-US style="FONT-FAMILY: 宋体">0xb0-0xf7</span><span style="FONT-FAMILY: 宋体">），</span><span lang=EN-US>10-15</span><span style="FONT-FAMILY: 宋体">区、</span><span lang=EN-US>88-94</span><span style="FONT-FAMILY: 宋体">区是有待进一步标准化的空白区。</span></p>
<p class=MsoNormal><span lang=EN-US></span>&nbsp;</p>
<p class=MsoNormal><span lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2</span><span style="FONT-FAMILY: 宋体">、</span><span lang=EN-US>Big5</span><span style="FONT-FAMILY: 宋体">又称大五码，主要为香港与台湾使用，即是一个繁体字编码。</span><span style="COLOR: black; FONT-FAMILY: 宋体">每个汉字由两个字节构成，第一个字节的范围从</span><span lang=EN-US style="COLOR: black; FONT-FAMILY: Arial">0X81</span><span style="COLOR: black; FONT-FAMILY: 宋体">－</span><span lang=EN-US style="COLOR: black; FONT-FAMILY: Arial">0XFE</span><span style="COLOR: black; FONT-FAMILY: 宋体">（即</span><span lang=EN-US style="COLOR: black; FONT-FAMILY: Arial">129-255</span><span style="COLOR: black; FONT-FAMILY: 宋体">），共</span><span lang=EN-US style="COLOR: black; FONT-FAMILY: Arial">126</span><span style="COLOR: black; FONT-FAMILY: 宋体">种。第二个字节的范围不连续，分别为</span><span lang=EN-US style="COLOR: black; FONT-FAMILY: Arial">0X40</span><span style="COLOR: black; FONT-FAMILY: 宋体">－</span><span lang=EN-US style="COLOR: black; FONT-FAMILY: Arial">0X7E</span><span style="COLOR: black; FONT-FAMILY: 宋体">（即</span><span lang=EN-US style="COLOR: black; FONT-FAMILY: Arial">64-126</span><span style="COLOR: black; FONT-FAMILY: 宋体">），</span><span lang=EN-US style="COLOR: black; FONT-FAMILY: Arial">0XA1</span><span style="COLOR: black; FONT-FAMILY: 宋体">－</span><span lang=EN-US style="COLOR: black; FONT-FAMILY: Arial">0XFE</span><span style="COLOR: black; FONT-FAMILY: 宋体">（即</span><span lang=EN-US style="COLOR: black; FONT-FAMILY: Arial">161-254</span><span style="COLOR: black; FONT-FAMILY: 宋体">），共</span><span lang=EN-US style="COLOR: black; FONT-FAMILY: Arial">157</span><span style="COLOR: black; FONT-FAMILY: 宋体">种。</span></p>
<p class=MsoNormal><span lang=EN-US style="COLOR: black; FONT-FAMILY: 宋体"></span>&nbsp;</p>
<p class=MsoNormal><span lang=EN-US style="COLOR: black; FONT-FAMILY: 宋体">&nbsp;&nbsp;&nbsp; 3</span><span style="COLOR: black; FONT-FAMILY: 宋体">、<span lang=EN-US>GBK</span>是<span lang=EN-US>GB2312</span>的扩展，是向上兼容的，因此<span lang=EN-US>GB2312</span>中的汉字的编码与<span lang=EN-US>GBK</span>中汉字的相同。另外，<span lang=EN-US>GBK</span>中还包含繁体字的编码，它与<span lang=EN-US>Big5</span>编码之间的关系我还没有弄明白，好像是不一致的。<span lang=EN-US>GBK</span>中每个汉字仍然包含两个字节，第一个字节的范围是<span lang=EN-US>0x81-0xFE</span>（即<span lang=EN-US>129-254</span>），第二个字节的范围是<span lang=EN-US>0x40-0xFE</span>（即<span lang=EN-US>64-254</span>）。<span lang=EN-US>GBK</span>中有码位<span lang=EN-US>23940</span>个，包含汉字<span lang=EN-US>21003</span>个。</span></p>
<p class=MsoNormal><span lang=EN-US style="COLOR: black; FONT-FAMILY: 宋体">&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; </span></p>
<p class=MsoNormal><span lang=en-us style="FONT-FAMILY: 宋体">&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; </span><span style="COLOR: black; FONT-FAMILY: 宋体">表<span lang=EN-US>1 </span>汉字编码范围</span></p>
<table class=MsoTableGrid id=table1 style="BORDER-RIGHT: medium none; BORDER-TOP: medium none; MARGIN-LEFT: 50.4pt; BORDER-LEFT: medium none; BORDER-BOTTOM: medium none; BORDER-COLLAPSE: collapse" cellSpacing=0 cellPadding=0 border=1>
    <tbody>
        <tr>
            <td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 1pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: windowtext 1pt solid; WIDTH: 81pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid" vAlign=top width=108>
            <p class=MsoNormal><span style="COLOR: black; FONT-FAMILY: 宋体">名称</span></p>
            </td>
            <td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 1pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 144pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid" vAlign=top width=192>
            <p class=MsoNormal><span style="COLOR: black; FONT-FAMILY: 宋体">第一字节</span></p>
            </td>
            <td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 1pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 153pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid" vAlign=top width=204>
            <p class=MsoNormal><span style="COLOR: black; FONT-FAMILY: 宋体">第二字节</span></p>
            </td>
        </tr>
        <tr>
            <td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: windowtext 1pt solid; WIDTH: 81pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid" vAlign=top width=108>
            <p class=MsoNormal><span lang=EN-US style="COLOR: black">GB2312</span></p>
            </td>
            <td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 144pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid" vAlign=top width=192>
            <p class=MsoNormal><span lang=EN-US style="COLOR: black">0xB0-0xF7(176-247) </span></p>
            </td>
            <td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 153pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid" vAlign=top width=204>
            <p class=MsoNormal><span lang=EN-US style="COLOR: black">0xA0-0xFE</span><span style="COLOR: black; FONT-FAMILY: 宋体">（</span><span lang=EN-US style="COLOR: black">160-254</span><span style="COLOR: black; FONT-FAMILY: 宋体">）</span></p>
            </td>
        </tr>
        <tr>
            <td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: windowtext 1pt solid; WIDTH: 81pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid" vAlign=top width=108>
            <p class=MsoNormal><span lang=EN-US style="COLOR: black">GBK</span></p>
            </td>
            <td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 144pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid" vAlign=top width=192>
            <p class=MsoNormal><span lang=EN-US style="COLOR: black">0x81-0xFE</span><span style="COLOR: black; FONT-FAMILY: 宋体">（</span><span lang=EN-US style="COLOR: black">129-254</span><span style="COLOR: black; FONT-FAMILY: 宋体">）</span></p>
            </td>
            <td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 153pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid" vAlign=top width=204>
            <p class=MsoNormal><span lang=EN-US style="COLOR: black">0x40-0xFE</span><span style="COLOR: black; FONT-FAMILY: 宋体">（</span><span lang=EN-US style="COLOR: black">64-254</span><span style="COLOR: black; FONT-FAMILY: 宋体">）</span></p>
            </td>
        </tr>
        <tr>
            <td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: windowtext 1pt solid; WIDTH: 81pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid" vAlign=top width=108>
            <p class=MsoNormal><span lang=EN-US style="COLOR: black">Big5</span></p>
            </td>
            <td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 144pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid" vAlign=top width=192>
            <p class=MsoNormal><span lang=EN-US style="COLOR: black">0x81-0xFE</span><span style="COLOR: black; FONT-FAMILY: 宋体">（</span><span lang=EN-US style="COLOR: black">129-255</span><span style="COLOR: black; FONT-FAMILY: 宋体">）</span></p>
            </td>
            <td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 153pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid" vAlign=top width=204>
            <p class=MsoNormal><span lang=EN-US style="COLOR: black">0x40-0x7E</span><span style="COLOR: black; FONT-FAMILY: 宋体">（</span><span lang=EN-US style="COLOR: black">64-126</span><span style="COLOR: black; FONT-FAMILY: 宋体">）</span></p>
            <p class=MsoNormal><span lang=EN-US style="COLOR: black">0xA1</span><span style="COLOR: black; FONT-FAMILY: 宋体">－</span><span lang=EN-US style="COLOR: black">0xFE</span><span style="COLOR: black; FONT-FAMILY: 宋体">（</span><span lang=EN-US style="COLOR: black">161-254</span><span style="COLOR: black; FONT-FAMILY: 宋体">）</span></p>
            </td>
        </tr>
    </tbody>
</table>
<p class=MsoNormal><span lang=EN-US style="COLOR: black; FONT-FAMILY: 宋体"></span>&nbsp;</p>
<p class=MsoNormal><span lang=EN-US style="COLOR: black; FONT-FAMILY: 宋体"></span>&nbsp;</p>
<p class=MsoNormal><span style="COLOR: black; FONT-FAMILY: 宋体">二、对汉字进行<span lang=EN-US>hash</span></span></p>
<p class=MsoNormal><span lang=EN-US style="COLOR: black; FONT-FAMILY: 宋体">&nbsp;&nbsp;&nbsp; </span><span style="COLOR: black; FONT-FAMILY: 宋体">为了处理汉字的方便，在查找汉字的时候，我们通常会用到<span lang=EN-US>hash</span>的方法，那怎么来确定一个汉字位置呢？这就和每种编码的排列有关了，这里主要给出一种<span lang=EN-US>hash</span>函数的策略。</span></p>
<p class=MsoNormal><span lang=EN-US style="COLOR: black; FONT-FAMILY: 宋体">&nbsp;&nbsp;&nbsp; </span><span style="COLOR: black; FONT-FAMILY: 宋体">对于<span lang=EN-US>GB2312</span>编码，设输入的汉字为<span lang=EN-US>GBword</span>，我们可以采用公式<span lang=EN-US>(C1-176)*94 + (C2-161)</span>确定<span lang=EN-US>GBindex</span>。其中，<span lang=EN-US>C1</span>表示第一字节，<span lang=EN-US>C2</span>表示第二字节。具体如下：</span></p>
<p class=MsoNormal style="TEXT-ALIGN: left" align=left><span lang=EN-US style="COLOR: #3700c8; FONT-FAMILY: Fixedsys">&nbsp;&nbsp;&nbsp; GBindex = ((unsigned char)GBword.at(0)-176)*94 + (unsigned char)GBword.at(1) - 161;</span></p>
<p class=MsoNormal style="TEXT-ALIGN: left" align=left><span lang=EN-US style="FONT-FAMILY: 宋体">&nbsp;&nbsp;&nbsp; </span><span style="FONT-FAMILY: 宋体">之所以用<span lang=EN-US>unsigned char</span>类型，是因为<span lang=EN-US>char</span>是一个字节，如果用<span lang=EN-US>unsigend int</span>，因为<span lang=EN-US>int</span>是<span lang=EN-US>4</span>个字节的，所以会造成扩展，导致错误。</span></p>
<p class=MsoNormal style="MARGIN-LEFT: 18.05pt; TEXT-INDENT: -35.9pt; TEXT-ALIGN: left" align=left><span lang=EN-US style="FONT-FAMILY: 宋体">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="FONT-FAMILY: 宋体">对于<span lang=EN-US>GBK</span>编码，设输入的汉字为<span lang=EN-US>GBKword</span>，则可以采用公式<span lang=EN-US>&nbsp;&nbsp; </span></span><span lang=EN-US style="FONT-FAMILY: 宋体">index=(ch1-0x81)*190+(ch2-0x40)-(ch2/128)</span><span style="FONT-FAMILY: 宋体">，其中<span lang=EN-US>ch1</span>是第一字节，<span lang=EN-US>ch2</span>是第二字节。</span></p>
<p class=MsoNormal><span lang=EN-US style="COLOR: black; FONT-FAMILY: 宋体">&nbsp;&nbsp;&nbsp; </span><span style="COLOR: black; FONT-FAMILY: 宋体">具体的，</span></p>
<p class=MsoNormal style="TEXT-ALIGN: left" align=left><span lang=EN-US style="FONT-SIZE: 10pt; COLOR: #3700c8; FONT-FAMILY: Fixedsys">&nbsp;&nbsp;&nbsp; GBKindex = ((unsigned char)GBKword[0]-129)*190 +</span></p>
<p class=MsoNormal style="TEXT-ALIGN: left" align=left><span lang=EN-US style="FONT-SIZE: 10pt; COLOR: #3700c8; FONT-FAMILY: Fixedsys">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ((unsigned char)GBKword[1]-64) - (unsigned char)GBKword[1]/128;</span></p>
<p class=MsoNormal><span lang=EN-US style="COLOR: black; FONT-FAMILY: 宋体"></span>&nbsp;</p>
<p class=MsoNormal><span style="COLOR: black; FONT-FAMILY: 宋体">三、怎样判断一个汉字的是什么编码</span></p>
<p class=MsoNormal><span style="COLOR: black; FONT-FAMILY: 宋体">直接根据汉字的编码范围判断，对于<span lang=EN-US>GB2312</span>和<span lang=EN-US>GBK</span>可用下面两个程序实现。</span></p>
<p class=MsoNormal style="TEXT-ALIGN: left" align=left><span lang=EN-US style="COLOR: black; FONT-FAMILY: 宋体">1</span><span style="COLOR: black; FONT-FAMILY: 宋体">、判断是否是<span lang=EN-US>GB2312</span></span></p>
<p class=MsoNormal style="TEXT-ALIGN: left" align=left><span lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: Fixedsys">bool isGBCode(const string&amp; strIn)</span></p>
<p class=MsoNormal style="TEXT-ALIGN: left" align=left><span lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: Fixedsys">{</span></p>
<p class=MsoNormal style="TEXT-ALIGN: left" align=left><span lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: Fixedsys">&nbsp;&nbsp;&nbsp; unsigned char ch1;</span></p>
<p class=MsoNormal style="TEXT-ALIGN: left" align=left><span lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: Fixedsys">&nbsp;&nbsp;&nbsp; unsigned char ch2;</span></p>
<p class=MsoNormal style="TEXT-ALIGN: left" align=left><span lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: Fixedsys">&nbsp;&nbsp;&nbsp; </span></p>
<p class=MsoNormal style="TEXT-ALIGN: left" align=left><span lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: Fixedsys">&nbsp;&nbsp;&nbsp; if (strIn.size() &gt;= 2)</span></p>
<p class=MsoNormal style="TEXT-ALIGN: left" align=left><span lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: Fixedsys">&nbsp;&nbsp;&nbsp; {</span></p>
<p class=MsoNormal style="TEXT-ALIGN: left" align=left><span lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: Fixedsys">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ch1 = (unsigned char)strIn.at(0);</span></p>
<p class=MsoNormal style="TEXT-ALIGN: left" align=left><span lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: Fixedsys">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ch2 = (unsigned char)strIn.at(1);</span></p>
<p class=MsoNormal style="TEXT-ALIGN: left" align=left><span lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: Fixedsys">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (ch1&gt;=176 &amp;&amp; ch1&lt;=247 &amp;&amp; ch2&gt;=160 &amp;&amp; ch2&lt;=254)</span></p>
<p class=MsoNormal style="TEXT-ALIGN: left" align=left><span lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: Fixedsys">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return true;</span></p>
<p class=MsoNormal style="TEXT-ALIGN: left" align=left><span lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: Fixedsys">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else return false;</span></p>
<p class=MsoNormal style="TEXT-ALIGN: left" align=left><span lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: Fixedsys">&nbsp;&nbsp;&nbsp; }</span></p>
<p class=MsoNormal style="TEXT-ALIGN: left" align=left><span lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: Fixedsys">&nbsp;&nbsp;&nbsp; else return false;</span></p>
<p class=MsoNormal style="TEXT-ALIGN: left" align=left><span lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: Fixedsys">}</span></p>
<p class=MsoNormal><span lang=EN-US style="COLOR: black; FONT-FAMILY: 宋体">2</span><span style="COLOR: black; FONT-FAMILY: 宋体">、判断是否是<span lang=EN-US>GBK</span>编码</span></p>
<p class=MsoNormal style="TEXT-ALIGN: left" align=left><span lang=EN-US style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: Fixedsys">bool isGBKCode(const string&amp; strIn)</span></p>
<p class=MsoNormal style="TEXT-ALIGN: left" align=left><span lang=EN-US style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: Fixedsys">{</span></p>
<p class=MsoNormal style="TEXT-ALIGN: left" align=left><span lang=EN-US style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: Fixedsys">&nbsp;&nbsp;&nbsp; unsigned char ch1;</span></p>
<p class=MsoNormal style="TEXT-ALIGN: left" align=left><span lang=EN-US style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: Fixedsys">&nbsp;&nbsp;&nbsp; unsigned char ch2;</span></p>
<p class=MsoNormal style="TEXT-ALIGN: left" align=left><span lang=EN-US style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: Fixedsys">&nbsp;&nbsp;&nbsp; </span></p>
<p class=MsoNormal style="TEXT-ALIGN: left" align=left><span lang=EN-US style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: Fixedsys">&nbsp;&nbsp;&nbsp; </span><span style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: Fixedsys">if (strIn.size() &gt;= 2)</span></p>
<p class=MsoNormal style="TEXT-ALIGN: left" align=left><span style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: Fixedsys">&nbsp;&nbsp;&nbsp; {</span></p>
<p class=MsoNormal style="TEXT-ALIGN: left" align=left><span style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: Fixedsys">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span lang=EN-US style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: Fixedsys">ch1 = (unsigned char)strIn.at(0);</span></p>
<p class=MsoNormal style="TEXT-ALIGN: left" align=left><span lang=EN-US style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: Fixedsys">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ch2 = (unsigned char)strIn.at(1);</span></p>
<p class=MsoNormal style="TEXT-ALIGN: left" align=left><span lang=EN-US style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: Fixedsys">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (ch1&gt;=129 &amp;&amp; ch1&lt;=254 &amp;&amp; ch2&gt;=64 &amp;&amp; ch2&lt;=254)</span></p>
<p class=MsoNormal style="TEXT-ALIGN: left" align=left><span lang=EN-US style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: Fixedsys">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return true;</span></p>
<p class=MsoNormal style="TEXT-ALIGN: left" align=left><span lang=EN-US style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: Fixedsys">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else return false;</span></p>
<p class=MsoNormal style="TEXT-ALIGN: left" align=left><span lang=EN-US style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: Fixedsys">&nbsp;&nbsp;&nbsp; }</span></p>
<p class=MsoNormal style="TEXT-ALIGN: left" align=left><span lang=EN-US style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: Fixedsys">&nbsp;&nbsp;&nbsp; else return false;</span></p>
<p class=MsoNormal style="TEXT-ALIGN: left" align=left><span style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: Fixedsys">}</span></p>
<p class=MsoNormal><span lang=EN-US style="COLOR: black; FONT-FAMILY: 宋体"></span>&nbsp;</p>
<p class=MsoNormal><span lang=EN-US style="COLOR: black; FONT-FAMILY: 宋体">3</span><span style="COLOR: black; FONT-FAMILY: 宋体">、对于<span lang=EN-US>Big5</span></span></p>
<p class=MsoPlainText><span lang=EN-US>&nbsp;&nbsp;&nbsp; </span>它的范围为：高字节从<span lang=EN-US>0xA0</span>到<span lang=EN-US>0xFE</span>，低字节从<span lang=EN-US>0x40</span>到<span lang=EN-US>0x7E</span>，和<span lang=EN-US>0xA1</span>到<span lang=EN-US>0xFE</span>两部分。判断一个汉字是否是<span lang=EN-US>BIG5</span>编码，可以如上对字符的编码范围判断即可。如何定位呢？那么也想象所有编码排列为一个二维坐标，纵坐标是高字节，横坐标是低字节。这样一行上的汉字个数：<span lang=EN-US>(0x7E-0x40+1)+(0xFE-0xA1+1)</span>＝<span lang=EN-US>157</span>。那么定位算法分两块，为<span lang=EN-US>: &nbsp;</span></p>
<p class=MsoPlainText><span lang=EN-US>&nbsp;&nbsp;&nbsp; if 0x40&lt;=ch2&lt;=0x7E: #is big5 char </span></p>
<p class=MsoPlainText>&nbsp;&nbsp;&nbsp; <span lang=PT-BR>index=((ch1-0xA1)*157+(ch2-0x40))*2 </span></p>
<p class=MsoPlainText><span lang=EN-US>&nbsp;&nbsp;&nbsp; elif 0xA1&lt;=ch2&lt;=0xFE: #is big5 char </span></p>
<p class=MsoPlainText>&nbsp;&nbsp;&nbsp; <span lang=PT-BR>index=((ch1-0xA1)*157+(ch2-0xA1+63))*2 </span></p>
<p class=MsoPlainText><span lang=PT-BR></span>&nbsp;</p>
<p class=MsoNormal><span style="FONT-FAMILY: 宋体">对于第二块，计算偏移量时因为有两块数值，所以在计算后面一段值时，不要忘了前面还有一段值。<span lang=EN-US>0x7E-0x40+1=63</span>。</span></p>
<p class=MsoNormal><span lang=EN-US style="FONT-FAMILY: 宋体"></span>&nbsp;</p>
<p class=MsoNormal><span style="FONT-FAMILY: 宋体">四、如果判断一个字符是西文字符还是中文字符</span></p>
<p class=MsoNormal><span lang=EN-US style="FONT-FAMILY: 宋体">&nbsp;&nbsp;&nbsp; </span><span style="FONT-FAMILY: 宋体">大家知道西文字符主要是指</span><span lang=EN-US style="COLOR: black; FONT-FAMILY: 宋体">ASCII</span><span style="COLOR: black; FONT-FAMILY: 宋体">码，它用一个字节表示。且这个字符转换成数字之后，该数字是大于<span lang=EN-US>0</span>的，而汉字是两个字节的，第一个字节的转化为数字之后应该是小于<span lang=EN-US>0</span>的，因此可以根据每个字节转化为数字之后是否小于<span lang=EN-US>0</span>，判断它是否是汉字。</span></p>
<p class=MsoNormal><span lang=EN-US style="COLOR: black; FONT-FAMILY: 宋体">&nbsp;&nbsp;&nbsp; </span><span style="COLOR: black; FONT-FAMILY: 宋体">例如，设输入字为<span lang=EN-US>strin</span>，则，</span></p>
<p class=MsoNormal><span lang=EN-US style="COLOR: black; FONT-FAMILY: 宋体">&nbsp;&nbsp;&nbsp;&nbsp; If (strin.at(0) &lt; 0)</span></p>
<p class=MsoNormal><span lang=EN-US style="COLOR: black; FONT-FAMILY: 宋体">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout &lt;&lt; &#8221;</span><span style="COLOR: black; FONT-FAMILY: 宋体">是汉字<span lang=EN-US>&#8221; &lt;&lt; endl;</span></span></p>
<p class=MsoNormal><span lang=EN-US style="COLOR: black; FONT-FAMILY: 宋体">&nbsp;&nbsp;&nbsp;&nbsp; else cout &lt;&lt; &#8221;</span><span style="COLOR: black; FONT-FAMILY: 宋体">不是汉字<span lang=EN-US>&#8221; &lt;&lt; endl;</span></span></p>
<img src ="http://www.cppblog.com/jacky2019/aggbug/35693.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/jacky2019/" target="_blank">小熊</a> 2007-11-01 14:53 <a href="http://www.cppblog.com/jacky2019/archive/2007/11/01/35693.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>常用数据类型使用转换详解</title><link>http://www.cppblog.com/jacky2019/archive/2007/11/01/35657.html</link><dc:creator>小熊</dc:creator><author>小熊</author><pubDate>Thu, 01 Nov 2007 01:30:00 GMT</pubDate><guid>http://www.cppblog.com/jacky2019/archive/2007/11/01/35657.html</guid><wfw:comment>http://www.cppblog.com/jacky2019/comments/35657.html</wfw:comment><comments>http://www.cppblog.com/jacky2019/archive/2007/11/01/35657.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/jacky2019/comments/commentRss/35657.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/jacky2019/services/trackbacks/35657.html</trackback:ping><description><![CDATA[<p><font style="BACKGROUND-COLOR: #ffffff" color=#009900><a href="http://www.vckbase.com/study/article/data_convert.htm">http://www.vckbase.com/study/article/data_convert.htm</a></font><font color=#009900></p>
<p align=left><br><br><br><strong>常用数据类型使用转换详解<br></strong></font>作者：程佩君</p>
<hr noShade SIZE=1>
<table class=big height=2155 cellSpacing=0 cellPadding=0 width="100%" border=0>
    <tbody>
        <tr>
            <td vAlign=top height=2418>
            <p>读者层次：<font color=#006699>初学</font><br><br>刚接触VC编程的朋友往往对许多数据类型的转换感到迷惑不解，本文将介绍一些常用数据类型的使用。<br><br>我们先定义一些常见类型变量借以说明</p>
            <p>int i = 100;<br>long l = 2001;<br>float f=300.2;<br>double d=12345.119;<br>char username[]="程佩君";<br>char temp[200];<br>char *buf;<br>CString str;<br>_variant_t v1;<br>_bstr_t v2;<br><br><font color=#6699ff><strong>一、其它数据类型转换为字符串</strong></font><br></p>
            <ul>
                <li><font color=#6699ff>短整型(int)</font><br>itoa(i,temp,10);///将i转换为字符串放入temp中,最后一个数字表示十进制<br>itoa(i,temp,2); ///按二进制方式转换
                <li><font color=#6699ff>长整型(long)</font><br>ltoa(l,temp,10);
                <li><font color=#6699ff>浮点数(float,double)</font><br>用fcvt可以完成转换,这是MSDN中的例子:<br>int decimal, sign; <br>char *buffer; <br>double source = 3.1415926535; <br>buffer = _fcvt( source, 7, &amp;decimal, &amp;sign ); <br>运行结果:source: 3.1415926535 buffer: '31415927' decimal: 1 sign: 0<br>decimal表示小数点的位置,sign表示符号:0为正数，1为负数
                <li><font color=#6699ff>CString变量</font><br>str = "2008北京奥运";<br>buf = (LPSTR)(LPCTSTR)str;
                <li><font color=#6699ff>BSTR变量</font><br>BSTR bstrValue = ::SysAllocString(L"程序员"); <br>char * buf = _com_util::ConvertBSTRToString(bstrValue); <br>SysFreeString(bstrValue); <br>AfxMessageBox(buf); <br>delete(buf);
                <li><font color=#6699ff>CComBSTR变量</font><br>CComBSTR bstrVar("test"); <br>char *buf = _com_util::ConvertBSTRToString(bstrVar.m_str); <br>AfxMessageBox(buf); <br>delete(buf); <br>
                <li><font color=#6699ff>_bstr_t变量</font><br>_bstr_t类型是对BSTR的封装，因为已经重载了=操作符，所以很容易使用<br>_bstr_t bstrVar("test"); <br>const char *buf = bstrVar;///不要修改buf中的内容 <br>AfxMessageBox(buf); <br><br>
                <li><font color=#6699ff>通用方法(针对非COM数据类型)</font><br>用sprintf完成转换<br>
                <pre>char  buffer[200];
                char  c = '1';
                int   i = 35;
                long  j = 1000;
                float f = 1.7320534f;
                sprintf( buffer, "%c",c);
                sprintf( buffer, "%d",i);
                sprintf( buffer, "%d",j);
                sprintf( buffer, "%f",f);
                </pre>
                </li>
            </ul>
            <p><strong><font color=#6699ff>二、字符串转换为其它数据类型</font></strong><br>strcpy(temp,"123"); </p>
            <ul>
                <li><font color=#6699ff>短整型(int)</font><br>i = atoi(temp);
                <li><font color=#6699ff>长整型(long)</font><br>l = atol(temp);
                <li><font color=#6699ff>浮点(double)</font><br>d = atof(temp);
                <li><font color=#6699ff>CString变量</font><br>CString name = temp;
                <li><font color=#6699ff>BSTR变量</font> <br>BSTR bstrValue = ::SysAllocString(L"程序员"); <br>...///完成对bstrValue的使用<br>SysFreeString(bstrValue); <br>
                <li><font color=#6699ff>CComBSTR变量</font><br>CComBSTR类型变量可以直接赋值<br>CComBSTR bstrVar1("test");<br>CComBSTR bstrVar2(temp);<br>
                <li><font color=#6699ff>_bstr_t变量</font><br>_bstr_t类型的变量可以直接赋值<br>_bstr_t bstrVar1("test"); <br>_bstr_t bstrVar2(temp); <br><br></li>
            </ul>
            <p><strong><font color=#6699ff>三、其它数据类型转换到CString</font></strong><br>使用CString的成员函数Format来转换,例如:<br></p>
            <ul>
                <li>整数(int)<br>str.Format("%d",i);
                <li>浮点数(float)<br>str.Format("%f",i);
                <li>字符串指针(char *)等已经被CString构造函数支持的数据类型可以直接赋值<br>str = username;
                <li>对于Format所不支持的数据类型，可以通过上面所说的关于其它数据类型转化到char *的方法先转到char *，然后赋值给CString变量。<br></li>
            </ul>
            <p><strong><font color=#6699ff>四、BSTR、_bstr_t与CComBSTR</font></strong><br></p>
            <ul>
                <li>CComBSTR 是ATL对BSTR的封装，_bstr_t是C++对BSTR的封装,BSTR是32位指针,但并不直接指向字串的缓冲区。<br>char *转换到BSTR可以这样: <br>BSTR b=_com_util::ConvertStringToBSTR("数据");///使用前需要加上comutil.h和comsupp.lib<br>SysFreeString(bstrValue); <br>反之可以使用<br>char *p=_com_util::ConvertBSTRToString(b);<br>delete p;<br>具体可以参考一，二段落里的具体说明。<br><br>CComBSTR与_bstr_t对大量的操作符进行了重载，可以直接进行=,!=,==等操作，所以使用非常方便。<br>特别是_bstr_t,建议大家使用它。<br></li>
            </ul>
            <p>&#160;</p>
            <p><strong><font color=#6699ff>五、VARIANT 、_variant_t 与 COleVariant</font></strong><br></p>
            <ul>
                <li>VARIANT的结构可以参考头文件VC98\Include\OAIDL.H中关于结构体tagVARIANT的定义。<br>对于VARIANT变量的赋值：首先给vt成员赋值，指明数据类型，再对联合结构中相同数据类型的变量赋值，举个例子：<br>VARIANT va;<br>int a=2001;<br>va.vt=VT_I4;///指明整型数据<br>va.lVal=a; ///赋值<br><br>对于不马上赋值的VARIANT，最好先用Void VariantInit(VARIANTARG FAR* pvarg);进行初始化,其本质是将vt设置为VT_EMPTY,下表我们列举vt与常用数据的对应关系:<br><br>
                <table cellSpacing=1 cellPadding=0 width=792 bgColor=#333333 border=0>
                    <tbody>
                        <tr bgColor=#ffffff>
                            <td width=442>Byte bVal; </td>
                            <td width=338>// VT_UI1.</td>
                        </tr>
                        <tr bgColor=#ffffff>
                            <td width=442>Short iVal; </td>
                            <td width=338>// VT_I2.</td>
                        </tr>
                        <tr bgColor=#ffffff>
                            <td width=442>long lVal; </td>
                            <td width=338>// VT_I4.</td>
                        </tr>
                        <tr bgColor=#ffffff>
                            <td width=442>float fltVal; </td>
                            <td width=338>// VT_R4.</td>
                        </tr>
                        <tr bgColor=#ffffff>
                            <td width=442>double dblVal; </td>
                            <td width=338>// VT_R8.</td>
                        </tr>
                        <tr bgColor=#ffffff>
                            <td width=442>VARIANT_BOOL boolVal; </td>
                            <td width=338>// VT_BOOL.</td>
                        </tr>
                        <tr bgColor=#ffffff>
                            <td width=442>SCODE scode; </td>
                            <td width=338>// VT_ERROR.</td>
                        </tr>
                        <tr bgColor=#ffffff>
                            <td width=442>CY cyVal; </td>
                            <td width=338>// VT_CY.</td>
                        </tr>
                        <tr bgColor=#ffffff>
                            <td width=442>DATE date; </td>
                            <td width=338>// VT_DATE.</td>
                        </tr>
                        <tr bgColor=#ffffff>
                            <td width=442>BSTR bstrVal; </td>
                            <td width=338>// VT_BSTR.</td>
                        </tr>
                        <tr bgColor=#ffffff>
                            <td width=442>DECIMAL FAR* pdecVal </td>
                            <td width=338>// VT_BYREF|VT_DECIMAL.</td>
                        </tr>
                        <tr bgColor=#ffffff>
                            <td width=442>IUnknown FAR* punkVal; </td>
                            <td width=338>// VT_UNKNOWN.</td>
                        </tr>
                        <tr bgColor=#ffffff>
                            <td width=442>IDispatch FAR* pdispVal; </td>
                            <td width=338>// VT_DISPATCH.</td>
                        </tr>
                        <tr bgColor=#ffffff>
                            <td width=442>SAFEARRAY FAR* parray; </td>
                            <td width=338>// VT_ARRAY|*.</td>
                        </tr>
                        <tr bgColor=#ffffff>
                            <td width=442>Byte FAR* pbVal; </td>
                            <td width=338>// VT_BYREF|VT_UI1.</td>
                        </tr>
                        <tr bgColor=#ffffff>
                            <td width=442>short FAR* piVal; </td>
                            <td width=338>// VT_BYREF|VT_I2.</td>
                        </tr>
                        <tr bgColor=#ffffff>
                            <td width=442>long FAR* plVal; </td>
                            <td width=338>// VT_BYREF|VT_I4.</td>
                        </tr>
                        <tr bgColor=#ffffff>
                            <td width=442>float FAR* pfltVal; </td>
                            <td width=338>// VT_BYREF|VT_R4.</td>
                        </tr>
                        <tr bgColor=#ffffff>
                            <td width=442>double FAR* pdblVal; </td>
                            <td width=338>// VT_BYREF|VT_R8.</td>
                        </tr>
                        <tr bgColor=#ffffff>
                            <td width=442>VARIANT_BOOL FAR* pboolVal; </td>
                            <td width=338>// VT_BYREF|VT_BOOL.</td>
                        </tr>
                        <tr bgColor=#ffffff>
                            <td width=442>SCODE FAR* pscode; </td>
                            <td width=338>// VT_BYREF|VT_ERROR.</td>
                        </tr>
                        <tr bgColor=#ffffff>
                            <td width=442>CY FAR* pcyVal; </td>
                            <td width=338>// VT_BYREF|VT_CY.</td>
                        </tr>
                        <tr bgColor=#ffffff>
                            <td width=442>DATE FAR* pdate; </td>
                            <td width=338>// VT_BYREF|VT_DATE.</td>
                        </tr>
                        <tr bgColor=#ffffff>
                            <td width=442>BSTR FAR* pbstrVal; </td>
                            <td width=338>// VT_BYREF|VT_BSTR.</td>
                        </tr>
                        <tr bgColor=#ffffff>
                            <td width=442>IUnknown FAR* FAR* ppunkVal; </td>
                            <td width=338>// VT_BYREF|VT_UNKNOWN.</td>
                        </tr>
                        <tr bgColor=#ffffff>
                            <td width=442>IDispatch FAR* FAR* ppdispVal; </td>
                            <td width=338>// VT_BYREF|VT_DISPATCH.</td>
                        </tr>
                        <tr bgColor=#ffffff>
                            <td width=442>SAFEARRAY FAR* FAR* pparray; </td>
                            <td width=338>// VT_ARRAY|*.</td>
                        </tr>
                        <tr bgColor=#ffffff>
                            <td width=442>VARIANT FAR* pvarVal; </td>
                            <td width=338>// VT_BYREF|VT_VARIANT.</td>
                        </tr>
                        <tr bgColor=#ffffff>
                            <td width=442>void FAR* byref; </td>
                            <td width=338>// Generic ByRef.</td>
                        </tr>
                        <tr bgColor=#ffffff>
                            <td width=442>char cVal; </td>
                            <td width=338>// VT_I1.</td>
                        </tr>
                        <tr bgColor=#ffffff>
                            <td width=442>unsigned short uiVal; </td>
                            <td width=338>// VT_UI2.</td>
                        </tr>
                        <tr bgColor=#ffffff>
                            <td width=442>unsigned long ulVal; </td>
                            <td width=338>// VT_UI4.</td>
                        </tr>
                        <tr bgColor=#ffffff>
                            <td width=442>int intVal; </td>
                            <td width=338>// VT_INT.</td>
                        </tr>
                        <tr bgColor=#ffffff>
                            <td width=442>unsigned int uintVal; </td>
                            <td width=338>// VT_UINT.</td>
                        </tr>
                        <tr bgColor=#ffffff>
                            <td width=442>char FAR * pcVal; </td>
                            <td width=338>// VT_BYREF|VT_I1.</td>
                        </tr>
                        <tr bgColor=#ffffff>
                            <td width=442>unsigned short FAR * puiVal; </td>
                            <td width=338>// VT_BYREF|VT_UI2.</td>
                        </tr>
                        <tr bgColor=#ffffff>
                            <td width=442>unsigned long FAR * pulVal; </td>
                            <td width=338>// VT_BYREF|VT_UI4.</td>
                        </tr>
                        <tr bgColor=#ffffff>
                            <td width=442>int FAR * pintVal; </td>
                            <td width=338>// VT_BYREF|VT_INT.</td>
                        </tr>
                        <tr bgColor=#ffffff>
                            <td width=442>unsigned int FAR * puintVal; </td>
                            <td width=338>//VT_BYREF|VT_UINT.</td>
                        </tr>
                    </tbody>
                </table>
                <br>
                <li>_variant_t是VARIANT的封装类，其赋值可以使用强制类型转换，其构造函数会自动处理这些数据类型。<br>使用时需加上#include &lt;comdef.h&gt;<br>例如：<br>long l=222;<br>ing i=100;<br>_variant_t lVal(l);<br>lVal = (long)i;<br><br>
                <li>COleVariant的使用与_variant_t的方法基本一样，请参考如下例子：<br>COleVariant v3 = "字符串", v4 = (long)1999;<br>CString str =(BSTR)v3.pbstrVal;<br>long i = v4.lVal;<br><br></li>
            </ul>
            <p><font color=#6699ff><strong>六、其它一些COM数据类型</strong></font></p>
            <ul>
                <li>根据ProgID得到CLSID<br>HRESULT CLSIDFromProgID( LPCOLESTR lpszProgID,LPCLSID pclsid);<br>CLSID clsid;<br>CLSIDFromProgID( L"MAPI.Folder",&amp;clsid);<br>
                <li>根据CLSID得到ProgID<br>WINOLEAPI ProgIDFromCLSID( REFCLSID clsid,LPOLESTR * lplpszProgID); <br>例如我们已经定义了 CLSID_IApplication,下面的代码得到ProgID<br>LPOLESTR pProgID = 0;<br>ProgIDFromCLSID( CLSID_IApplication,&amp;pProgID);<br>...///可以使用pProgID <br>CoTaskMemFree(pProgID);//不要忘记释放 <br></li>
            </ul>
            <p><font color=#6699ff><strong>七、ANSI与Unicode<br></strong></font>Unicode称为宽字符型字串,COM里使用的都是Unicode字符串。</p>
            <ul>
                <li>将ANSI转换到Unicode<br>(1)通过L这个宏来实现，例如: CLSIDFromProgID( L"MAPI.Folder",&amp;clsid);<br>(2)通过MultiByteToWideChar函数实现转换,例如:<br>char *szProgID = "MAPI.Folder";<br>WCHAR szWideProgID[128];<br>CLSID clsid;<br>long lLen = MultiByteToWideChar(CP_ACP,0,szProgID,strlen(szProgID),szWideProgID,sizeof(szWideProgID));<br>szWideProgID[lLen] = '\0'; <br>(3)通过A2W宏来实现,例如: <br>USES_CONVERSION; <br>CLSIDFromProgID( A2W(szProgID),&amp;clsid);
                <li>将Unicode转换到ANSI<br>(1)使用WideCharToMultiByte,例如:<br>// 假设已经有了一个Unicode 串 wszSomeString... <br>char szANSIString [MAX_PATH]; <br>WideCharToMultiByte ( CP_ACP, WC_COMPOSITECHECK, wszSomeString, -1, szANSIString, sizeof(szANSIString), NULL, NULL ); <br>(2)使用W2A宏来实现,例如:<br>USES_CONVERSION;<br>pTemp=W2A(wszSomeString); </li>
            </ul>
            <p><font color=#6699ff><strong>八、其它</strong></font></p>
            <ul>
                <li>对消息的处理中我们经常需要将WPARAM或LPARAM等32位数据（DWORD)分解成两个16位数据（WORD),例如：<br>LPARAM lParam;<br>WORD loValue = LOWORD(lParam);///取低16位<br>WORD hiValue = HIWORD(lParam);///取高16位<br><br>
                <li>对于16位的数据(WORD)我们可以用同样的方法分解成高低两个8位数据(BYTE),例如:<br>WORD wValue;<br>BYTE loValue = LOBYTE(wValue);///取低8位<br>BYTE hiValue = HIBYTE(wValue);///取高8位<br><br>
                <li>两个16位数据（WORD）合成32位数据(DWORD,LRESULT,LPARAM,或WPARAM)<br>LONG MAKELONG( WORD wLow, WORD wHigh );<br>WPARAM MAKEWPARAM( WORD wLow, WORD wHigh ); <br>LPARAM MAKELPARAM( WORD wLow, WORD wHigh );<br>LRESULT MAKELRESULT( WORD wLow, WORD wHigh ); <br><br>
                <li>两个8位的数据(BYTE)合成16位的数据(WORD)<br>WORD MAKEWORD( BYTE bLow, BYTE bHigh ); <br><br>
                <li>从R(red),G(green),B(blue)三色得到COLORREF类型的颜色值<br>COLORREF RGB( BYTE byRed,BYTE byGreen,BYTE byBlue );<br>例如COLORREF bkcolor = RGB(0x22,0x98,0x34);<br><br>
                <li>从COLORREF类型的颜色值得到RGB三个颜色值<br>BYTE Red = GetRValue(bkcolor); ///得到红颜色<br>BYTE Green = GetGValue(bkcolor); ///得到绿颜色<br>BYTE Blue = GetBValue(bkcolor); ///得到兰颜色<br></li>
            </ul>
            <p><font color=#6699ff><strong>九、注意事项</strong></font><br>假如需要使用到ConvertBSTRToString此类函数,需要加上头文件comutil.h,并在setting中加入comsupp.lib或者直接加上#pragma comment( lib, "comsupp.lib" )</p>
            <p>后记：本文匆匆写成，错误之处在所难免，欢迎指正.</p>
            </td>
        </tr>
    </tbody>
</table>
<img src ="http://www.cppblog.com/jacky2019/aggbug/35657.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/jacky2019/" target="_blank">小熊</a> 2007-11-01 09:30 <a href="http://www.cppblog.com/jacky2019/archive/2007/11/01/35657.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>“休息”了半年</title><link>http://www.cppblog.com/jacky2019/archive/2007/10/29/35443.html</link><dc:creator>小熊</dc:creator><author>小熊</author><pubDate>Mon, 29 Oct 2007 05:34:00 GMT</pubDate><guid>http://www.cppblog.com/jacky2019/archive/2007/10/29/35443.html</guid><wfw:comment>http://www.cppblog.com/jacky2019/comments/35443.html</wfw:comment><comments>http://www.cppblog.com/jacky2019/archive/2007/10/29/35443.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/jacky2019/comments/commentRss/35443.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/jacky2019/services/trackbacks/35443.html</trackback:ping><description><![CDATA[我又回来了。<img height=20 src="http://www.cppblog.com/Emoticons/QQ/06.gif" width=20 border=0><br><br>人生就像龟兔赛跑。一开始领先不代表你会一直领先。同样，一开始落后不代表你会一直落后。但是如果你下不了决心，不可能努力，没有自制力，没毅力的话，你肯定不会领先的。<br><br>乐观，开朗，积极的态度。好长时间没有体会到领先带给我的乐趣了；好长时间没有体会到学习技术带给我的快乐了。该重新归队，好好学习，好好工作了。<br><br>废话不多说，开工。<img height=20 src="http://www.cppblog.com/Emoticons/QQ/12.gif" width=20 border=0>
<img src ="http://www.cppblog.com/jacky2019/aggbug/35443.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/jacky2019/" target="_blank">小熊</a> 2007-10-29 13:34 <a href="http://www.cppblog.com/jacky2019/archive/2007/10/29/35443.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>python与c++交互学习入门之5</title><link>http://www.cppblog.com/jacky2019/archive/2007/06/06/25662.html</link><dc:creator>小熊</dc:creator><author>小熊</author><pubDate>Wed, 06 Jun 2007 08:25:00 GMT</pubDate><guid>http://www.cppblog.com/jacky2019/archive/2007/06/06/25662.html</guid><wfw:comment>http://www.cppblog.com/jacky2019/comments/25662.html</wfw:comment><comments>http://www.cppblog.com/jacky2019/archive/2007/06/06/25662.html#Feedback</comments><slash:comments>4</slash:comments><wfw:commentRss>http://www.cppblog.com/jacky2019/comments/commentRss/25662.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/jacky2019/services/trackbacks/25662.html</trackback:ping><description><![CDATA[<p>这次讲讲，如何扩展c++库。通过boost.python把c++库编译成python能够调用的dll。</p>
<p><br>通过上一次的教程后，大家都应该会使用boost.python了。把c++程序编译成pyd文件。由于c++有很多特性，所以，如果你的程</p>
<p>序用了很多的c++特性的话，那么你必须做很多工作了。像虚拟函数，函数重载，继承，默认值等等。具体如何转化，请参</p>
<p>boost.python的文档了。</p>
<p><br>这几天尝试着把c++程序库编译成python可调用的dll，不知道为什么一直不可用。。很是郁闷。老是显示如下的错误：</p>
<p>Traceback (most recent call last):<br>&nbsp; File "&lt;pyshell#3&gt;", line 1, in &lt;module&gt;<br>&nbsp;&nbsp;&nbsp; import pydll<br>ImportError: No module named pydll</p>
<p>意思是说找不到dll。我把dll都copy到python/dlls下了还是不行，而且我确定python的sys.path包含了python/dlls目录了。</p>
<p>很是不解。网上也很难找到资料，google了很长时间找不到有用的资料，好像中文方面的资料很少的。今天尝试了一下google</p>
<p>英文资料，终于有了新的发现：<br><a href="http://mail.python.org/pipermail/c++-sig/2007-February/011971.html">http://mail.python.org/pipermail/c++-sig/2007-February/011971.html</a><br>You are using Python2.5. In this version of Python you have to have<br>file extension<br>to be "pyd" - sge.pyd</p>
<p>-- <br>Roman Yakovenko<br>C++ Python language binding<br><a href="http://www.language-binding.net/">http://www.language-binding.net/</a></p>
<p>有人碰到的问题跟我的是一样的。后面那个Roman回答了一下，是文件扩展名的问题！！！为什么不支持dll呢？不解。回去试</p>
<p>了一下把后缀名改了就成功了。。。why???</p>
<p><br>下面来看一下我的那个简单的例子：<br>这个例子来自于网上，<br><a href="http://www.vckbase.com/document/viewdoc/?id=1540">http://www.vckbase.com/document/viewdoc/?id=1540</a><br>C++ 扩展和嵌入 Python<br>作者：胡金山<br>源码下载地址：<a href="http://www.vckbase.com/code/downcode.asp?id=2777">http://www.vckbase.com/code/downcode.asp?id=2777</a></p>
<p><br>这是一个非常简单的dll工程。给python提供了一个函数static PyObject* Recognise(PyObject *self, PyObject *args)。</p>
<p><br>1、不使用boost.python库来直接构建dll<br>接下来，我们来用C++为Python编写扩展模块(动态链接库)，并在Python程序中调用C++开发的扩展功能函数。生成一个取名为</p>
<p>pyUtil的Win32 DLL工程，除了pyUtil.cpp文件以外，从工程中移除所有其它文件，并填入如下的代码： </p>
<p>// pyUtil.cpp<br>#ifdef PYUTIL_EXPORTS<br>#define PYUTIL_API __declspec(dllexport)<br>#else<br>#define PYUTIL_API __declspec(dllimport)<br>#endif</p>
<p>#include&lt;windows.h&gt;<br>#include&lt;string&gt;<br>#include&lt;Python.h&gt;<br>BOOL APIENTRY DllMain( HANDLE hModule, <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DWORD&nbsp; ul_reason_for_call, <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; LPVOID lpReserved<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; )<br>{<br>&nbsp;&nbsp;&nbsp; switch (ul_reason_for_call)<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp; case DLL_PROCESS_ATTACH:<br>&nbsp;&nbsp;&nbsp; case DLL_THREAD_ATTACH:<br>&nbsp;&nbsp;&nbsp; case DLL_THREAD_DETACH:<br>&nbsp;&nbsp;&nbsp; case DLL_PROCESS_DETACH:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;<br>&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; return TRUE;<br>}<br>std::string Recognise_Img(const std::string url)<br>{<br>&nbsp;&nbsp;&nbsp; //返回结果<br>&nbsp;&nbsp;&nbsp; return "从dll中返回的数据... : " +url;<br>}<br>static PyObject* Recognise(PyObject *self, PyObject *args)<br>{<br>&nbsp;&nbsp;&nbsp; const char *url;<br>&nbsp;&nbsp;&nbsp; std::string sts;<br>&nbsp;&nbsp;&nbsp; if (!PyArg_ParseTuple(args, "s", &amp;url))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return NULL;<br>&nbsp;&nbsp;&nbsp; sts = Recognise_Img(url);<br>&nbsp;&nbsp;&nbsp; return Py_BuildValue("s", sts.c_str() );<br>}<br>static PyMethodDef AllMyMethods[] = {<br>&nbsp;&nbsp;&nbsp; {"Recognise",&nbsp; Recognise, METH_VARARGS},//暴露给Python的函数<br>&nbsp;&nbsp;&nbsp; {NULL,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NULL}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Sentinel */<br>};<br>extern "C" PYUTIL_API void initpyUtil()<br>{<br>&nbsp;&nbsp;&nbsp; PyObject *m, *d;<br>&nbsp;&nbsp;&nbsp; m = Py_InitModule("pyUtil", AllMyMethods); //初始化本模块，并暴露函数<br>&nbsp;&nbsp;&nbsp; d = PyModule_GetDict(m);<br>}</p>
<p>在Python代码中调用这个动态链接库： (记得把dll的扩展名改为.pyd，另外dll的路径要能够被检索到)<br>import pyUtil<br>result = pyUtil.Recognise("input url of specific data")<br>print "the result is: "+ result</p>
<p>&nbsp;</p>
<p>2、使用boost.python库来构建dll<br>用C++为Python写扩展时，如果您愿意使用Boost.Python库的话，开发过程会变得更开心J，要编写一个与上述pyUtil同样功能</p>
<p>的动态链接库，只需把文件内容替换为下面的代码。当然，编译需要boost_python.lib支持，运行需要boost_python.dll支持</p>
<p>。 <br>#include&lt;string&gt;<br>#include &lt;boost/python.hpp&gt;<br>using namespace boost::python;<br>#pragma comment(lib, "boost_python.lib")<br>std::string strtmp;<br>char const* Recognise(const char* url)<br>{<br>&nbsp;&nbsp;&nbsp; strtmp ="从dll中返回的数据... : ";<br>&nbsp;&nbsp;&nbsp; strtmp+=url;<br>&nbsp;&nbsp;&nbsp; return strtmp.c_str();<br>}<br>BOOST_PYTHON_MODULE(pyUtil)<br>{<br>&nbsp;&nbsp;&nbsp; def("Recognise", Recognise);<br>}</p>
<p>可以非常明显地看到，用了boost.python库之后，简单了很多。因为boost.python为你做了很多的事情。。恩。</p>
<p>&nbsp;</p>
<p>好像没有讲很多有用的东西，光顾着讲了，呵呵。。。我也还在继续学习之中。下次写点什么呢？继续学习了哦</p>
<p>&nbsp;</p>
<img src ="http://www.cppblog.com/jacky2019/aggbug/25662.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/jacky2019/" target="_blank">小熊</a> 2007-06-06 16:25 <a href="http://www.cppblog.com/jacky2019/archive/2007/06/06/25662.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>c++中嵌入python入门4 之 Boost.Python</title><link>http://www.cppblog.com/jacky2019/archive/2007/06/01/25254.html</link><dc:creator>小熊</dc:creator><author>小熊</author><pubDate>Fri, 01 Jun 2007 02:32:00 GMT</pubDate><guid>http://www.cppblog.com/jacky2019/archive/2007/06/01/25254.html</guid><wfw:comment>http://www.cppblog.com/jacky2019/comments/25254.html</wfw:comment><comments>http://www.cppblog.com/jacky2019/archive/2007/06/01/25254.html#Feedback</comments><slash:comments>5</slash:comments><wfw:commentRss>http://www.cppblog.com/jacky2019/comments/commentRss/25254.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/jacky2019/services/trackbacks/25254.html</trackback:ping><description><![CDATA[<p>坏境python25 + vs2005 (2005真耗资源阿。。。)</p>
<p>有一段时间没写blog了。这几天都在研究怎么封装c++，让python可以用c++的库。在网上发现了boost.python这个好咚咚。不</p>
<p>过在使用过程中碰到一点问题。本文教大家如何把</p>
<p>char const* greet()<br>{<br>&nbsp;&nbsp; return "hello, world";<br>}</p>
<p>封装成python。实际上这是python教程里面的咚咚。</p>
<p><br>首先下载Boost，<a href="http://www.boost.org/">www.boost.org</a>。boost.python在boost里面了。在visual studio 2005 command prompt中navigation到</p>
<p>boost\boost_1_34_0\下。记得一定要用visual studio 2005 command prompt这个vs2005带的tools，不要用cmd.exe，否则会</p>
<p>碰到很多错误的。然后就是把bjam.exe拷贝到一个能被找到的目录下，或者直接也拷贝到boost\boost_1_34_0\下即可。然后，</p>
<p>设置python的根目录和python的版本，也可直接把它们加到坏境目录中，那样就不用每次都设置一下。<br>set PYTHON_ROOT=c:/python25<br>set PYTHON_VERSION=2.5</p>
<p>接着就可以直接运行了，bjam -sTOOLS=vc-8_0<br>整个编译过程要很长时间。。。</p>
<p>成功之后，就会有好多个boost_python-vc80-****.dll,.lib的，把他们都拷贝到一个能被系统找到的目录，不妨直接把他们都</p>
<p>扔到c:\windows\system32下。</p>
<p>接着，我们开始编译hello。navigation到boost\boost_1_34_0\libs\python\example\tutorial下，bjam -sTOOLS=vc-8_0运行</p>
<p>，在bin的目录下即会生成hello.pyd。这下就基本成功了，如果没成功的话，check一下上面boost_python的那些dll能否被系</p>
<p>统找到。另外，这里有python25的一个bug。。。我花了很长时间才在python的mail lists中找到了。寒。。。</p>
<p>错误如下所示：<br>D:\Learn\Python\boost\boost_1_34_0\libs\python\example\tutorial&gt;bjam<br>Jamroot:17: in modules.load<br>rule python-extension unknown in module Jamfile&lt;/D:/Learn/Python/boost/boost_1_3<br>4_0/libs/python/example/tutorial&gt;.<br>D:/Learn/Python/boost/boost_1_34_0/tools/build/v2/build\project.jam:312: in load<br>-jamfile<br>D:/Learn/Python/boost/boost_1_34_0/tools/build/v2/build\project.jam:68: in load<br>D:/Learn/Python/boost/boost_1_34_0/tools/build/v2/build\project.jam:170: in proj<br>ect.find<br>D:/Learn/Python/boost/boost_1_34_0/tools/build/v2\build-system.jam:237: in load<br>D:\Learn\Python\boost\boost_1_34_0\libs\python\example\..\..\..\tools\build\v2/k<br>ernel\modules.jam:261: in import<br>D:\Learn\Python\boost\boost_1_34_0\libs\python\example\..\..\..\tools\build\v2/k<br>ernel/bootstrap.jam:132: in boost-build<br>D:\Learn\Python\boost\boost_1_34_0\libs\python\example\boost-build.jam:7: in mod<br>ule scope</p>
<p>解决办法如下：<br>在boost\boost_1_34_0\tools\build\v2\目录下找到user-config.jam文件，打开在<br>import toolset : using ;<br>下面加一行代码：<br>using python ;<br>再重新编译一下boost，然后就没问题了。tutorial里面的hello能顺利编译通过。ps.这个问题困扰了我好长时间。。sigh。。</p>
<p>。</p>
<p>编译成功后会产生一个hello.pyd，在bin的目录下面。</p>
<p><br>有好多办法测试此hello.pyd是否可以用。<br>方法一，把它拷贝到python25\dlls下，打开IDLE，<br>&gt;&gt;&gt; import hello<br>&gt;&gt;&gt; hello.greet()<br>'hello, world'<br>&gt;&gt;&gt; <br>方法二，直接在当前目录下写一个python文件，然后直接调用hello.pyd即可。总之，hello.pyd就是一个python文件了。。嗯</p>
<p>。操作hello.pyd根其他python文件是一样的。<br>这样就成功了。</p>
<p>如果碰到如下错误，是因为系统找不到boost_python的dll。强烈建议把他们都扔到system32下！。</p>
<p>&gt;&gt;&gt; import hello</p>
<p>Traceback (most recent call last):<br>&nbsp; File "&lt;pyshell#0&gt;", line 1, in &lt;module&gt;<br>&nbsp;&nbsp;&nbsp; import hello<br>ImportError: DLL load failed: 找不到指定的模块。<br>&gt;&gt;&gt; </p>
<p><br>说明，hello.cpp在boost\boost_1_34_0\libs\python\example\tutorial目录下。里面的内容是：</p>
<p>//&nbsp; Copyright Joel de Guzman 2002-2004. Distributed under the Boost<br>//&nbsp; Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt <br>//&nbsp; or copy at <a href="http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</a>)<br>//&nbsp; Hello World Example from the tutorial<br>//&nbsp; [Joel de Guzman 10/9/2002]</p>
<p>char const* greet()<br>{<br>&nbsp;&nbsp; return "hello, world";<br>}</p>
<p>#include &lt;boost/python/module.hpp&gt;<br>#include &lt;boost/python/def.hpp&gt;<br>using namespace boost::python;</p>
<p>BOOST_PYTHON_MODULE(hello)<br>{<br>&nbsp;&nbsp;&nbsp; def("greet", greet);<br>}</p>
<p><br>其中<br>BOOST_PYTHON_MODULE(hello)<br>{<br>&nbsp;&nbsp;&nbsp; def("greet", greet);<br>}<br>是对greet从c++向python的一个封装声明吧，装换就交给boost了。</p>
<p>&nbsp;</p>
<p>先写到这里了。下次再写。。嗯<br></p>
<img src ="http://www.cppblog.com/jacky2019/aggbug/25254.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/jacky2019/" target="_blank">小熊</a> 2007-06-01 10:32 <a href="http://www.cppblog.com/jacky2019/archive/2007/06/01/25254.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>笔记本主板坏了</title><link>http://www.cppblog.com/jacky2019/archive/2007/05/31/25203.html</link><dc:creator>小熊</dc:creator><author>小熊</author><pubDate>Thu, 31 May 2007 07:58:00 GMT</pubDate><guid>http://www.cppblog.com/jacky2019/archive/2007/05/31/25203.html</guid><wfw:comment>http://www.cppblog.com/jacky2019/comments/25203.html</wfw:comment><comments>http://www.cppblog.com/jacky2019/archive/2007/05/31/25203.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/jacky2019/comments/commentRss/25203.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/jacky2019/services/trackbacks/25203.html</trackback:ping><description><![CDATA[郁闷，极度郁闷。。。
<img src ="http://www.cppblog.com/jacky2019/aggbug/25203.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/jacky2019/" target="_blank">小熊</a> 2007-05-31 15:58 <a href="http://www.cppblog.com/jacky2019/archive/2007/05/31/25203.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>c++中嵌入python入门3</title><link>http://www.cppblog.com/jacky2019/archive/2007/05/17/24286.html</link><dc:creator>小熊</dc:creator><author>小熊</author><pubDate>Thu, 17 May 2007 07:16:00 GMT</pubDate><guid>http://www.cppblog.com/jacky2019/archive/2007/05/17/24286.html</guid><wfw:comment>http://www.cppblog.com/jacky2019/comments/24286.html</wfw:comment><comments>http://www.cppblog.com/jacky2019/archive/2007/05/17/24286.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/jacky2019/comments/commentRss/24286.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/jacky2019/services/trackbacks/24286.html</trackback:ping><description><![CDATA[<p>这次主要讲讲怎么把python中的class嵌入到c++中去。<br>顺便讲讲元组的操作和怎么编译python源代码。</p>
<p>1. 首先讲讲元组的操作<br>由于参数是通过元组传进去的，所以我们不能老是通过Py_BuildValue这个函数来操作元组，那样太不方便了。<br>Python提供了元组相关的操作，下面这个例子演示了如何操作。主要是下面几个函数：<br>//new一个元组，传入size<br>pArgs = PyTuple_New(argc - 3);&nbsp;<br>//set元组的直，第一个为元组，第二个为index（从0开始），第三个为value<br>PyTuple_SetItem(pArgs,0,Py_BuildValue("i",2000) );<br>PyTuple_SetItem(pArgs,1,Py_BuildValue("i",8) );</p>
<p>来自python doc的一个例子</p>
<p>#include &lt;Python.h&gt;<br>int<br>main(int argc, char *argv[])<br>{<br>&nbsp;&nbsp;&nbsp; PyObject *pName, *pModule, *pDict, *pFunc;<br>&nbsp;&nbsp;&nbsp; PyObject *pArgs, *pValue;<br>&nbsp;&nbsp;&nbsp; int i;</p>
<p>&nbsp;&nbsp;&nbsp; if (argc &lt; 3) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fprintf(stderr,"Usage: call pythonfile funcname [args]\n");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return 1;<br>&nbsp;&nbsp;&nbsp; }</p>
<p>&nbsp;&nbsp;&nbsp; Py_Initialize();<br>&nbsp;&nbsp;&nbsp; pName = PyString_FromString(argv[1]);<br>&nbsp;&nbsp;&nbsp; /* Error checking of pName left out */</p>
<p>&nbsp;&nbsp;&nbsp; pModule = PyImport_Import(pName);<br>&nbsp;&nbsp;&nbsp; Py_DECREF(pName);</p>
<p>&nbsp;&nbsp;&nbsp; if (pModule != NULL) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pFunc = PyObject_GetAttrString(pModule, argv[2]);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* pFunc is a new reference */</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (pFunc &amp;&amp; PyCallable_Check(pFunc)) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pArgs = PyTuple_New(argc - 3);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (i = 0; i &lt; argc - 3; ++i) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pValue = PyInt_FromLong(atoi(argv[i + 3]));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!pValue) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Py_DECREF(pArgs);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Py_DECREF(pModule);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fprintf(stderr, "Cannot convert argument\n");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return 1;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* pValue reference stolen here: */<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PyTuple_SetItem(pArgs, i, pValue);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pValue = PyObject_CallObject(pFunc, pArgs);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Py_DECREF(pArgs);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (pValue != NULL) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("Result of call: %ld\n", PyInt_AsLong(pValue));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Py_DECREF(pValue);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Py_DECREF(pFunc);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Py_DECREF(pModule);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PyErr_Print();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fprintf(stderr,"Call failed\n");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return 1;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (PyErr_Occurred())<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PyErr_Print();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fprintf(stderr, "Cannot find function \"%s\"\n", argv[2]);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Py_XDECREF(pFunc);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Py_DECREF(pModule);<br>&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; else {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PyErr_Print();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fprintf(stderr, "Failed to load \"%s\"\n", argv[1]);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return 1;<br>&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; Py_Finalize();<br>&nbsp;&nbsp;&nbsp; return 0;<br>}</p>
<p><br>2. class操作<br>把下面加入到test2.py中去。定义了一个很简单的类，有一个name成员变量，一个printName成员函数<br>class TestClass:<br>&nbsp;&nbsp;&nbsp; def __init__(self,name):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.name = name</p>
<p>&nbsp;&nbsp;&nbsp; def printName(self):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print self.name</p>
<p>cpp文件<br>#include &lt;python.h&gt;<br>int main()<br>{<br>&nbsp;Py_Initialize();</p>
<p>&nbsp;PyObject * pModule = NULL;<br>&nbsp;PyObject * pFunc&nbsp;&nbsp; = NULL;<br>&nbsp;PyObject * pArg&nbsp;&nbsp;&nbsp; = NULL;<br>&nbsp;PyObject * pClass&nbsp; = NULL;<br>&nbsp;PyObject * pObject = NULL;</p>
<p>&nbsp;pModule = PyImport_ImportModule("test2");<br>&nbsp;pClass&nbsp; = PyObject_GetAttrString(pModule, "TestClass");//得到那个类<br>&nbsp;pArg = PyTuple_New(1);<br>&nbsp;PyTuple_SetItem(pArg, 0, Py_BuildValue("s", "Jacky"));<br>&nbsp;pObject = PyEval_CallObject(pClass, pArg);//生成一个对象，或者叫作实例</p>
<p>&nbsp;pFunc = PyObject_GetAttrString(pObject, "printName");//得到该实例的成员函数<br>&nbsp;PyEval_CallObject(pFunc, NULL);//执行该实例的成员函数</p>
<p>&nbsp;Py_Finalize();</p>
<p>&nbsp;return 0;<br>}</p>
<p><br>没有什么资料，就先写到这里了。下面介绍一下怎么build python25的源代码</p>
<p>3. 编译python源代码<br>为什么要编译呢？因为没有python25_d.lib！呵呵。顺便可以了解一下代码结构。<br>解压缩后，有好多目录，其中pcbuild和pcbuild8是我们要的。pcbuild对应着vc7.1的,pcbuild8对应着vc8.0的<br>因为在用vc7.1，也就是2003了。所以我就说说怎么用2003来编译吧。事实上是从一位牛人那里学来的</p>
<p><a href="http://blog.donews.com/lemur/archive/2005/12/17/660973.aspx">http://blog.donews.com/lemur/archive/2005/12/17/660973.aspx</a>，那位大哥大概一年半前就在解剖python了，厉害</p>
<p>阿。看来我只能后来居上了，娃哈哈。我按照他说的试了一下，编译成功！</p>
<p>不过遇到一点小问题，用vc2003打开那个solution的时候，发现作者没有把source code control去掉，郁闷！害的我</p>
<p>们打开的时候一堆messagebox。不过不用管它就好了，一直确定。最后试了一下那个python25_d.lib，没问题。不过记</p>
<p>得把python25_d.dll copy到一个能被找到的目录，比如说c:\windows\system32\下面。python25.dll也在这个目录下</p>
<p>面。over。恩。</p>
<p>&nbsp;</p>
<img src ="http://www.cppblog.com/jacky2019/aggbug/24286.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/jacky2019/" target="_blank">小熊</a> 2007-05-17 15:16 <a href="http://www.cppblog.com/jacky2019/archive/2007/05/17/24286.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>c++中嵌入python入门2</title><link>http://www.cppblog.com/jacky2019/archive/2007/05/17/24276.html</link><dc:creator>小熊</dc:creator><author>小熊</author><pubDate>Thu, 17 May 2007 03:28:00 GMT</pubDate><guid>http://www.cppblog.com/jacky2019/archive/2007/05/17/24276.html</guid><wfw:comment>http://www.cppblog.com/jacky2019/comments/24276.html</wfw:comment><comments>http://www.cppblog.com/jacky2019/archive/2007/05/17/24276.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/jacky2019/comments/commentRss/24276.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/jacky2019/services/trackbacks/24276.html</trackback:ping><description><![CDATA[<p>1. 一个有一个参数的例子</p>
<p>python文件<br>#Filename test2.py</p>
<p>def Hello(s):<br>&nbsp;&nbsp;&nbsp; print "Hello, world!"<br>&nbsp;&nbsp;&nbsp; print s</p>
<p>cpp文件<br>#include &lt;python.h&gt;<br>int main()<br>{<br>&nbsp;Py_Initialize();</p>
<p>&nbsp;PyObject * pModule = NULL;<br>&nbsp;PyObject * pFunc&nbsp;&nbsp; = NULL;<br>&nbsp;PyObject * pArg&nbsp;&nbsp;&nbsp; = NULL;</p>
<p>&nbsp;pModule = PyImport_ImportModule("test2");<br>&nbsp;pFunc&nbsp;&nbsp; = PyObject_GetAttrString(pModule, "Hello");<br>&nbsp;pArg&nbsp;&nbsp;&nbsp; = Py_BuildValue("(s)", "function with argument");</p>
<p>&nbsp;PyEval_CallObject(pFunc, pArg);</p>
<p>&nbsp;Py_Finalize();</p>
<p>&nbsp;return 0;<br>}</p>
<p>注意，参数要以tuple元组形式传入。因为这个函数只要一个参数，所以我们直接使用(s)构造一个元组了。</p>
<p>2. 一个有两个参数的例子</p>
<p>python文件中加入以下代码，一个加函数<br>def Add(a, b):<br>&nbsp;&nbsp;&nbsp; print "a+b=", a+b</p>
<p>cpp文件，只改了两行，有注释的那两行<br>#include &lt;python.h&gt;<br>int main()<br>{<br>&nbsp;Py_Initialize();</p>
<p>&nbsp;PyObject * pModule = NULL;<br>&nbsp;PyObject * pFunc&nbsp;&nbsp; = NULL;<br>&nbsp;PyObject * pArg&nbsp;&nbsp;&nbsp; = NULL;</p>
<p>&nbsp;pModule = PyImport_ImportModule("test2");<br>&nbsp;pFunc&nbsp;&nbsp; = PyObject_GetAttrString(pModule, "Add");//终于告别hello world了，开始使用新的函数<br>&nbsp;pArg&nbsp;&nbsp;&nbsp; = Py_BuildValue("(i,i)", 10, 15);//构造一个元组</p>
<p>&nbsp;PyEval_CallObject(pFunc, pArg);</p>
<p>&nbsp;Py_Finalize();</p>
<p>&nbsp;return 0;<br>}</p>
<p>其它的就类似了。。。基本上，我们知道了怎么在c++中使用python中的函数。接下来学习一下如何使用python中的</p>
<p>class。</p>
<p>附：Py_BuildValue的使用例子，来自python documentation：</p>
<p>&nbsp;&nbsp;&nbsp; Py_BuildValue("")&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; None<br>&nbsp;&nbsp;&nbsp; Py_BuildValue("i", 123)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 123<br>&nbsp;&nbsp;&nbsp; Py_BuildValue("iii", 123, 456, 789)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (123, 456, 789)<br>&nbsp;&nbsp;&nbsp; Py_BuildValue("s", "hello")&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'hello'<br>&nbsp;&nbsp;&nbsp; Py_BuildValue("ss", "hello", "world")&nbsp;&nbsp;&nbsp; ('hello', 'world')<br>&nbsp;&nbsp;&nbsp; Py_BuildValue("s#", "hello", 4)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'hell'<br>&nbsp;&nbsp;&nbsp; Py_BuildValue("()")&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ()<br>&nbsp;&nbsp;&nbsp; Py_BuildValue("(i)", 123)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (123,)<br>&nbsp;&nbsp;&nbsp; Py_BuildValue("(ii)", 123, 456)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (123, 456)<br>&nbsp;&nbsp;&nbsp; Py_BuildValue("(i,i)", 123, 456)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (123, 456)<br>&nbsp;&nbsp;&nbsp; Py_BuildValue("[i,i]", 123, 456)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [123, 456]<br>&nbsp;&nbsp;&nbsp; Py_BuildValue("{s:i,s:i}",<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "abc", 123, "def", 456)&nbsp;&nbsp;&nbsp; {'abc': 123, 'def': 456}<br>&nbsp;&nbsp;&nbsp; Py_BuildValue("((ii)(ii)) (ii)",<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1, 2, 3, 4, 5, 6)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (((1, 2), (3, 4)), (5, 6))<br></p>
<img src ="http://www.cppblog.com/jacky2019/aggbug/24276.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/jacky2019/" target="_blank">小熊</a> 2007-05-17 11:28 <a href="http://www.cppblog.com/jacky2019/archive/2007/05/17/24276.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>c++中嵌入python入门1</title><link>http://www.cppblog.com/jacky2019/archive/2007/05/17/24269.html</link><dc:creator>小熊</dc:creator><author>小熊</author><pubDate>Thu, 17 May 2007 03:03:00 GMT</pubDate><guid>http://www.cppblog.com/jacky2019/archive/2007/05/17/24269.html</guid><wfw:comment>http://www.cppblog.com/jacky2019/comments/24269.html</wfw:comment><comments>http://www.cppblog.com/jacky2019/archive/2007/05/17/24269.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/jacky2019/comments/commentRss/24269.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/jacky2019/services/trackbacks/24269.html</trackback:ping><description><![CDATA[<p>本人是用vc2003+python2.5学习的，其它的也应该差不了多少</p>
<p>0. 坏境设置<br>把python的include/libs目录分别加到vc的include/lib directories中去。另外，由于python没有提供debug lib，体地说，就是没有提供python25_d.lib了。你可以自己编译python的源代码来得到python25_d.lib的，偶还没试过，呵呵。而且网上找了一下也没下载到。所以，如果你想要在debug下运行程序的话，你要把pyconfig.h（在python25/include/目录下）的大概是在283行，把pragma comment(lib,"python25_d.lib")改成pragma comment(lib,"python25.lib")，让python都使用非debug lib.</p>
<p>1. 开始编程了<br>#include &lt;python.h&gt;<br>第一步就是包含python的头文件</p>
<p>2. 看一个很简单的例子<br>1)python文件test.py，很简单的定义了一个函数</p>
<p>#Filename test.py<br>def Hello():<br>&nbsp;&nbsp;&nbsp; print "Hello, world!"</p>
<p>这个应该能看懂的吧？否则的话，回去再练练python吧，呵呵。《简明Python教程》Swaroop, C. H. 著。沈洁元&nbsp; 译。</p>
<p><br>2)cpp文件</p>
<p>#include &lt;python.h&gt; //包含头文件，在c++中嵌入python，这是必须的<br>int main()<br>{<br>&nbsp;Py_Initialize();</p>
<p>&nbsp;PyObject * pModule = NULL;<br>&nbsp;PyObject * pFunc&nbsp;&nbsp; = NULL;</p>
<p>&nbsp;pModule = PyImport_ImportModule("test");<br>&nbsp;pFunc&nbsp;&nbsp; = PyObject_GetAttrString(pModule, "Hello");<br>&nbsp;PyEval_CallObject(pFunc, NULL);</p>
<p>&nbsp;Py_Finalize();</p>
<p>&nbsp;return 0;<br>}</p>
<p>第一步还是包含头文件</p>
<p>第二步，使用python之前，要调用Py_Initialize();这个函数进行初始化。<br>帮助文档中如是说：<br>The basic initialization function is Py_Initialize(). This initializes the table of loaded modules, and creates the fundamental modules __builtin__, __main__, sys, and exceptions. It also initializes the module search path (sys.path). </p>
<p>反正，一开始你一定要调用。</p>
<p>第三步，声明一些Python的变量，PyObject类型的。其实声明也可放在前面，这个倒是无所谓的。</p>
<p>第四步，import module，也就是你的脚本名字，不需要加后缀名，否则会出错的。</p>
<p>第五步，从你import进来的module中得到你要的函数<br>&nbsp;pFunc&nbsp;&nbsp; = PyObject_GetAttrString(pModule, "Hello");<br>上面的例子已经够清楚的了，最后一个是你要得到的函数的名字</p>
<p>第六步，调用PyEval_CallObject来执行你的函数，第二个参数为我们要调用的函数的函数，本例子不含参数，所以设置为NULL。</p>
<p>第七步，调用Py_Finalize，这个根Py_Initialize相对应的。一个在最前面，一个在最后面。</p>
<p>&nbsp;</p>
<p>第一次写教程。这个例子非常简单，本人也还在学习当中阿，只能保证大家能够把这个例子运行起来。建议大家去看python的documentaion，里面有讲怎么embedding python的。先写到这里，其实目前也只学到这么多，呵呵。下次学了更多以后再写。Over。恩。</p>
<p>&nbsp;</p>
<img src ="http://www.cppblog.com/jacky2019/aggbug/24269.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/jacky2019/" target="_blank">小熊</a> 2007-05-17 11:03 <a href="http://www.cppblog.com/jacky2019/archive/2007/05/17/24269.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>sigh</title><link>http://www.cppblog.com/jacky2019/archive/2007/05/08/23647.html</link><dc:creator>小熊</dc:creator><author>小熊</author><pubDate>Tue, 08 May 2007 13:14:00 GMT</pubDate><guid>http://www.cppblog.com/jacky2019/archive/2007/05/08/23647.html</guid><wfw:comment>http://www.cppblog.com/jacky2019/comments/23647.html</wfw:comment><comments>http://www.cppblog.com/jacky2019/archive/2007/05/08/23647.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/jacky2019/comments/commentRss/23647.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/jacky2019/services/trackbacks/23647.html</trackback:ping><description><![CDATA[sigh sigh sigh ...
<img src ="http://www.cppblog.com/jacky2019/aggbug/23647.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/jacky2019/" target="_blank">小熊</a> 2007-05-08 21:14 <a href="http://www.cppblog.com/jacky2019/archive/2007/05/08/23647.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>The difference between GetDC  and GetWindowDC</title><link>http://www.cppblog.com/jacky2019/archive/2007/04/21/22467.html</link><dc:creator>小熊</dc:creator><author>小熊</author><pubDate>Sat, 21 Apr 2007 04:54:00 GMT</pubDate><guid>http://www.cppblog.com/jacky2019/archive/2007/04/21/22467.html</guid><wfw:comment>http://www.cppblog.com/jacky2019/comments/22467.html</wfw:comment><comments>http://www.cppblog.com/jacky2019/archive/2007/04/21/22467.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/jacky2019/comments/commentRss/22467.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/jacky2019/services/trackbacks/22467.html</trackback:ping><description><![CDATA[<p><span>GetDC</span></p>
<p>The GetDC function retrieves a handle of a display device context (DC) for the client area of the specified window. The display device context can be used in subsequent GDI functions to draw in the client area of the window. </p>
<p>This function retrieves a common, class, or private device context depending on the class style specified for the specified window. For common device contexts, GetDC assigns default attributes to the device context each time it is retrieved. For class and private device contexts, GetDC leaves the previously assigned attributes unchanged. </p>
<p>HDC GetDC(</p>
<p><span>&nbsp;&nbsp;&nbsp; </span>HWND hWnd <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>// handle of window&nbsp;</p>
<p><span>&nbsp;&nbsp; </span>);<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></p>
<p>Parameters</p>
<p>hWnd</p>
<p>Identifies the window whose device context is to be retrieved.</p>
<p>&nbsp;</p>
<p><span>GetWindowDC</span></p>
<p>The GetWindowDC function retrieves the device context (DC) for the entire window, including title bar, menus, and scroll bars. A window device context permits painting anywhere in a window, because the origin of the device context is the upper-left corner of the window instead of the client area. </p>
<p>GetWindowDC assigns default attributes to the window device context each time it retrieves the device context. Previous attributes are lost. </p>
<p>HDC GetWindowDC(</p>
<p><span>&nbsp;&nbsp;&nbsp; </span>HWND hWnd <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>// handle of window&nbsp;</p>
<p><span>&nbsp;&nbsp; </span>);<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></p>
<p>Parameters</p>
<p>hWnd</p>
<p>Identifies the window with a device context that is to be retrieved.</p>
<img src ="http://www.cppblog.com/jacky2019/aggbug/22467.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/jacky2019/" target="_blank">小熊</a> 2007-04-21 12:54 <a href="http://www.cppblog.com/jacky2019/archive/2007/04/21/22467.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>求n!的结果中末尾0的个数</title><link>http://www.cppblog.com/jacky2019/archive/2007/04/19/22306.html</link><dc:creator>小熊</dc:creator><author>小熊</author><pubDate>Thu, 19 Apr 2007 07:30:00 GMT</pubDate><guid>http://www.cppblog.com/jacky2019/archive/2007/04/19/22306.html</guid><wfw:comment>http://www.cppblog.com/jacky2019/comments/22306.html</wfw:comment><comments>http://www.cppblog.com/jacky2019/archive/2007/04/19/22306.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/jacky2019/comments/commentRss/22306.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/jacky2019/services/trackbacks/22306.html</trackback:ping><description><![CDATA[<p><span>终于悟到了。</span></p>
<p><span>首先，很容易想到的就是，求</span>0<span>的个数，就是求</span>5<span>的个数（如果这个你都想不明白的话，那就。。。再好好想想吧，呵呵）；</span></p>
<p><span>接下来，如何求</span>5<span>的个数呢？如果遍历一遍的话，那显然是太慢了！因为这种计算题太有规律了！想了好久，终于想出来了：</span></p>
<p>Result = 0; // <span>最后的结果</span></p>
<p>while ( N &gt;= 5 )</p>
<p>{</p>
<p>&nbsp;N /= 5;</p>
<p>&nbsp;Result += N;</p>
<p>}</p>
<p>// <span>结束了。</span></p>
<p>&nbsp;</p>
<p><span>没错，就是这么简单！下面简单说说为什么这样子做是对的（偶小试了一下，没问题，呵呵）：</span></p>
<p><span>第一次除以</span>5<span>表示</span>5<span>的倍数的个数，</span></p>
<p><span>第二次除以</span>5<span>表示</span>5<span>的平方的倍数的个数，（显然，</span>5<span>的平方暗含了两个</span>0<span>）</span></p>
<p><span>。。。依此类推</span></p>
<p><span>最后当</span>N&lt;5<span>了，结束。</span></p>
<p>&nbsp;</p>
<p><span>小小的验证一下：</span></p>
<p>26<span>！</span></p>
<p>26/5 = 5<span>，</span> 5/5 = 1<span>，那么最后</span>0<span>的个数就是</span>6<span>了。用</span>Google<span>算了一下，结果</span>G<span>大叔直接用有效数字表示了，</span>@$%$%@$%<span>。。。不过应该是没错了。恩。</span></p>
<p>Sigh<span>，知道结果后才知道原来这么简单的阿，偶土了。</span></p>
<img src ="http://www.cppblog.com/jacky2019/aggbug/22306.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/jacky2019/" target="_blank">小熊</a> 2007-04-19 15:30 <a href="http://www.cppblog.com/jacky2019/archive/2007/04/19/22306.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>48 Ways to Wisdom - Way 4, Introduce Yourself to Yourself</title><link>http://www.cppblog.com/jacky2019/archive/2007/04/18/22222.html</link><dc:creator>小熊</dc:creator><author>小熊</author><pubDate>Wed, 18 Apr 2007 08:22:00 GMT</pubDate><guid>http://www.cppblog.com/jacky2019/archive/2007/04/18/22222.html</guid><wfw:comment>http://www.cppblog.com/jacky2019/comments/22222.html</wfw:comment><comments>http://www.cppblog.com/jacky2019/archive/2007/04/18/22222.html#Feedback</comments><slash:comments>4</slash:comments><wfw:commentRss>http://www.cppblog.com/jacky2019/comments/commentRss/22222.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/jacky2019/services/trackbacks/22222.html</trackback:ping><description><![CDATA[<p><a name=ETFTOP><span>Did you ever get on a train going somewhere, only to find that you're headed in the wrong direction?</span></a></p>
<p><span><span>The same thing happens in life. We set goals and make plans - and sometimes discover that we're on "the wrong train."</span></span></p>
<p><span><em><span>Bi-vinat ha-lave</span></em></span><span><span> literally means "understanding the heart." The heart is the seat of emotions. We say: "My heart is heavy, my heart is lifted, my heart is broken," etc. To understand your heart is to understand your true inner self.</span></span></p>
<p><span><span>Many people go through life making assumptions about who they are. They never take time to "meet" themselves. Don't be afraid of discovering that the "real you" may be different than the "current you."</span></span></p>
<p><span><span>Often a crisis hits at midlife when people ask: "What's my life about? Is this all worth it?" We've heard stories of people who suddenly change direction, quitting their job and getting divorced. You know, like the successful doctor who decides he never wanted to go into medicine in the first place -- so he drops it and becomes an artist.</span></span></p>
<p><span><span>Knowing yourself is the essence of being alive. If you don't know yourself, you are not living. If you don't know what makes you tick, you're a robot, a puppet, a zombie.</span></span></p>
<p><span><span>So don't wait for a crisis. </span></span><span><strong><span>Life is too short to take wrong trains.</span></strong></span><span></span></p>
<p><strong><span>GETTING STARTED</span></strong></p>
<p><span>Think of someone you'd be fascinated to meet, someone you'd really like to find out what makes him tick.</span></p>
<p><span>Now realize the most fascinating person you could ever meet is ... yourself.</span></p>
<p><span>Sit down, say hello, and introduce yourself to yourself. Become familiar with yourself as if you'd just met a long-lost cousin. Interview yourself. Ask questions about your life and the direction you're going. Search out your dreams -- both the ones you're fulfilling and the ones you've pushed to the back of your mind.</span></p>
<p><span>Get down to basics. You want to be rich. You want to be famous. You want to be good. You want to accomplish. You want meaning. You want to be creative. But why do you want all this? What's driving you? What you really want out of life?</span></p>
<p><span>The process of self-discovery involves asking a series of questions, always probing deeper until the underlying truth emerges. Ask yourself 10 questions that you would ask an intimate friend. Then wait for answers. Don't worry, no one is going to poke fun at you.</span></p>
<ol type=1>
    <li><span>What is the <strong>purpose</strong> of life?</span></li>
    <li><span>What is my <strong>goal</strong> in life?</span></li>
    <li><span>Why did I choose this <strong>career</strong>?</span></li>
    <li><span>How do I spend my <strong>spare</strong> <strong>time</strong>?</span></li>
    <li><span>What is my <strong>motivation</strong> for doing what I do?</span></li>
    <li><span>What really makes me <strong>happy</strong>?</span></li>
    <li><strong><span>Am I as happy as I want to be</span></strong><span>?</span></li>
    <li><span>Is it more important to be <strong>rich</strong> or to be <strong>happy</strong>?</span></li>
    <li><span>What are my future <strong>plans</strong>? Why?</span></li>
    <li><span>What are my secret <strong>dreams</strong> and <strong>ambitions</strong>?</span></li>
</ol>
<p><span>Don't be surprised if the answers aren't immediate. This process can take many months. Stick with it and find out what makes you tick. The answers are hiding in there. After all, you have a fascinating partner.</span></p>
<p><span>Finally, the most important question to ask is:</span></p>
<p><strong><span>"What am I living for?"</span></strong></p>
<p><span>It sounds like a simple question, but many are embarrassed to ask it. A voice inside us says, "Nah, why ask such a basic question?" We're resistant because we know this requires a lot of difficult soul-searching. And when you thoroughly know yourself, then you have changed. You've changed your relationship with yourself and the world.</span></p>
<p><strong><span>CONFIDENCE IN DECISION-MAKING</span></strong></p>
<p><span>People often avoid making decisions out of fear of making a mistake.</span></p>
<p><span>Actually, the failure to make decisions is one of life's biggest mistakes.</span></p>
<p><span>Imagine the beggar who receives a letter saying that he's inherited a million dollars. If he doesn't read the letter, is he rich ... or not?</span></p>
<p><span>Similarly, God gave us the free will to make choices in life and achieve greatness. But if we're not aware of our free will, then we don't really have it. And then we wind up blaming others when things go wrong -- even though we know the decision is really up to us.</span></p>
<p><span>If you're not using your potential, it wears away at your confidence. Do you know what your potential is? Have you tried to use it? You have to tackle life. You haven't given up yet, have you? Let's get on with the game, with the business of really living, of not just "going through the motions."</span></p>
<p><span>Know the difference between "making decisions" and just floating, falling into place. Did you choose to go to college? Or perhaps you had nothing to do with the decision. Was it something you just did because you graduated high school and everybody else was doing it? Did you think it through and actually make a decision?</span></p>
<p><span>Imagine this private conversation of a college student:</span></p>
<p><em><span>Why am I going to college?<br>To get a degree.<br>Why?<br>Because I want to get into a good graduate school.<br>Why?<br>So I'll get a good job.<br>Why?<br>So I can pay back my college loans!</span></em></p>
<p><span>Through the process of questioning, he reveals a logical fault in his motivation. Really, the primary reason for going to college should be to acquire wisdom, knowledge and information. In other words, to get an education!</span></p>
<p><span>Now try the process yourself, using this example:</span></p>
<p><em><span>Why do I want to get married?</span></em></p>
<p><span>Don't accept pat answers. Keep asking "Why, why why?" Be frank. It's yourself. Ask any question you like. Be patient and persistent. Eventually you'll get an answer.</span></p>
<p><span>When you thoroughly analyze an issue, then you can make wise decisions with confidence.</span></p>
<p><span>Identify where you lack confidence. What makes you nervous? What situations inhibit you from being yourself? Why can't you make decisions? Is it that you don't know how to make decisions? Or that you doubt your decisions after they're made? Or you just don't feel like making decisions?</span></p>
<p><span>Enjoy making decisions. Deal with the world you live in. That's loving the dynamics of life.</span></p>
<p><strong><span>ISOLATE YOUR BLOCKS</span></strong></p>
<p><span>Anytime you find it difficult to achieve a goal, figure out what's holding you back.</span></p>
<p><span>Everyone has problems. Being aware of these problems is the key to getting in touch with yourself. Because as long as you don't face problems, they fester and bug you from behind.</span></p>
<p><span>Write your "blocks" on a piece of paper. That's a good step in the right direction. By isolating specific obstacles, you turn them into concrete challenges that require solutions.</span></p>
<p><span>Ask yourself:</span></p>
<ul type=disc>
    <li><span>Am I lazy? Why?</span></li>
    <li><span>Am I disorganized? Why?</span></li>
    <li><span>Do I get angry? When?</span></li>
    <li><span>Why do ever I get defensive? About what?</span></li>
    <li><span>What makes me jealous?</span></li>
    <li><span>What makes me arrogant?</span></li>
    <li><span>Do I have trouble making decisions? Why?</span></li>
    <li><span>Do I lack self-discipline?</span></li>
    <li><span>Do I lack self-confidence?</span></li>
    <li><span>Why don't I take more initiative?</span></li>
</ul>
<p><span>Negative character traits are the roots of our problems. Make a list of your negative traits, and identify when they affect you the most. Then analyze what triggers these reactions in you. Finally, formulate an effective counter-approach.</span></p>
<p><span>Working through this takes time. But do you have anything better to be doing right now?</span></p>
<p><strong><span>READ YOUR EMOTIONS</span></strong></p>
<p><span>Get in touch with your emotional state. Take a reading of how you feel. Happy? Angry? Tense? Sad? Emotions are a measuring stick for what's going on below the surface. It's like taking your temperature. If you're sick, you need to be aware so you can fix the problem.</span></p>
<p><span>Find out why you're upset. Who or what is pressuring you? Is it an internal or an external problem? Identify it.</span></p>
<p><span>Let's say you are irritated. Why?</span></p>
<p><em><span>Because the boss chewed me out.<br>So why am I irritated?<br>Because I resent him.<br>So what? Why does that bother me?<br>Because I feel I am no good.<br>I'm no good? He's nuts!</span></em></p>
<p><span>Get out of yourself and track it down. If you don't, it's just irritation. And the next thing you know, you'll go home and yell at your kids.</span></p>
<p><span>Once you've identified what causes negative feelings, adjust yourself to minimize the impact. Either avoid these situations, or prepare yourself to handle them when they arise.</span></p>
<p><span>Further, root out negative motivations that corrupt your behavior. Let's say that you give charity. Why? One motivation is to help humanity. Another is the pleasure of being constructive. A third is the desire to do the right thing. These are all positive motivations. A negative motivation for giving charity is: "I want people to admire me." That's corruptive.</span></p>
<p><span>The next time you give charity, do so anonymously. Eliminate the wrong reasons. They are destructive.</span></p>
<p><span>The same goes with the positive emotions. Be aware of how your emotional state affects decisions. For example, don't buy a new stereo when you're in a euphoric mood. Wait. Think it over. You are susceptible.</span></p>
<p><span>Pinpoint what makes you happy. You can have more joy on a daily basis by formulating some practical applications. You got up in the morning, it's a gorgeous day and you feet great. You're energized. Now take that feeling and teach yourself how to get up on the right side -- every day!</span></p>
<p><span>Another example: You did a good job and got the boss's compliment. Now focus: Do you need the boss to tell you did a good job? No! Create your own pleasure out of doing a good job.</span></p>
<p><strong><span>GET IN TOUCH WITH YOUR TWO SIDES</span></strong></p>
<p><span>Everyone has an urge for greatness. We want self respect, power, fame. We want to accomplish, to be strong, to do the right thing, to even save the world.</span></p>
<p><span>Yet at the same time, we have a counter-urge to run away from responsibility, to get into bed and crawl under the covers.</span></p>
<p><span>Someone may say, "Life is beautiful," but he doesn't feel it. His emotions hold him back and he walks around going, "Ugh, life is a burden."</span></p>
<p><span>Recognize the volcano of conflict within you: What you truly "want," versus what you "feel" like. This is the conflict between body and soul.</span></p>
<p><span>Once you appreciate the dichotomy, you can identify at any moment whether your body or soul is talking. This makes it possible to live with sanity and choose the right thing.</span></p>
<p><span>The next step is to make peace between your two sides. The easiest way is to squash your drive to be great. But life is not about taking the easy way out. Just because you feel uncomfortable about an idea doesn't mean it's wrong for you. It's hard to break habits, and growth can be frightening.</span></p>
<p><span>For example, would you rather be happy or rich? Okay, you'd rather be happy. Now imagine this exchange:</span></p>
<p><span>"Come on, I'll teach you how to be happy. All it requires is effort and change."</span></p>
<p><span>"Oh, I'd love to, but I can't right now. It's impossible. I've got a flight to catch."</span></p>
<p><span>"Really? I'll pay you $10,000 a week to work on happiness."</span></p>
<p><span>"Sure! Where do I sign up?"</span></p>
<p><span>"Oh, but I thought you can't right now..."</span></p>
<p><span>We conceal our problems with rationalization: "I'll wreck my mind thinking about what life is about! Nobody really knows what life is about. It's not going to work. Nothing can be done about it anyway. I don't really care. It's not worth the time!"</span></p>
<p><span>The Sages say that a person only makes a mistake when overcome by a moment of insanity. So realize that you are fighting "insanity." It is not logical. You've got to be on guard. Because if you get off track, you'll pay for it down the road.</span></p>
<p><span>So ... do you want to change? What have you got against it? Feel the antipathy of the body. We are so darn lazy. The body just wants to sleep. "Aaaah ... I don't want to change. I'm happy enough. I'm comfortable in my niche of misery." Are you rich enough? No! So are you happy enough?</span></p>
<p><span>You see the importance of tracking that down? You have to identify the animal you are fighting. "The dread of change."</span></p>
<p><span>If you're alert, you see the enemy. You can fight it. You may lose a struggle with the body, but at least you have your confidence. "I know what I am doing."</span></p>
<p><strong><span>COAX THE BODY</span></strong></p>
<p><span>Get in touch with your spiritual core. Know what is driving you. Don't let free will be a subconscious thing. You want greatness. But the body says that's too much effort.</span></p>
<p><span>To try to convince the body, try to identify the tangible benefit. "Why is it necessary? What will it do for me?" You have to bring it home to emotional realization. "What do I lose?" What do I gain?" Only then will the idea have power. And you'll get out there and do it.</span></p>
<p><span>Here's the secret formula: Identify with your intellect, and coax your heart along. For example, if you're emotionally convinced of the benefit of getting into shape, then even when you break out in a cold sweat and your heart is doing palpitations, you will keep going. Because you have decided, "I want this," you know it is important.</span></p>
<p><span>To avoid negative backlash, your emotions have to feel comfortable with the changes you make. Learn to relax and reassure the body. Cajole the body and say, "It won't be so bad. Remember the last time you made an effort, how great you felt!" Be encouraging and reward yourself for success.</span></p>
<p><span>Don't say it doesn't work. You haven't made the effort. Don't give up on your intuition and perception. Just realize you haven't yet brought it home to actualization.</span></p>
<p><span>Consider how the basic human drives affect you: security, self-respect, honor, passions, social pressure, and possessions. Pay particularly close attention to how you accept responsibility. Let's say that you made a mistake. You want to apologize in a full and forthright manner. Yet you feel like forgetting the whole thing, hiding, running away and saying "it's not my fault."</span></p>
<p><span>This is the volcano. We want to be tough, dedicated and powerful -- yet we feel like being marshmallows. Choosing the path of the soul doesn't come naturally. It takes a lot of time and hard work.</span></p>
<p><strong><span>KNOW WHAT YOU KNOW</span></strong></p>
<p><span>Don't think that just because you understand something, you are living with it. It is possible to believe one way, and yet act another. It happens to us all the time. You can believe it's important to eat healthy food, yet gorge yourself on French fries and chocolate cake.</span></p>
<p><span>Our actions are determined by our level of clarity. If we understand an idea on just a superficial level, then we'll have difficulty sticking to it when the going gets tough.</span></p>
<p><span>Next time you go to a funeral, watch carefully. When they remove the body from the chapel, the mourners start to cry. Are they crying because they want to body to stay there?! No. All of a sudden there is a realization of death, that he won't be coming back. At the cemetery, they lower the casket into the ground and the mourners cry again. It's the emotional realization that death is final now.</span></p>
<p><span>Until you align your feelings with reality, you are in dreamland. Growth begins in the mind, but your heart has to buy into everything your mind discovers. Only then will you integrate these ideas into day-to-day life.</span></p>
<p><span>A lot of people believe in God. There are very few people who live with God. Does that make sense? You have to assimilate something that you've accepted as true. It has to become part of you.</span></p>
<p><strong><span>FIVE-FINGER CLARITY</span></strong></p>
<p><span>You've got to know yourself cold, just like you know your hand has five fingers. How do you know you are on the right path? How do you know you're not making a mistake right now?</span></p>
<p><span>To develop this clarity, articulate the important principles that guide your life. For example, in Judaism we say that love is an obligation. Is this reasonable? Work the issue through with yourself:</span></p>
<p><em><span>"Ridiculous. You can't obligate me to love."<br>"But if I have children, will I love them?"<br>"Of course I'm going to love my kids!"<br>"How do I know? I don't know what kind of kids I'm going to have. Maybe they'll be brats and I won't love them."<br>"I will. I'm obligated to love my children."</span></em></p>
<p><span>Do you see the contradiction? On an intuitive level, you know that love is an obligation. But the concept is not so clear that you can articulate it.</span></p>
<p><span>Take your time. Sort out the basic aspects of living. Ask yourself important questions about life's global and spiritual issues.</span></p>
<p><span>--?What is the meaning of existence?<br>--?What's good about living?<br>--?How do I feel about humanity?<br>--?What is the afterlife?<br>--?How do I understand good versus evil?<br>--?Do I have free will? How do I activate it?<br>--?What makes me sad? Is it okay to be sad?<br>--?How do I feel about God?<br>--?Am I proud to be a Jew?<br>--?How do I understand the Holocaust?</span></p>
<p><span>Some of these topics may be unpleasant to think about. If so, why is it unpleasant? Track it down.</span></p>
<p><span>Don't just use slogans to parrot things that you heard. Know why you are doing what you are doing. Otherwise, it's just society talking. You may have adopted part of society without analyzing its validity. Check it out.</span></p>
<p><span>Work through all the issues until you have "five-finger clarity." A human being who knows what he wants will get there. By hook or by crook. It's like a homing mechanism on a missile. If you program it right, you will get there.</span></p>
<p><strong><span>WHY IS "KNOWING YOURSELF" AN INGREDIENT IN WISDOM?</span></strong></p>
<ul type=disc>
    <li><span>You can know truth if you look honestly into yourself. </span></li>
    <li><span>Emotions are powerful forces of greatness. Know them. Harness them. </span></li>
    <li><span>Identify your problems. It's the beginning of solving them. </span></li>
    <li><span>If you don't get it straight now, you're bound to make some bad mistakes. </span></li>
    <li><span>Don't be afraid of finding out who you really are. </span></li>
    <li><span>Use your free will as a conscious tool for better living. </span></li>
    <li><span>If you're angry or upset, track it down. What's the root? </span></li>
    <li><span>If you're acting illogically, at least acknowledge that to yourself! </span></li>
    <li><span>The key to sanity is letting truth into the body. </span></li>
    <li><span>You can't afford to wait too long to get to know yourself. Because you are the most fascinating person you'll ever meet.</span></li>
</ul>
<p>&nbsp;</p>
<img src ="http://www.cppblog.com/jacky2019/aggbug/22222.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/jacky2019/" target="_blank">小熊</a> 2007-04-18 16:22 <a href="http://www.cppblog.com/jacky2019/archive/2007/04/18/22222.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>动态语言</title><link>http://www.cppblog.com/jacky2019/archive/2007/04/16/22034.html</link><dc:creator>小熊</dc:creator><author>小熊</author><pubDate>Mon, 16 Apr 2007 10:15:00 GMT</pubDate><guid>http://www.cppblog.com/jacky2019/archive/2007/04/16/22034.html</guid><wfw:comment>http://www.cppblog.com/jacky2019/comments/22034.html</wfw:comment><comments>http://www.cppblog.com/jacky2019/archive/2007/04/16/22034.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/jacky2019/comments/commentRss/22034.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/jacky2019/services/trackbacks/22034.html</trackback:ping><description><![CDATA[<p><span>所谓动态语言，也叫脚本语言，就是说<strong>一种在执行期间才去发现数据类型的程序设计语言，主要创建一些需要</strong></span><strong><span>经常更新的动态系统。</span></strong><span>常见的有</span><span>Python, Lua, Perl, PHP<span>等。</span><strong></strong></span></p>
<p><span>以往，我们所使用的语言，比如</span><span>C<span>，</span>C++<span>等等，都称为静态语言。什么是静态语言呢？就是说，在使用数据之前，我们必须首先定义数据类型，这些数据类型包括</span>int, float, double<span>等等。就相当于在使用它们之前，首先要为它们分配好内存空间；而动态语言就刚刚是相反的，它是在得到数据类型之后，再为它分配内存空间。</span></span></p>
<p><span>&#8220;脚本语言除了接近口语化的设计外，另加上简化后的语法。（除了内建的命令外，通常仅需简单的逻辑判断与数值计算即可胜任）因此用脚本语言制作游戏，不再是非程序员不可的工作（除了系统本身的修订），企画人员也可以很快地进入状态。另外，如果将来需要将游戏移植到其他平台时，比起程序与资料的盘根错节的设计，利用脚本语言来开发的游戏，只需要修改系统本身，脚本语言部分本身毋须更动，相形之下出现问题的机会与范围就缩小了很多。</span></p>
<img src ="http://www.cppblog.com/jacky2019/aggbug/22034.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/jacky2019/" target="_blank">小熊</a> 2007-04-16 18:15 <a href="http://www.cppblog.com/jacky2019/archive/2007/04/16/22034.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>名言</title><link>http://www.cppblog.com/jacky2019/archive/2007/04/13/21808.html</link><dc:creator>小熊</dc:creator><author>小熊</author><pubDate>Fri, 13 Apr 2007 08:42:00 GMT</pubDate><guid>http://www.cppblog.com/jacky2019/archive/2007/04/13/21808.html</guid><wfw:comment>http://www.cppblog.com/jacky2019/comments/21808.html</wfw:comment><comments>http://www.cppblog.com/jacky2019/archive/2007/04/13/21808.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/jacky2019/comments/commentRss/21808.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/jacky2019/services/trackbacks/21808.html</trackback:ping><description><![CDATA[C++名人Alexandrescu说，十几岁的少年天才到处都有，三十多岁的优秀设计师凤毛麟角，掌握一种力量是容易的，学会恰当地使用这种力量却难得多，这就是聪明与智慧之间的差别。文武之道，有张有弛，知道什么是该做的，什么是不该做的，这是人生最大的学问。
<img src ="http://www.cppblog.com/jacky2019/aggbug/21808.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/jacky2