mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
270 lines
19 KiB
HTML
270 lines
19 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">
|
||
|
<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.8.18"/>
|
||
|
<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
|
||
|
 <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.8.18 -->
|
||
|
<script type="text/javascript">
|
||
|
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */
|
||
|
var searchBox = new SearchBox("searchBox", "../../search",false,'Search');
|
||
|
/* @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&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&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 <assert.h></code><br />
|
||
|
<code>#include <inttypes.h></code><br />
|
||
|
<code>#include <stdio.h></code><br />
|
||
|
<code>#include <stdlib.h></code><br />
|
||
|
<code>#include <string.h></code><br />
|
||
|
<code>#include <time.h></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 * </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"> </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"> </td></tr>
|
||
|
<tr class="memitem:aa8dca7b867074164d5f45b0f3851269d"><td class="memItemLeft" align="right" valign="top">static void </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"> </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"> </td></tr>
|
||
|
<tr class="memitem:ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="memItemLeft" align="right" valign="top"><a id="ae66f6b31b5ad750f1fe042a706a4e3d4"></a>
|
||
|
int </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"> </td><td class="mdescRight">Driver Code. <br /></td></tr>
|
||
|
<tr class="separator:ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="memSeparator" colspan="2"> </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">◆ </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 </td>
|
||
|
<td class="paramname"><em>value</em>, </td>
|
||
|
</tr>
|
||
|
<tr>
|
||
|
<td class="paramkey"></td>
|
||
|
<td></td>
|
||
|
<td class="paramtype">char * </td>
|
||
|
<td class="paramname"><em>dest</em>, </td>
|
||
|
</tr>
|
||
|
<tr>
|
||
|
<td class="paramkey"></td>
|
||
|
<td></td>
|
||
|
<td class="paramtype">int </td>
|
||
|
<td class="paramname"><em>base</em> </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> {</div>
|
||
|
<div class="line"><a name="l00027"></a><span class="lineno"> 27</span>  <span class="keyword">const</span> <span class="keywordtype">char</span> hex_table[] = {<span class="charliteral">'0'</span>, <span class="charliteral">'1'</span>, <span class="charliteral">'2'</span>, <span class="charliteral">'3'</span>, <span class="charliteral">'4'</span>, <span class="charliteral">'5'</span>, <span class="charliteral">'6'</span>, <span class="charliteral">'7'</span>,</div>
|
||
|
<div class="line"><a name="l00028"></a><span class="lineno"> 28</span>  <span class="charliteral">'8'</span>, <span class="charliteral">'9'</span>, <span class="charliteral">'a'</span>, <span class="charliteral">'b'</span>, <span class="charliteral">'c'</span>, <span class="charliteral">'d'</span>, <span class="charliteral">'e'</span>, <span class="charliteral">'f'</span>};</div>
|
||
|
<div class="line"><a name="l00029"></a><span class="lineno"> 29</span>  </div>
|
||
|
<div class="line"><a name="l00030"></a><span class="lineno"> 30</span>  <span class="keywordtype">int</span> len = 0;</div>
|
||
|
<div class="line"><a name="l00031"></a><span class="lineno"> 31</span>  <span class="keywordflow">do</span></div>
|
||
|
<div class="line"><a name="l00032"></a><span class="lineno"> 32</span>  {</div>
|
||
|
<div class="line"><a name="l00033"></a><span class="lineno"> 33</span>  dest[len++] = hex_table[value % base];</div>
|
||
|
<div class="line"><a name="l00034"></a><span class="lineno"> 34</span>  value /= base;</div>
|
||
|
<div class="line"><a name="l00035"></a><span class="lineno"> 35</span>  } <span class="keywordflow">while</span> (value != 0);</div>
|
||
|
<div class="line"><a name="l00036"></a><span class="lineno"> 36</span>  </div>
|
||
|
<div class="line"><a name="l00037"></a><span class="lineno"> 37</span>  <span class="comment">/* reverse characters */</span></div>
|
||
|
<div class="line"><a name="l00038"></a><span class="lineno"> 38</span>  <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0, limit = len / 2; i < limit; ++i)</div>
|
||
|
<div class="line"><a name="l00039"></a><span class="lineno"> 39</span>  {</div>
|
||
|
<div class="line"><a name="l00040"></a><span class="lineno"> 40</span>  <span class="keywordtype">char</span> t = dest[i];</div>
|
||
|
<div class="line"><a name="l00041"></a><span class="lineno"> 41</span>  dest[i] = dest[len - 1 - i];</div>
|
||
|
<div class="line"><a name="l00042"></a><span class="lineno"> 42</span>  dest[len - 1 - i] = t;</div>
|
||
|
<div class="line"><a name="l00043"></a><span class="lineno"> 43</span>  }</div>
|
||
|
<div class="line"><a name="l00044"></a><span class="lineno"> 44</span>  dest[len] = <span class="charliteral">'\0'</span>;</div>
|
||
|
<div class="line"><a name="l00045"></a><span class="lineno"> 45</span>  <span class="keywordflow">return</span> dest;</div>
|
||
|
<div class="line"><a name="l00046"></a><span class="lineno"> 46</span> }</div>
|
||
|
</div><!-- fragment -->
|
||
|
</div>
|
||
|
</div>
|
||
|
<a id="aa8dca7b867074164d5f45b0f3851269d"></a>
|
||
|
<h2 class="memtitle"><span class="permalink"><a href="#aa8dca7b867074164d5f45b0f3851269d">◆ </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="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> {</div>
|
||
|
<div class="line"><a name="l00053"></a><span class="lineno"> 53</span>  <span class="keyword">const</span> <span class="keywordtype">int</span> <a class="code" href="../../df/df3/union__find_8c.html#a0592dba56693fad79136250c11e5a7fe">MAX_SIZE</a> = 100;</div>
|
||
|
<div class="line"><a name="l00054"></a><span class="lineno"> 54</span>  <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="../../df/df3/union__find_8c.html#a0592dba56693fad79136250c11e5a7fe">MAX_SIZE</a>);</div>
|
||
|
<div class="line"><a name="l00055"></a><span class="lineno"> 55</span>  <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="../../df/df3/union__find_8c.html#a0592dba56693fad79136250c11e5a7fe">MAX_SIZE</a>);</div>
|
||
|
<div class="line"><a name="l00056"></a><span class="lineno"> 56</span>  </div>
|
||
|
<div class="line"><a name="l00057"></a><span class="lineno"> 57</span>  <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 1; i <= 100; ++i) <span class="comment">/* test 100 random numbers */</span></div>
|
||
|
<div class="line"><a name="l00058"></a><span class="lineno"> 58</span>  {</div>
|
||
|
<div class="line"><a name="l00059"></a><span class="lineno"> 59</span>  <span class="comment">/* Generate value from 0 to 100 */</span></div>
|
||
|
<div class="line"><a name="l00060"></a><span class="lineno"> 60</span>  <span class="keywordtype">int</span> value = rand() % 100;</div>
|
||
|
<div class="line"><a name="l00061"></a><span class="lineno"> 61</span>  </div>
|
||
|
<div class="line"><a name="l00062"></a><span class="lineno"> 62</span>  <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>  <span class="comment">// 0);</span></div>
|
||
|
<div class="line"><a name="l00064"></a><span class="lineno"> 64</span>  snprintf(str1, <a class="code" href="../../df/df3/union__find_8c.html#a0592dba56693fad79136250c11e5a7fe">MAX_SIZE</a>, <span class="stringliteral">"%o"</span>, value); <span class="comment">//* standard C - to octal */</span></div>
|
||
|
<div class="line"><a name="l00065"></a><span class="lineno"> 65</span>  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>  snprintf(str1, <a class="code" href="../../df/df3/union__find_8c.html#a0592dba56693fad79136250c11e5a7fe">MAX_SIZE</a>, <span class="stringliteral">"%d"</span>, value); <span class="comment">/* standard C - to decimal */</span></div>
|
||
|
<div class="line"><a name="l00067"></a><span class="lineno"> 67</span>  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>  snprintf(str1, <a class="code" href="../../df/df3/union__find_8c.html#a0592dba56693fad79136250c11e5a7fe">MAX_SIZE</a>, <span class="stringliteral">"%x"</span>, value); <span class="comment">/* standard C - to hexadecimal */</span></div>
|
||
|
<div class="line"><a name="l00069"></a><span class="lineno"> 69</span>  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>  }</div>
|
||
|
<div class="line"><a name="l00071"></a><span class="lineno"> 71</span>  </div>
|
||
|
<div class="line"><a name="l00072"></a><span class="lineno"> 72</span>  free(str1);</div>
|
||
|
<div class="line"><a name="l00073"></a><span class="lineno"> 73</span>  free(str2);</div>
|
||
|
<div class="line"><a name="l00074"></a><span class="lineno"> 74</span> }</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 -->
|
||
|
<div class="ttc" id="aunion__find_8c_html_a0592dba56693fad79136250c11e5a7fe"><div class="ttname"><a href="../../df/df3/union__find_8c.html#a0592dba56693fad79136250c11e5a7fe">MAX_SIZE</a></div><div class="ttdeci">#define MAX_SIZE</div><div class="ttdoc">maximum number of elements in the set</div><div class="ttdef"><b>Definition:</b> union_find.c:8</div></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>
|
||
|
<!-- 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="http://www.doxygen.org/index.html">
|
||
|
<img class="footer" src="../../doxygen.png" alt="doxygen"/></a> 1.8.18 </li>
|
||
|
</ul>
|
||
|
</div>
|
||
|
</body>
|
||
|
</html>
|