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

📄 ch02_17.htm

📁 By Tom Christiansen and Nathan Torkington ISBN 1-56592-243-3 First Edition, published August 1998
💻 HTM
字号:
<HTML><HEAD><TITLE>Recipe 2.16. Converting Between Octal and Hexadecimal (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen &amp; Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly &amp; Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:30:22Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-243-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch02_01.htm"TITLE="2. Numbers"><LINKREL="prev"HREF="ch02_16.htm"TITLE="2.15. Using Complex Numbers"><LINKREL="next"HREF="ch02_18.htm"TITLE="2.17. Putting Commas in Numbers"></HEAD><BODYBGCOLOR="#FFFFFF"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Perl Cookbook"><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><p><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch02_16.htm"TITLE="2.15. Using Complex Numbers"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 2.15. Using Complex Numbers"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch02_01.htm"TITLE="2. Numbers"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch02_18.htm"TITLE="2.17. Putting Commas in Numbers"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 2.17. Putting Commas in Numbers"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch02-14189">2.16. Converting Between Octal and Hexadecimal</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch02-pgfId-1235">Problem</A></H3><PCLASS="para"><ACLASS="indexterm"NAME="ch02-idx-1000008104-0"></A><ACLASS="indexterm"NAME="ch02-idx-1000008104-1"></A><ACLASS="indexterm"NAME="ch02-idx-1000008104-2"></A><ACLASS="indexterm"NAME="ch02-idx-1000008104-3"></A><ACLASS="indexterm"NAME="ch02-idx-1000008104-4"></A>You want to convert a string (e.g., &quot;<CODECLASS="literal">0x55</CODE>&quot; or &quot;<CODECLASS="literal">0755</CODE>&quot;) containing an octal or hexadecimal number to the correct number.</P><PCLASS="para">Perl only understands octal and hexadecimal numbers when they occur as literals in your programs. If they are obtained by reading from files or supplied as command-line arguments, no automatic conversion takes place.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch02-pgfId-1243">Solution</A></H3><PCLASS="para">Use Perl's <CODECLASS="literal">oct</CODE> and <CODECLASS="literal">hex</CODE> functions: <ACLASS="indexterm"NAME="ch02-idx-1000008105-0"></A><ACLASS="indexterm"NAME="ch02-idx-1000008105-1"></A></P><PRECLASS="programlisting">$number = hex($hexadecimal);         # hexadecimal$number = oct($octal);               # octal</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch02-pgfId-1253">Discussion</A></H3><PCLASS="para">The <CODECLASS="literal">oct</CODE> function converts octal numbers with or without the leading &quot;<CODECLASS="literal">0</CODE>&quot;: &quot;<CODECLASS="literal">0350</CODE>&quot; or &quot;<CODECLASS="literal">350</CODE>&quot;. In fact, it even converts hexadecimal (&quot;<CODECLASS="literal">0x350</CODE>&quot;) numbers if they have a leading &quot;<CODECLASS="literal">0x</CODE>&quot;. The <CODECLASS="literal">hex</CODE> function only converts hexadecimal numbers, with or without a leading &quot;<CODECLASS="literal">0x</CODE>&quot;: &quot;<CODECLASS="literal">0x255</CODE>&quot;, &quot;<CODECLASS="literal">3A</CODE>&quot;, &quot;<CODECLASS="literal">ff</CODE>&quot;, or &quot;<CODECLASS="literal">deadbeef</CODE>&quot;. (Letters may be in upper- or lowercase.)</P><PCLASS="para">Here's an example that accepts a number in either decimal, octal, or hex, and prints that number in all three bases. It uses the <CODECLASS="literal">oct</CODE> function to convert from octal and hexadecimal if the input began with a 0. It then uses <CODECLASS="literal">printf</CODE> to convert back into hex, octal, and decimal as needed.</P><PRECLASS="programlisting">print &quot;Gimme a number in decimal, octal, or hex: &quot;;$num = &lt;STDIN&gt;;chomp $num;exit unless defined $num;$num = oct($num) if $num =~ /^0/; # does both oct and hexprintf &quot;%d %x %o\n&quot;, $num, $num, $num;</PRE><PCLASS="para">The following code converts Unix file permissions. They're always given in octal, so we use <CODECLASS="literal">oct</CODE> instead of <CODECLASS="literal">hex</CODE>.</P><PRECLASS="programlisting">print &quot;Enter file permission in octal: &quot;;$permissions = &lt;STDIN&gt;;die &quot;Exiting ...\n&quot; unless defined $permissions;chomp $permissions;$permissions = oct($permissions);   # permissions always octalprint &quot;The decimal value is $permissions\n&quot;;</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch02-pgfId-1287">See Also</A></H3><PCLASS="para">The "Scalar Value Constructors" section in <EMCLASS="emphasis">perldata</EM> (1) and the <ACLASS="olink"HREF="../prog/ch02_03.htm#PERL2-CH-2-SECT-3.2.1">"Numeric Literals"</A> section of <ACLASS="olink"HREF="../prog/ch02_01.htm">Chapter 2</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A>; the <CODECLASS="literal">oct</CODE> and <CODECLASS="literal">hex</CODE> functions in <EMCLASS="emphasis">perlfunc </EM>(1) and <ACLASS="olink"HREF="../prog/ch03_01.htm">Chapter 3</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A>.</P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch02_16.htm"TITLE="2.15. Using Complex Numbers"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 2.15. Using Complex Numbers"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="book"HREF="index.htm"TITLE="Perl Cookbook"><IMGSRC="../gifs/txthome.gif"ALT="Perl Cookbook"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch02_18.htm"TITLE="2.17. Putting Commas in Numbers"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 2.17. Putting Commas in Numbers"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">2.15. Using Complex Numbers</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="index"HREF="index/index.htm"TITLE="Book Index"><IMGSRC="../gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228">2.17. Putting Commas in Numbers</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><FONTSIZE="-1"></DIV<!-- LIBRARY NAV BAR --> <img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p> <a href="copyrght.htm">Copyright &copy; 2002</a> O'Reilly &amp; Associates. All rights reserved.</font> </p> <map name="library-map"> <area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map> </BODY></HTML>

⌨️ 快捷键说明

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