??xml version="1.0" encoding="utf-8" standalone="yes"?>久久久久五月天,亚洲精品九九,亚洲欧美在线专区http://www.aygfsteel.com/lukewange-hit1983/category/34833.htmlpresentzh-cnFri, 14 Nov 2008 10:50:51 GMTFri, 14 Nov 2008 10:50:51 GMT60位运?/title><link>http://www.aygfsteel.com/lukewange-hit1983/archive/2008/11/12/240082.html</link><dc:creator>LukeW</dc:creator><author>LukeW</author><pubDate>Wed, 12 Nov 2008 05:53:00 GMT</pubDate><guid>http://www.aygfsteel.com/lukewange-hit1983/archive/2008/11/12/240082.html</guid><wfw:comment>http://www.aygfsteel.com/lukewange-hit1983/comments/240082.html</wfw:comment><comments>http://www.aygfsteel.com/lukewange-hit1983/archive/2008/11/12/240082.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/lukewange-hit1983/comments/commentRss/240082.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/lukewange-hit1983/services/trackbacks/240082.html</trackback:ping><description><![CDATA[<strong>C中的位运?/strong><br /> 能够q用CQ何整形的数据cd?包括char, int), 无论有没有short, long, unsignedq样的限定词.<br /> <br /> <br /> 位运的应用<br /> <div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> 交换指针变量x,y所指向的存储位|处存放的?br /> </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> 优势是不需要第三个位置来时存储另一个?br /> </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> 但是q个Ҏq没有明昄性能优势,只是一个智力上的消?/span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> inplace_swap(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">x, </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">y)<br /> {<br />  </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">x </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">x </span><span style="color: rgb(0, 0, 0);">^</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">y;<br />  </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">x </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">x </span><span style="color: rgb(0, 0, 0);">^</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">y;<br />  </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">x </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">x </span><span style="color: rgb(0, 0, 0);">^</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">y;<br /> }<br /> </span></div> <br /> <strong>位运常见用?</strong><br /> 实现掩码q算<br /> <br /> <br /> <br /> -----------------------------------<br /> <strong>Java中的位运?/strong><br /> <br /> <br /> <img src ="http://www.aygfsteel.com/lukewange-hit1983/aggbug/240082.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/lukewange-hit1983/" target="_blank">LukeW</a> 2008-11-12 13:53 <a href="http://www.aygfsteel.com/lukewange-hit1983/archive/2008/11/12/240082.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>大端端 -- 各系l及机器的信息表C?/title><link>http://www.aygfsteel.com/lukewange-hit1983/archive/2008/11/12/240053.html</link><dc:creator>LukeW</dc:creator><author>LukeW</author><pubDate>Wed, 12 Nov 2008 03:58:00 GMT</pubDate><guid>http://www.aygfsteel.com/lukewange-hit1983/archive/2008/11/12/240053.html</guid><wfw:comment>http://www.aygfsteel.com/lukewange-hit1983/comments/240053.html</wfw:comment><comments>http://www.aygfsteel.com/lukewange-hit1983/archive/2008/11/12/240053.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/lukewange-hit1983/comments/commentRss/240053.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/lukewange-hit1983/services/trackbacks/240053.html</trackback:ping><description><![CDATA[因ؓ现行的计机都是以八位一个字节ؓ存储单位,那么一?6位的整数,也就是C语言中的short,在内存中可能有两U存储顺序big- <br /> <br /> endian和litte-endian.考虑一个short整数0x3132(0x32是低?0x31是高?,把它赋值给一个short变量,那么它在内存中的存储?br /> <br /> 能有如下两种情况:<br /> 大端字节(Big-endian):<br /> <br /> short变量地址<br />        0x1000                  0x1001<br /> ___________________________________<br /> |                 |<br /> |         0x31    |       0x32<br /> |________________ | ________________<br /> 高位字节在低位字节的前面,也就是高位在内存地址低的一?可以q样C(大端->高位->在前->正常的逻辑序)<br />  <br /> 端字节(little-endian):<br /> <br /> short变量地址<br />        0x1000                  0x1001<br /> _____________________________________<br /> |                 |<br /> |         0x32    |       0x31<br /> |________________ | __________________<br /> 低位字节在高位字节的前面,也就是低位在内存地址低的一?可以q样C(端->低位->在前->与正帔R辑序相反)<br />  <br /> 可以做个实验<br /> 在windows上下如下E序<br /> <div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">stdio.h</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br /> #include </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">assert.h</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br />  <br /> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> main( </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> )<br /> {<br />         </span><span style="color: rgb(0, 0, 255);">short</span><span style="color: rgb(0, 0, 0);"> test;<br />         FILE</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);"> fp;<br />         <br />         test </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0x3132</span><span style="color: rgb(0, 0, 0);">;  </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">(31ASIIC码的’1’,32ASIIC码的’2’)</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> ((fp </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> fopen (</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">c:</span><span style="color: rgb(0, 0, 0);">""</span><span style="color: rgb(0, 0, 0);">test.txt</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">wb</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)) </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> NULL)<br />               assert(</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">);<br />         fwrite(</span><span style="color: rgb(0, 0, 0);">&</span><span style="color: rgb(0, 0, 0);">test, </span><span style="color: rgb(0, 0, 255);">sizeof</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">short</span><span style="color: rgb(0, 0, 0);">), </span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">, fp);<br />         fclose(fp);<br /> }</span></div> <br />     然后在C盘下打开test.txt文g,可以看见内容?1,而test{于0x3132,可以明显的看出来x86的字节顺序是低位在前.如果我们<br /> 把这D同L代码攑ֈ(big-endian)的机器上执行,那么打出来的文g是12.q在本机中用是没有问题?但当你把q个文g从一<br /> 个big- endian机器复制C个little-endian机器上时出现问题了.<br /> <br />     如上qC?我们在big-endian的机器上创徏了这个test文g,把其复制到little-endian的机器上再用freadd一?short?br /> ?我们得到的就不再?x3132而是0x3231?q样d的数据就是错误的,所以在两个字节序不一L机器上传输数据时需要特?br /> 心字节序,理解了字节顺序在可以帮助我们写出UL行更高的代码.<br /> <br /> 正因为有字节序的差?所以在|络传输的时候定义了所有字节顺序相关的数据都用big-endian,BSD的代码中定义了四个宏来处<br /> ?<br /> <div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> ntohs(n)     </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">|络字节序C机字节顺?nbsp;n代表net, h代表host, s代表short</span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> htons(n)     </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">L字节序到网l字节顺?nbsp;n代表net, h代表host, s代表short</span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> ntohl(n)      </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">|络字节序C机字节顺?nbsp;n代表net, h代表host, s代表 long</span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> htonl(n)      </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">L字节序到网l字节顺?nbsp;n代表net, h代表host, s代表 long</span></div> <br /> 举例说明下这其中一个宏的实?<br /> <div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> sw16(x) "</span><span style="color: rgb(0, 0, 0);"><br />     ((</span><span style="color: rgb(0, 0, 255);">short</span><span style="color: rgb(0, 0, 0);">)( </span><span style="color: rgb(0, 0, 0);">"<br /> </span><span style="color: rgb(0, 0, 0);">        (((</span><span style="color: rgb(0, 0, 255);">short</span><span style="color: rgb(0, 0, 0);">)(x) </span><span style="color: rgb(0, 0, 0);">&</span><span style="color: rgb(0, 0, 0);"> (</span><span style="color: rgb(0, 0, 255);">short</span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(0, 0, 0);">0x00ffU</span><span style="color: rgb(0, 0, 0);">) </span><span style="color: rgb(0, 0, 0);"><<</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">8</span><span style="color: rgb(0, 0, 0);">) </span><span style="color: rgb(0, 0, 0);">|</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"<br /> </span><span style="color: rgb(0, 0, 0);">        (((</span><span style="color: rgb(0, 0, 255);">short</span><span style="color: rgb(0, 0, 0);">)(x) </span><span style="color: rgb(0, 0, 0);">&</span><span style="color: rgb(0, 0, 0);"> (</span><span style="color: rgb(0, 0, 255);">short</span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(0, 0, 0);">0xff00U</span><span style="color: rgb(0, 0, 0);">) </span><span style="color: rgb(0, 0, 0);">>></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">8</span><span style="color: rgb(0, 0, 0);">) ))</span></div> <br /> q里实现的是一个交换两个字节顺?其他几个宏类?<br /> <br /> 我们改写一下上面的E序<br /> <div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">stdio.h</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br /> #include </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">assert.h</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br /> <br /> </span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> sw16(x) "</span><span style="color: rgb(0, 0, 0);"><br />     ((</span><span style="color: rgb(0, 0, 255);">short</span><span style="color: rgb(0, 0, 0);">)( </span><span style="color: rgb(0, 0, 0);">"<br /> </span><span style="color: rgb(0, 0, 0);">        (((</span><span style="color: rgb(0, 0, 255);">short</span><span style="color: rgb(0, 0, 0);">)(x) </span><span style="color: rgb(0, 0, 0);">&</span><span style="color: rgb(0, 0, 0);"> (</span><span style="color: rgb(0, 0, 255);">short</span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(0, 0, 0);">0x00ffU</span><span style="color: rgb(0, 0, 0);">) </span><span style="color: rgb(0, 0, 0);"><<</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">8</span><span style="color: rgb(0, 0, 0);">) </span><span style="color: rgb(0, 0, 0);">|</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"<br /> </span><span style="color: rgb(0, 0, 0);">        (((</span><span style="color: rgb(0, 0, 255);">short</span><span style="color: rgb(0, 0, 0);">)(x) </span><span style="color: rgb(0, 0, 0);">&</span><span style="color: rgb(0, 0, 0);"> (</span><span style="color: rgb(0, 0, 255);">short</span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(0, 0, 0);">0xff00U</span><span style="color: rgb(0, 0, 0);">) </span><span style="color: rgb(0, 0, 0);">>></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">8</span><span style="color: rgb(0, 0, 0);">) ))<br /> <br /> </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> 因ؓx86下面是低位在?需要交换一下变成网l字节顺?/span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> htons(x) sw16(x)</span><span style="color: rgb(0, 0, 0);"><br />  <br /> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> main( </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> )<br /> {<br />         </span><span style="color: rgb(0, 0, 255);">short</span><span style="color: rgb(0, 0, 0);"> test;<br />         FILE</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);"> fp;<br />         <br />         test </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> htons(</span><span style="color: rgb(0, 0, 0);">0x3132</span><span style="color: rgb(0, 0, 0);">); </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">(31ASIIC码的’1’,32ASIIC码的’2’)</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> ((fp </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> fopen (</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">c:</span><span style="color: rgb(0, 0, 0);">""</span><span style="color: rgb(0, 0, 0);">test.txt</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">wb</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)) </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> NULL)<br />               assert(</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">);<br />         fwrite(</span><span style="color: rgb(0, 0, 0);">&</span><span style="color: rgb(0, 0, 0);">test, </span><span style="color: rgb(0, 0, 255);">sizeof</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">short</span><span style="color: rgb(0, 0, 0);">), </span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">, fp);<br />         fclose(fp);<br /> }</span></div> <br />  <br />     如果在高字节在前的机器上,׃与网l字节顺序一?所以我们什么都不干可以了,只需要把#define htons(x) sw16(x)宏替<br /> <br /> 换ؓ #define htons(x) (x).<br />     一开始我在理解这个问题时,d想ؓ什么其他数据不用交换字节顺?比如说我们write一块buffer到文?最后终于想明白?<br /> <br /> 因ؓ都是unsigned charcd一个字节一个字节的写进?q个序是固定的,不存在字节顺序的问题.<br /> <p><strong><font color="#0000ff">【用函数判断pȝ是Big Endianq是Little Endian?/font></strong><br /> </p> <br /> <div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: rgb(0, 0, 255);">bool</span><span style="color: rgb(0, 0, 0);"> IsBig_Endian()<br /> </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">如果字节序ؓbig-endianQ返回true;<br /> </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">反之?nbsp;  little-endianQ返回false</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);">{<br />     unsigned </span><span style="color: rgb(0, 0, 255);">short</span><span style="color: rgb(0, 0, 0);"> test </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0x1122</span><span style="color: rgb(0, 0, 0);">;<br />     </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">( (unsigned </span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">) </span><span style="color: rgb(0, 0, 0);">&</span><span style="color: rgb(0, 0, 0);">test ) </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0x11</span><span style="color: rgb(0, 0, 0);">)<br />        </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> TRUE;<br /> </span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);"><br />     </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> FALSE;<br /> <br /> }</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">IsBig_Endian()<br /> </span></div> <br /> <strong><font color="#0000ff">【打印程序对象的字节表示?/font></strong><font color="#0000ff"><br /> </font> <div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> 可在不同q_与硬件架构的机器中测试运行这D代?理解大端表示和小端表C的不同.<br /> </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> q段代码使用强制cd转换规避cdpȝ</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);">#incluede </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">stdio.h</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br /> <br /> </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> 假设每个字节都是非负整数</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);">typedef unsigned </span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">byte_pointer;<br /> <br /> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> show_bytes(byte_pointer start, </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> len)<br /> {<br />  </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">; i </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);"> len; i</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">)<br />   printf(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> %.2x</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, start[i]);<br />  printf(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">\n</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /> }<br /> <br /> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> show_int(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> x)<br /> {<br />  show_bytes((byte_pointer) </span><span style="color: rgb(0, 0, 0);">&</span><span style="color: rgb(0, 0, 0);">x, </span><span style="color: rgb(0, 0, 255);">sizeof</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">));<br /> }<br /> <br /> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> show_float(</span><span style="color: rgb(0, 0, 255);">float</span><span style="color: rgb(0, 0, 0);"> x)<br /> {<br />  show_bytes((byte_pointer) </span><span style="color: rgb(0, 0, 0);">&</span><span style="color: rgb(0, 0, 0);">x, </span><span style="color: rgb(0, 0, 255);">sizeof</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">float</span><span style="color: rgb(0, 0, 0);">));<br /> }<br /> <br /> </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> 在用相同编?如ASCII~码)的系l中,字符串字节表C得到的l果一般是相同?所以文本数据比二进制数据具有更强的q_无关?/span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> show_string(</span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">x)<br /> {<br />  show_bytes((byte_pointer) x, strlen(x));<br /> }<br /> <br /> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> show_pointer(</span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">x)<br /> {<br />  show_bytes((byte_pointer) </span><span style="color: rgb(0, 0, 0);">&</span><span style="color: rgb(0, 0, 0);">x, </span><span style="color: rgb(0, 0, 255);">sizeof</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">));<br /> }<br /> <br /> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> test_show_bytes(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> val)<br /> {<br />  </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> ival </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> val;<br />  </span><span style="color: rgb(0, 0, 255);">float</span><span style="color: rgb(0, 0, 0);"> fval </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> (</span><span style="color: rgb(0, 0, 255);">float</span><span style="color: rgb(0, 0, 0);">)ival;<br />  </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">pval </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">&</span><span style="color: rgb(0, 0, 0);">ival;<br />  <br />  show_int(ival); </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> 各个机器因ؓ大端表示和小端表C的不同,从而只是字节顺序不?/span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);"> show_float(fval); </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> 各个机器因ؓ大端表示和小端表C的不同,从而只是字节顺序不?/span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);"> show_pointer(pval); </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> 指针值是与机器相关的(linux,sun使用4字节地址, 而alpha使用八字节地址)</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);">}</span></div> <br /> ---------------------------------------------<br /> 对于如数?2345在int型和float型时的编码表C?br /> <img src ="http://www.aygfsteel.com/lukewange-hit1983/aggbug/240053.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/lukewange-hit1983/" target="_blank">LukeW</a> 2008-11-12 11:58 <a href="http://www.aygfsteel.com/lukewange-hit1983/archive/2008/11/12/240053.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>j2me 联网技术分析ȝhttp://www.aygfsteel.com/lukewange-hit1983/archive/2008/11/04/238629.htmlLukeWLukeWTue, 04 Nov 2008 08:22:00 GMThttp://www.aygfsteel.com/lukewange-hit1983/archive/2008/11/04/238629.htmlhttp://www.aygfsteel.com/lukewange-hit1983/comments/238629.htmlhttp://www.aygfsteel.com/lukewange-hit1983/archive/2008/11/04/238629.html#Feedback0http://www.aygfsteel.com/lukewange-hit1983/comments/commentRss/238629.htmlhttp://www.aygfsteel.com/lukewange-hit1983/services/trackbacks/238629.html基本?

