TheAlgorithms-C/d0/d8a/octal__to__hexadecimal_8c.html

311 lines
20 KiB
HTML
Raw Normal View History

<!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">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.9.1"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Algorithms_in_C: conversions/octal_to_hexadecimal.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 style="height: 56px;">
<td id="projectalign" style="padding-left: 0.5em;">
<div id="projectname">Algorithms_in_C
&#160;<span id="projectnumber">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.1 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "../../search",false,'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:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(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:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(document).ready(function(){initNavTree('d0/d8a/octal__to__hexadecimal_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">
<iframe src="javascript:void(0)" frameborder="0"
name="MSearchResults" id="MSearchResults">
</iframe>
</div>
<div class="header">
<div class="summary">
<a href="#func-members">Functions</a> </div>
<div class="headertitle">
<div class="title">octal_to_hexadecimal.c File Reference</div> </div>
</div><!--header-->
<div class="contents">
<p>Octal to hexadecimal conversion by scanning user input.
<a href="#details">More...</a></p>
<div class="textblock"><code>#include &lt;assert.h&gt;</code><br />
<code>#include &lt;math.h&gt;</code><br />
<code>#include &lt;stdio.h&gt;</code><br />
<code>#include &lt;stdlib.h&gt;</code><br />
<code>#include &lt;string.h&gt;</code><br />
</div><div class="textblock"><div class="dynheader">
Include dependency graph for octal_to_hexadecimal.c:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d5/ddd/octal__to__hexadecimal_8c__incl.svg" width="432" height="127"><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 name="func-members"></a>
Functions</h2></td></tr>
<tr class="memitem:a86a4edb605c2a03d9175c59de679347d"><td class="memItemLeft" align="right" valign="top">long&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d0/d8a/octal__to__hexadecimal_8c.html#a86a4edb605c2a03d9175c59de679347d">octalToDecimal</a> (long octalValue)</td></tr>
<tr class="memdesc:a86a4edb605c2a03d9175c59de679347d"><td class="mdescLeft">&#160;</td><td class="mdescRight">Convert octal number to decimal number. <a href="../../d0/d8a/octal__to__hexadecimal_8c.html#a86a4edb605c2a03d9175c59de679347d">More...</a><br /></td></tr>
<tr class="separator:a86a4edb605c2a03d9175c59de679347d"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a042035b4ba7b92974f0edf6eb1b0cbe1"><td class="memItemLeft" align="right" valign="top">char *&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d0/d8a/octal__to__hexadecimal_8c.html#a042035b4ba7b92974f0edf6eb1b0cbe1">octalToHexadecimal</a> (long octalValue)</td></tr>
<tr class="memdesc:a042035b4ba7b92974f0edf6eb1b0cbe1"><td class="mdescLeft">&#160;</td><td class="mdescRight">Convert octal number to hexadecimal number dynamically allocated memory needs to be freed by the calling the function free. <a href="../../d0/d8a/octal__to__hexadecimal_8c.html#a042035b4ba7b92974f0edf6eb1b0cbe1">More...</a><br /></td></tr>
<tr class="separator:a042035b4ba7b92974f0edf6eb1b0cbe1"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:aa8dca7b867074164d5f45b0f3851269d"><td class="memItemLeft" align="right" valign="top">static void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d0/d8a/octal__to__hexadecimal_8c.html#aa8dca7b867074164d5f45b0f3851269d">test</a> ()</td></tr>
<tr class="memdesc:aa8dca7b867074164d5f45b0f3851269d"><td class="mdescLeft">&#160;</td><td class="mdescRight">Test function. <a href="../../d0/d8a/octal__to__hexadecimal_8c.html#aa8dca7b867074164d5f45b0f3851269d">More...</a><br /></td></tr>
<tr class="separator:aa8dca7b867074164d5f45b0f3851269d"><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/d8a/octal__to__hexadecimal_8c.html#ae66f6b31b5ad750f1fe042a706a4e3d4">main</a> ()</td></tr>
<tr class="memdesc:ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="mdescLeft">&#160;</td><td class="mdescRight">Main function. <a href="../../d0/d8a/octal__to__hexadecimal_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>Octal to hexadecimal conversion by scanning user input. </p>
<p>The octalToHexadecimal function take the octal number as long return a string hexadecimal value after conversion </p><dl class="section author"><dt>Author</dt><dd><a href="https://github.com/rachitbhalla">Rachit Bhalla</a> </dd></dl>
</div><h2 class="groupheader">Function Documentation</h2>
<a id="ae66f6b31b5ad750f1fe042a706a4e3d4"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ae66f6b31b5ad750f1fe042a706a4e3d4">&#9670;&nbsp;</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"><a name="l00061"></a><span class="lineno"> 61</span>&#160;{</div>
<div class="line"><a name="l00062"></a><span class="lineno"> 62</span>&#160; <span class="comment">// execute the tests</span></div>
<div class="line"><a name="l00063"></a><span class="lineno"> 63</span>&#160; <a class="code" href="../../d0/d8a/octal__to__hexadecimal_8c.html#aa8dca7b867074164d5f45b0f3851269d">test</a>();</div>
<div class="line"><a name="l00064"></a><span class="lineno"> 64</span>&#160; </div>
<div class="line"><a name="l00065"></a><span class="lineno"> 65</span>&#160; <span class="comment">// get the value of octal number as input</span></div>
<div class="line"><a name="l00066"></a><span class="lineno"> 66</span>&#160; <span class="keywordtype">int</span> octalValue;</div>
<div class="line"><a name="l00067"></a><span class="lineno"> 67</span>&#160; printf(<span class="stringliteral">&quot;Enter an octal number: &quot;</span>);</div>
<div class="line"><a name="l00068"></a><span class="lineno"> 68</span>&#160; scanf(<span class="stringliteral">&quot;%d&quot;</span>, &amp;octalValue);</div>
<div class="line"><a name="l00069"></a><span class="lineno"> 69</span>&#160; </div>
<div class="line"><a name="l00070"></a><span class="lineno"> 70</span>&#160; <span class="comment">// call the function octalToHexadecimal and print the hexadecimal value</span></div>
<div class="line"><a name="l00071"></a><span class="lineno"> 71</span>&#160; <span class="keywordtype">char</span> *hexadecimalValue = <a class="code" href="../../d0/d8a/octal__to__hexadecimal_8c.html#a042035b4ba7b92974f0edf6eb1b0cbe1">octalToHexadecimal</a>(octalValue);</div>
<div class="line"><a name="l00072"></a><span class="lineno"> 72</span>&#160; printf(<span class="stringliteral">&quot;Equivalent hexadecimal number is: %s&quot;</span>, hexadecimalValue);</div>
<div class="line"><a name="l00073"></a><span class="lineno"> 73</span>&#160; </div>
<div class="line"><a name="l00074"></a><span class="lineno"> 74</span>&#160; <span class="comment">// free the memory allocated dynamically in function octalToHexadecimal</span></div>
<div class="line"><a name="l00075"></a><span class="lineno"> 75</span>&#160; free(hexadecimalValue);</div>
<div class="line"><a name="l00076"></a><span class="lineno"> 76</span>&#160; </div>
<div class="line"><a name="l00077"></a><span class="lineno"> 77</span>&#160; <span class="comment">// return 0 and exit</span></div>
<div class="line"><a name="l00078"></a><span class="lineno"> 78</span>&#160; <span class="keywordflow">return</span> 0;</div>
<div class="line"><a name="l00079"></a><span class="lineno"> 79</span>&#160;}</div>
<div class="ttc" id="aoctal__to__hexadecimal_8c_html_a042035b4ba7b92974f0edf6eb1b0cbe1"><div class="ttname"><a href="../../d0/d8a/octal__to__hexadecimal_8c.html#a042035b4ba7b92974f0edf6eb1b0cbe1">octalToHexadecimal</a></div><div class="ttdeci">char * octalToHexadecimal(long octalValue)</div><div class="ttdoc">Convert octal number to hexadecimal number dynamically allocated memory needs to be freed by the call...</div><div class="ttdef"><b>Definition:</b> octal_to_hexadecimal.c:38</div></div>
<div class="ttc" id="aoctal__to__hexadecimal_8c_html_aa8dca7b867074164d5f45b0f3851269d"><div class="ttname"><a href="../../d0/d8a/octal__to__hexadecimal_8c.html#aa8dca7b867074164d5f45b0f3851269d">test</a></div><div class="ttdeci">static void test()</div><div class="ttdoc">Test function.</div><div class="ttdef"><b>Definition:</b> octal_to_hexadecimal.c:48</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/d8a/octal__to__hexadecimal_8c_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph.svg" width="502" height="63"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div>
</div>
<a id="a86a4edb605c2a03d9175c59de679347d"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a86a4edb605c2a03d9175c59de679347d">&#9670;&nbsp;</a></span>octalToDecimal()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">long octalToDecimal </td>
<td>(</td>
<td class="paramtype">long&#160;</td>
<td class="paramname"><em>octalValue</em></td><td>)</td>
<td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Convert octal number to decimal number. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">octalValue</td><td>is the octal number that needs to be converted </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>a decimal number after conversion </dd></dl>
<div class="fragment"><div class="line"><a name="l00020"></a><span class="lineno"> 20</span>&#160; {</div>
<div class="line"><a name="l00021"></a><span class="lineno"> 21</span>&#160; <span class="keywordtype">long</span> decimalValue = 0;</div>
<div class="line"><a name="l00022"></a><span class="lineno"> 22</span>&#160; <span class="keywordtype">int</span> i = 0;</div>
<div class="line"><a name="l00023"></a><span class="lineno"> 23</span>&#160; <span class="keywordflow">while</span> (octalValue) {</div>
<div class="line"><a name="l00024"></a><span class="lineno"> 24</span>&#160; <span class="comment">// Extracts right-most digit, multiplies it with 8^i, and increment i by 1</span></div>
<div class="line"><a name="l00025"></a><span class="lineno"> 25</span>&#160; decimalValue += (long)(octalValue % 10) * pow(8, i++);</div>
<div class="line"><a name="l00026"></a><span class="lineno"> 26</span>&#160; <span class="comment">// Shift right in base 10</span></div>
<div class="line"><a name="l00027"></a><span class="lineno"> 27</span>&#160; octalValue /= 10;</div>
<div class="line"><a name="l00028"></a><span class="lineno"> 28</span>&#160; }</div>
<div class="line"><a name="l00029"></a><span class="lineno"> 29</span>&#160; <span class="keywordflow">return</span> decimalValue;</div>
<div class="line"><a name="l00030"></a><span class="lineno"> 30</span>&#160;}</div>
</div><!-- fragment -->
</div>
</div>
<a id="a042035b4ba7b92974f0edf6eb1b0cbe1"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a042035b4ba7b92974f0edf6eb1b0cbe1">&#9670;&nbsp;</a></span>octalToHexadecimal()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">char* octalToHexadecimal </td>
<td>(</td>
<td class="paramtype">long&#160;</td>
<td class="paramname"><em>octalValue</em></td><td>)</td>
<td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Convert octal number to hexadecimal number dynamically allocated memory needs to be freed by the calling the function free. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">octalValue</td><td>is the octal number that needs to be converted </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>a hexadecimal value as a string after conversion </dd></dl>
<div class="fragment"><div class="line"><a name="l00038"></a><span class="lineno"> 38</span>&#160; {</div>
<div class="line"><a name="l00039"></a><span class="lineno"> 39</span>&#160; <span class="keywordtype">char</span> *hexadecimalValue = malloc(256 * <span class="keyword">sizeof</span>(<span class="keywordtype">char</span>));</div>
<div class="line"><a name="l00040"></a><span class="lineno"> 40</span>&#160; sprintf(hexadecimalValue, <span class="stringliteral">&quot;%lX&quot;</span>, <a class="code" href="../../d0/d8a/octal__to__hexadecimal_8c.html#a86a4edb605c2a03d9175c59de679347d">octalToDecimal</a>(octalValue));</div>
<div class="line"><a name="l00041"></a><span class="lineno"> 41</span>&#160; <span class="keywordflow">return</span> hexadecimalValue;</div>
<div class="line"><a name="l00042"></a><span class="lineno"> 42</span>&#160;}</div>
<div class="ttc" id="aoctal__to__hexadecimal_8c_html_a86a4edb605c2a03d9175c59de679347d"><div class="ttname"><a href="../../d0/d8a/octal__to__hexadecimal_8c.html#a86a4edb605c2a03d9175c59de679347d">octalToDecimal</a></div><div class="ttdeci">long octalToDecimal(long octalValue)</div><div class="ttdoc">Convert octal number to decimal number.</div><div class="ttdef"><b>Definition:</b> octal_to_hexadecimal.c:20</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/d8a/octal__to__hexadecimal_8c_a042035b4ba7b92974f0edf6eb1b0cbe1_cgraph.svg" width="311" height="38"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div>
</div>
<a id="aa8dca7b867074164d5f45b0f3851269d"></a>
<h2 class="memtitle"><span class="permalink"><a href="#aa8dca7b867074164d5f45b0f3851269d">&#9670;&nbsp;</a></span>test()</h2>
<div class="memitem">
<div class="memproto">
<table class="mlabels">
<tr>
<td class="mlabels-left">
<table class="memname">
<tr>
<td class="memname">static void test </td>
<td>(</td>
<td class="paramtype">void&#160;</td>
<td class="paramname"></td><td>)</td>
<td></td>
</tr>
</table>
</td>
<td class="mlabels-right">
<span class="mlabels"><span class="mlabel">static</span></span> </td>
</tr>
</table>
</div><div class="memdoc">
<p>Test function. </p>
<dl class="section return"><dt>Returns</dt><dd>void </dd></dl>
<div class="fragment"><div class="line"><a name="l00048"></a><span class="lineno"> 48</span>&#160; {</div>
<div class="line"><a name="l00049"></a><span class="lineno"> 49</span>&#160; <span class="comment">/* test that hexadecimal value of octal number 213 is 8B */</span></div>
<div class="line"><a name="l00050"></a><span class="lineno"> 50</span>&#160; assert(strcmp(<a class="code" href="../../d0/d8a/octal__to__hexadecimal_8c.html#a042035b4ba7b92974f0edf6eb1b0cbe1">octalToHexadecimal</a>(213), <span class="stringliteral">&quot;8B&quot;</span>) == 0);</div>
<div class="line"><a name="l00051"></a><span class="lineno"> 51</span>&#160; </div>
<div class="line"><a name="l00052"></a><span class="lineno"> 52</span>&#160; <span class="comment">/* test that hexadecimal value of octal number 174 is 7C */</span></div>
<div class="line"><a name="l00053"></a><span class="lineno"> 53</span>&#160; assert(strcmp(<a class="code" href="../../d0/d8a/octal__to__hexadecimal_8c.html#a042035b4ba7b92974f0edf6eb1b0cbe1">octalToHexadecimal</a>(174), <span class="stringliteral">&quot;7C&quot;</span>) == 0);</div>
<div class="line"><a name="l00054"></a><span class="lineno"> 54</span>&#160;}</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/d8a/octal__to__hexadecimal_8c_aa8dca7b867074164d5f45b0f3851269d_cgraph.svg" width="403" height="38"><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/d8a/octal__to__hexadecimal_8c.html">octal_to_hexadecimal.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.1 </li>
</ul>
</div>
</body>
</html>