TheAlgorithms-C/dd/d53/int__to__string_8c.html

269 lines
19 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/int_to_string.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('dd/d53/int__to__string_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">int_to_string.c File Reference</div> </div>
</div><!--header-->
<div class="contents">
<p>Convert a positive integer to string (non-standard function) representation.
<a href="#details">More...</a></p>
<div class="textblock"><code>#include &lt;assert.h&gt;</code><br />
<code>#include &lt;inttypes.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 />
<code>#include &lt;time.h&gt;</code><br />
</div><div class="textblock"><div class="dynheader">
Include dependency graph for int_to_string.c:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../db/d37/int__to__string_8c__incl.svg" width="531" 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:a969911f32f1c435bb2bf166574ef9ae5"><td class="memItemLeft" align="right" valign="top">char *&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../dd/d53/int__to__string_8c.html#a969911f32f1c435bb2bf166574ef9ae5">int_to_string</a> (uint16_t value, char *dest, int base)</td></tr>
<tr class="memdesc:a969911f32f1c435bb2bf166574ef9ae5"><td class="mdescLeft">&#160;</td><td class="mdescRight">Converts an integer value to a null-terminated string using the specified base and stores the result in the array given by str parameter. <a href="../../dd/d53/int__to__string_8c.html#a969911f32f1c435bb2bf166574ef9ae5">More...</a><br /></td></tr>
<tr class="separator:a969911f32f1c435bb2bf166574ef9ae5"><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="../../dd/d53/int__to__string_8c.html#aa8dca7b867074164d5f45b0f3851269d">test</a> ()</td></tr>
<tr class="memdesc:aa8dca7b867074164d5f45b0f3851269d"><td class="mdescLeft">&#160;</td><td class="mdescRight">Test function. <a href="../../dd/d53/int__to__string_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"><a id="ae66f6b31b5ad750f1fe042a706a4e3d4"></a>
int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../dd/d53/int__to__string_8c.html#ae66f6b31b5ad750f1fe042a706a4e3d4">main</a> ()</td></tr>
<tr class="memdesc:ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="mdescLeft">&#160;</td><td class="mdescRight">Driver Code. <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 a positive integer to string (non-standard function) representation. </p>
</div><h2 class="groupheader">Function Documentation</h2>
<a id="a969911f32f1c435bb2bf166574ef9ae5"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a969911f32f1c435bb2bf166574ef9ae5">&#9670;&nbsp;</a></span>int_to_string()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">char* int_to_string </td>
<td>(</td>
<td class="paramtype">uint16_t&#160;</td>
<td class="paramname"><em>value</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">char *&#160;</td>
<td class="paramname"><em>dest</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>base</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Converts an integer value to a null-terminated string using the specified base and stores the result in the array given by str parameter. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">value</td><td>Value to be converted to a string. </td></tr>
<tr><td class="paramname">dest</td><td>pointer to array in memory to store the resulting null-terminated string. </td></tr>
<tr><td class="paramname">base</td><td>Numerical base used to represent the value as a string, between 2 and 16, where 10 means decimal base, 16 hexadecimal, 8 octal, and 2 binary. </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>A pointer to the resulting null-terminated string, same as parameter str. </dd></dl>
<dl class="section note"><dt>Note</dt><dd>The destination array must be pre-allocated by the calling function. </dd></dl>
<div class="fragment"><div class="line"><a name="l00026"></a><span class="lineno"> 26</span>&#160;{</div>
<div class="line"><a name="l00027"></a><span class="lineno"> 27</span>&#160; <span class="keyword">const</span> <span class="keywordtype">char</span> hex_table[] = {<span class="charliteral">&#39;0&#39;</span>, <span class="charliteral">&#39;1&#39;</span>, <span class="charliteral">&#39;2&#39;</span>, <span class="charliteral">&#39;3&#39;</span>, <span class="charliteral">&#39;4&#39;</span>, <span class="charliteral">&#39;5&#39;</span>, <span class="charliteral">&#39;6&#39;</span>, <span class="charliteral">&#39;7&#39;</span>,</div>
<div class="line"><a name="l00028"></a><span class="lineno"> 28</span>&#160; <span class="charliteral">&#39;8&#39;</span>, <span class="charliteral">&#39;9&#39;</span>, <span class="charliteral">&#39;a&#39;</span>, <span class="charliteral">&#39;b&#39;</span>, <span class="charliteral">&#39;c&#39;</span>, <span class="charliteral">&#39;d&#39;</span>, <span class="charliteral">&#39;e&#39;</span>, <span class="charliteral">&#39;f&#39;</span>};</div>
<div class="line"><a name="l00029"></a><span class="lineno"> 29</span>&#160; </div>
<div class="line"><a name="l00030"></a><span class="lineno"> 30</span>&#160; <span class="keywordtype">int</span> len = 0;</div>
<div class="line"><a name="l00031"></a><span class="lineno"> 31</span>&#160; <span class="keywordflow">do</span></div>
<div class="line"><a name="l00032"></a><span class="lineno"> 32</span>&#160; {</div>
<div class="line"><a name="l00033"></a><span class="lineno"> 33</span>&#160; dest[len++] = hex_table[value % base];</div>
<div class="line"><a name="l00034"></a><span class="lineno"> 34</span>&#160; value /= base;</div>
<div class="line"><a name="l00035"></a><span class="lineno"> 35</span>&#160; } <span class="keywordflow">while</span> (value != 0);</div>
<div class="line"><a name="l00036"></a><span class="lineno"> 36</span>&#160; </div>
<div class="line"><a name="l00037"></a><span class="lineno"> 37</span>&#160; <span class="comment">/* reverse characters */</span></div>
<div class="line"><a name="l00038"></a><span class="lineno"> 38</span>&#160; <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0, limit = len / 2; i &lt; limit; ++i)</div>
<div class="line"><a name="l00039"></a><span class="lineno"> 39</span>&#160; {</div>
<div class="line"><a name="l00040"></a><span class="lineno"> 40</span>&#160; <span class="keywordtype">char</span> t = dest[i];</div>
<div class="line"><a name="l00041"></a><span class="lineno"> 41</span>&#160; dest[i] = dest[len - 1 - i];</div>
<div class="line"><a name="l00042"></a><span class="lineno"> 42</span>&#160; dest[len - 1 - i] = t;</div>
<div class="line"><a name="l00043"></a><span class="lineno"> 43</span>&#160; }</div>
<div class="line"><a name="l00044"></a><span class="lineno"> 44</span>&#160; dest[len] = <span class="charliteral">&#39;\0&#39;</span>;</div>
<div class="line"><a name="l00045"></a><span class="lineno"> 45</span>&#160; <span class="keywordflow">return</span> dest;</div>
<div class="line"><a name="l00046"></a><span class="lineno"> 46</span>&#160;}</div>
</div><!-- fragment -->
</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><code>void</code> </dd></dl>
<div class="fragment"><div class="line"><a name="l00052"></a><span class="lineno"> 52</span>&#160;{</div>
<div class="line"><a name="l00053"></a><span class="lineno"> 53</span>&#160; <span class="keyword">const</span> <span class="keywordtype">int</span> <a class="code" href="../../db/dd5/prime__seive_8c.html#ac1215a37edfa07d37edf6ec65f2235c7">MAX_SIZE</a> = 100;</div>
<div class="line"><a name="l00054"></a><span class="lineno"> 54</span>&#160; <span class="keywordtype">char</span> *str1 = (<span class="keywordtype">char</span> *)calloc(<span class="keyword">sizeof</span>(<span class="keywordtype">char</span>), <a class="code" href="../../db/dd5/prime__seive_8c.html#ac1215a37edfa07d37edf6ec65f2235c7">MAX_SIZE</a>);</div>
<div class="line"><a name="l00055"></a><span class="lineno"> 55</span>&#160; <span class="keywordtype">char</span> *str2 = (<span class="keywordtype">char</span> *)calloc(<span class="keyword">sizeof</span>(<span class="keywordtype">char</span>), <a class="code" href="../../db/dd5/prime__seive_8c.html#ac1215a37edfa07d37edf6ec65f2235c7">MAX_SIZE</a>);</div>
<div class="line"><a name="l00056"></a><span class="lineno"> 56</span>&#160; </div>
<div class="line"><a name="l00057"></a><span class="lineno"> 57</span>&#160; <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 1; i &lt;= 100; ++i) <span class="comment">/* test 100 random numbers */</span></div>
<div class="line"><a name="l00058"></a><span class="lineno"> 58</span>&#160; {</div>
<div class="line"><a name="l00059"></a><span class="lineno"> 59</span>&#160; <span class="comment">/* Generate value from 0 to 100 */</span></div>
<div class="line"><a name="l00060"></a><span class="lineno"> 60</span>&#160; <span class="keywordtype">int</span> value = rand() % 100;</div>
<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">// assert(strcmp(itoa(value, str1, 2), int_to_string(value, str2, 2)) ==</span></div>
<div class="line"><a name="l00063"></a><span class="lineno"> 63</span>&#160; <span class="comment">// 0);</span></div>
<div class="line"><a name="l00064"></a><span class="lineno"> 64</span>&#160; snprintf(str1, <a class="code" href="../../db/dd5/prime__seive_8c.html#ac1215a37edfa07d37edf6ec65f2235c7">MAX_SIZE</a>, <span class="stringliteral">&quot;%o&quot;</span>, value); <span class="comment">//* standard C - to octal */</span></div>
<div class="line"><a name="l00065"></a><span class="lineno"> 65</span>&#160; assert(strcmp(str1, <a class="code" href="../../dd/d53/int__to__string_8c.html#a969911f32f1c435bb2bf166574ef9ae5">int_to_string</a>(value, str2, 8)) == 0);</div>
<div class="line"><a name="l00066"></a><span class="lineno"> 66</span>&#160; snprintf(str1, <a class="code" href="../../db/dd5/prime__seive_8c.html#ac1215a37edfa07d37edf6ec65f2235c7">MAX_SIZE</a>, <span class="stringliteral">&quot;%d&quot;</span>, value); <span class="comment">/* standard C - to decimal */</span></div>
<div class="line"><a name="l00067"></a><span class="lineno"> 67</span>&#160; assert(strcmp(str1, <a class="code" href="../../dd/d53/int__to__string_8c.html#a969911f32f1c435bb2bf166574ef9ae5">int_to_string</a>(value, str2, 10)) == 0);</div>
<div class="line"><a name="l00068"></a><span class="lineno"> 68</span>&#160; snprintf(str1, <a class="code" href="../../db/dd5/prime__seive_8c.html#ac1215a37edfa07d37edf6ec65f2235c7">MAX_SIZE</a>, <span class="stringliteral">&quot;%x&quot;</span>, value); <span class="comment">/* standard C - to hexadecimal */</span></div>
<div class="line"><a name="l00069"></a><span class="lineno"> 69</span>&#160; assert(strcmp(str1, <a class="code" href="../../dd/d53/int__to__string_8c.html#a969911f32f1c435bb2bf166574ef9ae5">int_to_string</a>(value, str2, 16)) == 0);</div>
<div class="line"><a name="l00070"></a><span class="lineno"> 70</span>&#160; }</div>
<div class="line"><a name="l00071"></a><span class="lineno"> 71</span>&#160; </div>
<div class="line"><a name="l00072"></a><span class="lineno"> 72</span>&#160; free(str1);</div>
<div class="line"><a name="l00073"></a><span class="lineno"> 73</span>&#160; free(str2);</div>
<div class="line"><a name="l00074"></a><span class="lineno"> 74</span>&#160;}</div>
<div class="ttc" id="aint__to__string_8c_html_a969911f32f1c435bb2bf166574ef9ae5"><div class="ttname"><a href="../../dd/d53/int__to__string_8c.html#a969911f32f1c435bb2bf166574ef9ae5">int_to_string</a></div><div class="ttdeci">char * int_to_string(uint16_t value, char *dest, int base)</div><div class="ttdoc">Converts an integer value to a null-terminated string using the specified base and stores the result ...</div><div class="ttdef"><b>Definition:</b> int_to_string.c:25</div></div>
<div class="ttc" id="aprime__seive_8c_html_ac1215a37edfa07d37edf6ec65f2235c7"><div class="ttname"><a href="../../db/dd5/prime__seive_8c.html#ac1215a37edfa07d37edf6ec65f2235c7">MAX_SIZE</a></div><div class="ttdeci">const unsigned long long MAX_SIZE</div><div class="ttdoc">for assert for standard input output for general purpose standard library</div><div class="ttdef"><b>Definition:</b> prime_seive.c:11</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="../../dd/d53/int__to__string_8c_aa8dca7b867074164d5f45b0f3851269d_cgraph.svg" width="196" 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="../../dd/d53/int__to__string_8c.html">int_to_string.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>