TheAlgorithms-C/d0/dd9/hexadecimal__to__octal2_8c.html
2022-09-08 00:17:03 +00:00

299 lines
20 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
<meta name="generator" content="Doxygen 1.9.5"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Algorithms_in_C: conversions/hexadecimal_to_octal2.c File Reference</title>
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<link href="../../navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../resize.js"></script>
<script type="text/javascript" src="../../navtreedata.js"></script>
<script type="text/javascript" src="../../navtree.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
jax: ["input/TeX","output/HTML-CSS"],
});
</script>
<script type="text/javascript" async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML/MathJax.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr id="projectrow">
<td id="projectalign">
<div id="projectname">Algorithms_in_C<span id="projectnumber">&#160;1.0.0</span>
</div>
<div id="projectbrief">Set of algorithms implemented in C.</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.5 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "../../search/",'.html');
/* @license-end */
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function() {
initMenu('../../',true,false,'search.php','Search');
$(document).ready(function() { init_search(); });
});
/* @license-end */
</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
<div id="nav-tree">
<div id="nav-tree-contents">
<div id="nav-sync" class="sync"></div>
</div>
</div>
<div id="splitbar" style="-moz-user-select:none;"
class="ui-resizable-handle">
</div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(document).ready(function(){initNavTree('d0/dd9/hexadecimal__to__octal2_8c.html','../../'); initResizable(); });
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<div id="MSearchResults">
<div class="SRPage">
<div id="SRIndex">
<div id="SRResults"></div>
<div class="SRStatus" id="Loading">Loading...</div>
<div class="SRStatus" id="Searching">Searching...</div>
<div class="SRStatus" id="NoMatches">No Matches</div>
</div>
</div>
</div>
</div>
<div class="header">
<div class="summary">
<a href="#define-members">Macros</a> &#124;
<a href="#func-members">Functions</a> </div>
<div class="headertitle"><div class="title">hexadecimal_to_octal2.c File Reference</div></div>
</div><!--header-->
<div class="contents">
<p>Convert hexadecimal number to octal number (with decimal intermediary)
<a href="#details">More...</a></p>
<div class="textblock"><code>#include &lt;stdio.h&gt;</code><br />
<code>#include &lt;string.h&gt;</code><br />
</div><div class="textblock"><div class="dynheader">
Include dependency graph for hexadecimal_to_octal2.c:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d8/dfc/hexadecimal__to__octal2_8c__incl.svg" width="180" height="124"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="define-members" name="define-members"></a>
Macros</h2></td></tr>
<tr class="memitem:ad47196c6a06f99a1437e9569bae13c5f"><td class="memItemLeft" align="right" valign="top"><a id="ad47196c6a06f99a1437e9569bae13c5f" name="ad47196c6a06f99a1437e9569bae13c5f"></a>
#define&#160;</td><td class="memItemRight" valign="bottom"><b>MAX_OCT_STR_LEN</b>&#160;&#160;&#160;23 /* 17_7777_7777_7777_7777_7777 */</td></tr>
<tr class="separator:ad47196c6a06f99a1437e9569bae13c5f"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ab64e6838b9026996a18766d334beea20"><td class="memItemLeft" align="right" valign="top"><a id="ab64e6838b9026996a18766d334beea20" name="ab64e6838b9026996a18766d334beea20"></a>
#define&#160;</td><td class="memItemRight" valign="bottom"><b>MAX_HEX_STR_LEN</b>&#160;&#160;&#160;17 /* FFFF_FFFF_FFFF_FFFF */</td></tr>
<tr class="separator:ab64e6838b9026996a18766d334beea20"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="func-members" name="func-members"></a>
Functions</h2></td></tr>
<tr class="memitem:ab83549c0d43c88544be47fa6c4885ae4"><td class="memItemLeft" align="right" valign="top">const char *&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d0/dd9/hexadecimal__to__octal2_8c.html#ab83549c0d43c88544be47fa6c4885ae4">hex_to_oct</a> (const char *hex)</td></tr>
<tr class="memdesc:ab83549c0d43c88544be47fa6c4885ae4"><td class="mdescLeft">&#160;</td><td class="mdescRight">for printf() and fgets() <a href="../../d0/dd9/hexadecimal__to__octal2_8c.html#ab83549c0d43c88544be47fa6c4885ae4">More...</a><br /></td></tr>
<tr class="separator:ab83549c0d43c88544be47fa6c4885ae4"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="memItemLeft" align="right" valign="top">int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d0/dd9/hexadecimal__to__octal2_8c.html#ae66f6b31b5ad750f1fe042a706a4e3d4">main</a> ()</td></tr>
<tr class="memdesc:ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="mdescLeft">&#160;</td><td class="mdescRight">Main function. <a href="../../d0/dd9/hexadecimal__to__octal2_8c.html#ae66f6b31b5ad750f1fe042a706a4e3d4">More...</a><br /></td></tr>
<tr class="separator:ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table>
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
<div class="textblock"><p >Convert hexadecimal number to octal number (with decimal intermediary) </p>
<p >The input is valid from 0 to 0xFFFF_FFFF_FFFF_FFFF.</p>
<p >At first, this program converts a hex string to an unsigned long long decimal, and then to an octal string.</p>
<p >When there is an invalid character in input string, this program stops parsing and converts the string until that character.</p>
<dl class="section see"><dt>See also</dt><dd>hexadecimal_to_octal.c </dd></dl>
</div><h2 class="groupheader">Function Documentation</h2>
<a id="ab83549c0d43c88544be47fa6c4885ae4" name="ab83549c0d43c88544be47fa6c4885ae4"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ab83549c0d43c88544be47fa6c4885ae4">&#9670;&#160;</a></span>hex_to_oct()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">const char * hex_to_oct </td>
<td>(</td>
<td class="paramtype">const char *&#160;</td>
<td class="paramname"><em>hex</em></td><td>)</td>
<td></td>
</tr>
</table>
</div><div class="memdoc">
<p>for printf() and fgets() </p>
<p >for memset()</p>
<p >Convert a hexadecimal number to octal number. </p><dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">hex</td><td>Hexadecimal number to convert. </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>A pointer to the converted octal string. </dd></dl>
<div class="fragment"><div class="line"><span class="lineno"> 25</span>{</div>
<div class="line"><span class="lineno"> 26</span><span class="preprocessor">#define MAX_OCT_STR_LEN 23 </span><span class="comment">/* 17_7777_7777_7777_7777_7777 */</span><span class="preprocessor"></span></div>
<div class="line"><span class="lineno"> 27</span> <span class="keyword">static</span> <span class="keywordtype">char</span> octal[MAX_OCT_STR_LEN];</div>
<div class="line"><span class="lineno"> 28</span> memset(octal, <span class="charliteral">&#39;\0&#39;</span>, MAX_OCT_STR_LEN); <span class="comment">// Initialize as NULL string</span></div>
<div class="line"><span class="lineno"> 29</span> </div>
<div class="line"><span class="lineno"> 30</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> <span class="keywordtype">long</span> decimal = 0;</div>
<div class="line"><span class="lineno"> 31</span> <span class="keywordtype">int</span> i = 0;</div>
<div class="line"><span class="lineno"> 32</span> <span class="keywordtype">int</span> len;</div>
<div class="line"><span class="lineno"> 33</span> </div>
<div class="line"><span class="lineno"> 34</span> <span class="keywordflow">if</span> (hex == NULL)</div>
<div class="line"><span class="lineno"> 35</span> {</div>
<div class="line"><span class="lineno"> 36</span> <span class="comment">// Return an empty string</span></div>
<div class="line"><span class="lineno"> 37</span> <span class="keywordflow">return</span> octal;</div>
<div class="line"><span class="lineno"> 38</span> }</div>
<div class="line"><span class="lineno"> 39</span> </div>
<div class="line"><span class="lineno"> 40</span> <span class="comment">/* Hexadecimal to decimal conversion */</span></div>
<div class="line"><span class="lineno"> 41</span> <span class="keywordflow">while</span> (*hex != <span class="charliteral">&#39;\n&#39;</span> &amp;&amp; *hex != <span class="charliteral">&#39;\0&#39;</span>)</div>
<div class="line"><span class="lineno"> 42</span> {</div>
<div class="line"><span class="lineno"> 43</span> <span class="keywordtype">char</span> ch = *hex;</div>
<div class="line"><span class="lineno"> 44</span> </div>
<div class="line"><span class="lineno"> 45</span> <span class="keywordflow">if</span> (ch &gt;= <span class="charliteral">&#39;0&#39;</span> &amp;&amp; ch &lt;= <span class="charliteral">&#39;9&#39;</span>)</div>
<div class="line"><span class="lineno"> 46</span> {</div>
<div class="line"><span class="lineno"> 47</span> ch -= <span class="charliteral">&#39;0&#39;</span>;</div>
<div class="line"><span class="lineno"> 48</span> }</div>
<div class="line"><span class="lineno"> 49</span> <span class="keywordflow">else</span> <span class="keywordflow">if</span> (ch &gt;= <span class="charliteral">&#39;a&#39;</span> &amp;&amp; ch &lt;= <span class="charliteral">&#39;f&#39;</span>)</div>
<div class="line"><span class="lineno"> 50</span> {</div>
<div class="line"><span class="lineno"> 51</span> ch = ch - <span class="charliteral">&#39;a&#39;</span> + 10;</div>
<div class="line"><span class="lineno"> 52</span> }</div>
<div class="line"><span class="lineno"> 53</span> <span class="keywordflow">else</span> <span class="keywordflow">if</span> (ch &gt;= <span class="charliteral">&#39;A&#39;</span> &amp;&amp; ch &lt;= <span class="charliteral">&#39;F&#39;</span>)</div>
<div class="line"><span class="lineno"> 54</span> {</div>
<div class="line"><span class="lineno"> 55</span> ch = ch - <span class="charliteral">&#39;A&#39;</span> + 10;</div>
<div class="line"><span class="lineno"> 56</span> }</div>
<div class="line"><span class="lineno"> 57</span> <span class="keywordflow">else</span></div>
<div class="line"><span class="lineno"> 58</span> {</div>
<div class="line"><span class="lineno"> 59</span> printf(<span class="stringliteral">&quot;Invalid hexadecimal input: %c\n&quot;</span>, ch);</div>
<div class="line"><span class="lineno"> 60</span> <span class="keywordflow">break</span>;</div>
<div class="line"><span class="lineno"> 61</span> }</div>
<div class="line"><span class="lineno"> 62</span> </div>
<div class="line"><span class="lineno"> 63</span> decimal *= 16;</div>
<div class="line"><span class="lineno"> 64</span> decimal += ch;</div>
<div class="line"><span class="lineno"> 65</span> hex++;</div>
<div class="line"><span class="lineno"> 66</span> }</div>
<div class="line"><span class="lineno"> 67</span> </div>
<div class="line"><span class="lineno"> 68</span> <span class="comment">/* Decimal to octal conversion */</span></div>
<div class="line"><span class="lineno"> 69</span> <span class="keywordflow">if</span> (decimal == 0)</div>
<div class="line"><span class="lineno"> 70</span> {</div>
<div class="line"><span class="lineno"> 71</span> octal[0] = <span class="charliteral">&#39;0&#39;</span>;</div>
<div class="line"><span class="lineno"> 72</span> len = 1;</div>
<div class="line"><span class="lineno"> 73</span> }</div>
<div class="line"><span class="lineno"> 74</span> <span class="keywordflow">else</span></div>
<div class="line"><span class="lineno"> 75</span> {</div>
<div class="line"><span class="lineno"> 76</span> i = 0;</div>
<div class="line"><span class="lineno"> 77</span> <span class="keywordflow">while</span> (decimal &gt; 0)</div>
<div class="line"><span class="lineno"> 78</span> {</div>
<div class="line"><span class="lineno"> 79</span> octal[i] = <span class="charliteral">&#39;0&#39;</span> + decimal % 8;</div>
<div class="line"><span class="lineno"> 80</span> i++;</div>
<div class="line"><span class="lineno"> 81</span> decimal /= 8;</div>
<div class="line"><span class="lineno"> 82</span> }</div>
<div class="line"><span class="lineno"> 83</span> </div>
<div class="line"><span class="lineno"> 84</span> len = i;</div>
<div class="line"><span class="lineno"> 85</span> }</div>
<div class="line"><span class="lineno"> 86</span> </div>
<div class="line"><span class="lineno"> 87</span> octal[len] = <span class="charliteral">&#39;\0&#39;</span>;</div>
<div class="line"><span class="lineno"> 88</span> </div>
<div class="line"><span class="lineno"> 89</span> <span class="comment">/* Reverse the octal string */</span></div>
<div class="line"><span class="lineno"> 90</span> <span class="keywordflow">for</span> (i = 0; i &lt; len / 2; i++)</div>
<div class="line"><span class="lineno"> 91</span> {</div>
<div class="line"><span class="lineno"> 92</span> <span class="keywordtype">char</span> tmp = octal[i];</div>
<div class="line"><span class="lineno"> 93</span> octal[i] = octal[len - i - 1];</div>
<div class="line"><span class="lineno"> 94</span> octal[len - i - 1] = tmp;</div>
<div class="line"><span class="lineno"> 95</span> }</div>
<div class="line"><span class="lineno"> 96</span> </div>
<div class="line"><span class="lineno"> 97</span> <span class="keywordflow">return</span> octal;</div>
<div class="line"><span class="lineno"> 98</span>}</div>
</div><!-- fragment -->
</div>
</div>
<a id="ae66f6b31b5ad750f1fe042a706a4e3d4" name="ae66f6b31b5ad750f1fe042a706a4e3d4"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ae66f6b31b5ad750f1fe042a706a4e3d4">&#9670;&#160;</a></span>main()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">int main </td>
<td>(</td>
<td class="paramtype">void&#160;</td>
<td class="paramname"></td><td>)</td>
<td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Main function. </p>
<dl class="section return"><dt>Returns</dt><dd>0 on exit </dd></dl>
<div class="fragment"><div class="line"><span class="lineno"> 105</span>{</div>
<div class="line"><span class="lineno"> 106</span><span class="preprocessor">#define MAX_HEX_STR_LEN 17 </span><span class="comment">/* FFFF_FFFF_FFFF_FFFF */</span><span class="preprocessor"></span></div>
<div class="line"><span class="lineno"> 107</span> <span class="keywordtype">char</span> hex[MAX_HEX_STR_LEN];</div>
<div class="line"><span class="lineno"> 108</span> </div>
<div class="line"><span class="lineno"> 109</span> <span class="comment">/* Input hexadecimal number from user */</span></div>
<div class="line"><span class="lineno"> 110</span> printf(<span class="stringliteral">&quot;Enter any hexadecimal number: &quot;</span>);</div>
<div class="line"><span class="lineno"> 111</span> fgets(hex, MAX_HEX_STR_LEN, stdin);</div>
<div class="line"><span class="lineno"> 112</span> </div>
<div class="line"><span class="lineno"> 113</span> <span class="keyword">const</span> <span class="keywordtype">char</span> *octal = <a class="code hl_function" href="../../d0/dd9/hexadecimal__to__octal2_8c.html#ab83549c0d43c88544be47fa6c4885ae4">hex_to_oct</a>(hex);</div>
<div class="line"><span class="lineno"> 114</span> </div>
<div class="line"><span class="lineno"> 115</span> printf(<span class="stringliteral">&quot;Hexadecimal number = %s\n&quot;</span>, hex);</div>
<div class="line"><span class="lineno"> 116</span> printf(<span class="stringliteral">&quot;Octal number = %s\n&quot;</span>, octal);</div>
<div class="line"><span class="lineno"> 117</span> </div>
<div class="line"><span class="lineno"> 118</span> <span class="keywordflow">return</span> 0;</div>
<div class="line"><span class="lineno"> 119</span>}</div>
<div class="ttc" id="ahexadecimal__to__octal2_8c_html_ab83549c0d43c88544be47fa6c4885ae4"><div class="ttname"><a href="../../d0/dd9/hexadecimal__to__octal2_8c.html#ab83549c0d43c88544be47fa6c4885ae4">hex_to_oct</a></div><div class="ttdeci">const char * hex_to_oct(const char *hex)</div><div class="ttdoc">for printf() and fgets()</div><div class="ttdef"><b>Definition:</b> hexadecimal_to_octal2.c:24</div></div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d0/dd9/hexadecimal__to__octal2_8c_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph.svg" width="196" height="36"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div>
</div>
</div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<li class="navelem"><a class="el" href="../../dir_9babc524d57e25f3d9f4179cabc3599e.html">conversions</a></li><li class="navelem"><a class="el" href="../../d0/dd9/hexadecimal__to__octal2_8c.html">hexadecimal_to_octal2.c</a></li>
<li class="footer">Generated by <a href="https://www.doxygen.org/index.html"><img class="footer" src="../../doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.9.5 </li>
</ul>
</div>
</body>
</html>