Generic Connections

In the CLDC Generic Connection framework, all connections are created using the open static method from the Connector class. If successful, this method returns an object that implements one of the generic connection interfaces. Figure 1 shows how these interfaces form an is-a hierarchy. The Connection interface is the base interface such that StreamConnectionNotifier is a Connection and InputConnection is a Connection too.

fig1.gif
Figure 1: Connection interface hierarchy
  • The Connection interface is the most basic connection type. It can only be opened and closed.
  • The InputConnection interface represents a device from which data can be read. Its openInputStream method returns an input stream for the connection.
  • The OuputConnection interface represents a device to which data can be written. Its openOutputStream method returns an output stream for the connection.
  • The StreamConnection interface combines the input and output connections.
  • The ContentConnection is a subinterface of StreamConnection. It provides access to some of the basic meta data information provided by HTTP connections.
  • The StreamConnectionNotified waits for a connection to be established. It returns a StreamConnection on which a communication link has ben established.
  • The DatagramConnection represents a datagram endpoint.

The open method of the Connector class has the following syntax, where the String parameter has the format "protocol:address;parameters".

Connector.open(String);

Here are a few examples:

HTTP Connection

Connector.open("http://java.sun.com/developer");

Datagram Connection

Connector.open("datagram://address:port#");

Communicate with a Port

Connector.open("comm:0;baudrate=9600');

Open Files

Connector.open("file:/myFile.txt");


The HttpConnection Interface:

The HTTP protocol is a request-response application protocol in which the parameters of the request must be set before the request is sent. The connection could be in one of the three following states:
  • Setup: No connection yet
  • Connected: Connection has been made, the request has been sent, and some response is expected
  • Closed: Connection is closed

In the setup state the following methods can be invoked:

  • setRequestMethod
  • setRequestProperty

For example, suppose you have this connection:

HttpConnection c = (HttpConnection)
Connector.open(
"http://java.sun.com/developer");

Then, you can set the request method to be of type POST as follows:

c.setRequestMethod(HttpConnection.POST);

And likewise, you can set some of the HTTP properties. For example, you can set the User-Agent as follows:

c.setRequestProperty("User-Agent","Profile/MIDP-1.0 Configuration/CLDC-1.0");

If there is a method that requires data to be sent or received from the server, there is a state transition from Setup to Connected. Examples of methods that cause the transition include:

openInputStream
openOutputStream
openDataInputStream
openDataOutputStream
getLength
getType
getDate
getExpiration

And while the connection is open, some of these methods that may be invoked:

getURL
getProtocol
getHost
getPort


------------------------------------------------------------
要注意的问题:
开发中遇到个很头疼的问? 与服务器通信write()数据时报java.io.IOException: Couldn't write to socket.
但是服务器抓不到M? 一开始怀疑是q徏立连接出的问? 实际上服务器抓不到包也有可能是流在没有close的时候就已经报错?
?
conn.open("url");
out = conn.openDataOutputStream();//此时进行与服务器的三次握手;
                                  //但是如果在out.close()之前出现异常服务器是抓不CQ何包?br /> out.write(byte[] bb);

关于q个的解释应该是的~冲机制.
所以正的写法应该是捕捉到异常之后在catch块中把流close?strong>.
服务器端开发h员一般会说收不到包所以连接有问题,会把责Q推给客户?抓住q个证据在跟服务器端的同事扯皮时处于有利的位置,嘎嘎.
q有是要多做小实验, 注意代码要规范严?

发现的几个问?

1. java.io.IOException: Couldn't write to socket

2. java.io.IOException: Couldn't read from socket

CMNET联网Ҏ:

CMWAP联网Ҏ:

Ud资费늚处理:

一个通用的HTTPq接装:



LukeW 2008-11-04 16:22 发表评论
]]>
关于断点l传的调?/title><link>http://www.aygfsteel.com/lukewange-hit1983/archive/2008/09/24/230950.html</link><dc:creator>LukeW</dc:creator><author>LukeW</author><pubDate>Wed, 24 Sep 2008 09:57:00 GMT</pubDate><guid>http://www.aygfsteel.com/lukewange-hit1983/archive/2008/09/24/230950.html</guid><wfw:comment>http://www.aygfsteel.com/lukewange-hit1983/comments/230950.html</wfw:comment><comments>http://www.aygfsteel.com/lukewange-hit1983/archive/2008/09/24/230950.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/lukewange-hit1983/comments/commentRss/230950.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/lukewange-hit1983/services/trackbacks/230950.html</trackback:ping><description><![CDATA[一些讨?<br /> http://topic.csdn.net/t/20061214/22/5231907.html <img src ="http://www.aygfsteel.com/lukewange-hit1983/aggbug/230950.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/lukewange-hit1983/" target="_blank">LukeW</a> 2008-09-24 17:57 <a href="http://www.aygfsteel.com/lukewange-hit1983/archive/2008/09/24/230950.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>eclipse真机调试J2meE序http://www.aygfsteel.com/lukewange-hit1983/archive/2008/08/15/222266.htmlLukeWLukeWFri, 15 Aug 2008 07:22:00 GMThttp://www.aygfsteel.com/lukewange-hit1983/archive/2008/08/15/222266.htmlhttp://www.aygfsteel.com/lukewange-hit1983/comments/222266.htmlhttp://www.aygfsteel.com/lukewange-hit1983/archive/2008/08/15/222266.html#Feedback0http://www.aygfsteel.com/lukewange-hit1983/comments/commentRss/222266.htmlhttp://www.aygfsteel.com/lukewange-hit1983/services/trackbacks/222266.html
1. 真机调试其实是厂商SDKҎJAVA的调试API层实现的.

2. 要注意JAD文g中字D늚长度不要q长, 否则会报? 无法调试.

3. 相比? netbeansq行真机调试要方便一? 当然eclipse应该也没问题(懒得实验?.).


LukeW 2008-08-15 15:22 发表评论
]]>
վ֩ģ壺 Ϫ| | Ͻ| ͨ| | ʯ| Ԫ| üɽ| | Ͽ| | | γ| ؿ˹| Դ| | | | µ| ƺ| | | ˮ| | | | | | ˱| Ȩ| Ӣɳ| | | Դ| ˮ| ̫| Ϫ| ޶| | | |