⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch25_02.htm

📁 编程珍珠,里面很多好用的代码,大家可以参考学习呵呵,
💻 HTM
字号:
<html><head><title>Endianness and Number Width (Programming Perl)</title><!-- STYLESHEET --><link rel="stylesheet" type="text/css" href="../style/style1.css"><!-- METADATA --><!--Dublin Core Metadata--><meta name="DC.Creator" content=""><meta name="DC.Date" content=""><meta name="DC.Format" content="text/xml" scheme="MIME"><meta name="DC.Generator" content="XSLT stylesheet, xt by James Clark"><meta name="DC.Identifier" content=""><meta name="DC.Language" content="en-US"><meta name="DC.Publisher" content="O'Reilly &amp; Associates, Inc."><meta name="DC.Source" content="" scheme="ISBN"><meta name="DC.Subject.Keyword" content=""><meta name="DC.Title" content="Endianness and Number Width"><meta name="DC.Type" content="Text.Monograph"></head><body><!-- START OF BODY --><!-- TOP BANNER --><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home"><map name="banner-map"><AREA SHAPE="RECT" COORDS="0,0,466,71" HREF="index.htm" ALT="Programming Perl"><AREA SHAPE="RECT" COORDS="467,0,514,18" HREF="jobjects/fsearch.htm" ALT="Search this book"></map><!-- TOP NAV BAR --><div class="navbar"><table width="515" border="0"><tr><td align="left" valign="top" width="172"><a href="ch25_01.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="ch25_01.htm">Chapter 25: Portable Perl</a></td><td align="right" valign="top" width="172"><a href="ch25_03.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr></table></div><hr width="515" align="left"><!-- SECTION BODY --><h2 class="sect1">25.2. Endianness and Number Width</h2><a name="INDEX-4322"></a><a name="INDEX-4323"></a><a name="INDEX-4324"></a><a name="INDEX-4325"></a><a name="INDEX-4326"></a><a name="INDEX-4327"></a><a name="INDEX-4328"></a><a name="INDEX-4329"></a><a name="INDEX-4330"></a><a name="INDEX-4331"></a><a name="INDEX-4332"></a><p>Computers store integers and floating-point numbers in differentorders (<em class="emphasis">big-endian</em> or <em class="emphasis">little-endian</em>) and different widths (32-bit and64-bit being the most common today).  Normally, you won't have tothink about this.  But if your program sends binary data across anetwork connection, or onto disk to be read by a different computer,you may need to take precautions.</p><p>Conflicting orders can make an utter mess out of numbers.  If alittle-endian host (such as an Intel CPU) stores 0x12345678 (305,419,896in decimal), a big-endian host (such as a Motorola CPU) will read itas 0x78563412 (2,018,915,346 in decimal).  To avoid this problem innetwork (socket) connections, use the <tt class="literal">pack</tt> and <tt class="literal">unpack</tt> formats<tt class="literal">n</tt> and <tt class="literal">N</tt>, which write unsigned short and long numbers inbig-endian order (also called "network" order) regardless of theplatform.</p><p>You can explore the endianness of your platform by unpacking adata structure packed in native format such as:<blockquote><pre class="programlisting">print unpack("h*", pack("s2", 1, 2)), "\n";# '10002000' on e.g. Intel x86 or Alpha 21064 in little-endian mode# '00100020' on e.g. Motorola 68040</pre></blockquote>To determine your endianness, you could use eitherof these statements:<blockquote><pre class="programlisting">$is_big_endian    = unpack("h*", pack("s", 1)) =~ /01/;$is_little_endian = unpack("h*", pack("s", 1)) =~ /^1/;</pre></blockquote>Even if two systems have the same endianness, there can still beproblems when transferring data between 32-bit and 64-bit platforms.There is no good solution other than to avoid transferring or storingraw binary numbers.  Either transfer and store numbers as text insteadof binary, or use modules like <tt class="literal">Data::Dumper</tt> or <tt class="literal">Storable</tt> to dothis for you. You really want to be using text-oriented protocols in any event--they're more robust, more maintainable, and more extensible than binary protocols.</p><p>Of course, with the advent of XML and Unicode, our definition of textis getting more flexible. For instance, between two systems runningPerl 5.6.0 (or newer), you can transport a sequence of integersencoded as characters in <tt class="literal">utf8</tt> (Perl's version ofUTF-8).  If both ends are running on an architecture with 64-bitintegers, you can exchange 64-bit integers.  Otherwise, you're limitedto 32-bit integers.  Use <tt class="literal">pack</tt> with a<tt class="literal">U*</tt> template to send, and <tt class="literal">unpack</tt>with a <tt class="literal">U*</tt> template to receive.</p><!-- BOTTOM NAV BAR --><hr width="515" align="left"><div class="navbar"><table width="515" border="0"><tr><td align="left" valign="top" width="172"><a href="ch25_01.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0"></a></td><td align="right" valign="top" width="172"><a href="ch25_03.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">25.1. Newlines</td><td align="center" valign="top" width="171"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0"></a></td><td align="right" valign="top" width="172">25.3. Files and Filesystems</td></tr></table></div><hr width="515" align="left"><!-- LIBRARY NAV BAR --><img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p><font size="-1"><a href="copyrght.htm">Copyright &copy; 2001</a> O'Reilly &amp; Associates. All rights reserved.</font></p><map name="library-map"> <area shape="rect" coords="2,-1,79,99" href="../index.htm"><area shape="rect" coords="84,1,157,108" href="../perlnut/index.htm"><area shape="rect" coords="162,2,248,125" href="../prog/index.htm"><area shape="rect" coords="253,2,326,130" href="../advprog/index.htm"><area shape="rect" coords="332,1,407,112" href="../cookbook/index.htm"><area shape="rect" coords="414,2,523,103" href="../sysadmin/index.htm"></map><!-- END OF BODY --></body></html>

⌨️ 快捷键说明

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