Note that there are some explanatory texts on larger screens.

plurals
  1. POinteger to string
    text
    copied!<p>The strutils.h library contains a function IntegerToString. (You might have wondered how the computer actually goes about the process of converting an integer into its string representation.)</p> <p>As it turns out, the easiest way to implement this function is to use the recursive decomposition of an integer. </p> <p>Someone wrote a recursive implementation of IntegerToString so that it operates recursively without using any of the iterative constructs such as while and for.</p> <p>Although it helped me ... i m not able to understand the code :S </p> <pre><code>#include &lt;iostream&gt; #include &lt;string&gt; using namespace std; const char digit_pairs[201] = { "00010203040506070809" "10111213141516171819" "20212223242526272829" "30313233343536373839" "40414243444546474849" "50515253545556575859" "60616263646566676869" "70717273747576777879" "80818283848586878889" "90919293949596979899" }; std::string&amp; itostr(int n, std::string&amp; s) { if(n==0) { s="0"; return s; } int sign = -(n&lt;0); unsigned int val = (n^sign)-sign; int size; if(val&gt;=10000) { if(val&gt;=10000000) { if(val&gt;=1000000000) size=10; else if(val&gt;=100000000) size=9; else size=8; } else { if(val&gt;=1000000) size=7; else if(val&gt;=100000) size=6; else size=5; } } else { if(val&gt;=100) { if(val&gt;=1000) size=4; else size=3; } else { if(val&gt;=10) size=2; else size=1; } } size -= sign; s.resize(size); char* c = &amp;s[0]; if(sign) *c='-'; c += size-1; while(val&gt;=100) { int pos = val % 100; val /= 100; *(short*)(c-1)=*(short*)(digit_pairs+2*pos); c-=2; } while(val&gt;0) { *c--='0' + (val % 10); val /= 10; } return s; } </code></pre> <p>just want to get better understanding of this code.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload