Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>So if you're in a hurry to get <em>anything</em>, and are not too concerned about efficiency, try this. I've adapted a function that I wrote for the following SO question: <a href="https://stackoverflow.com/questions/15375366/c-string-int-string-int/15375420#15375420">c++ string (int) + string (int)</a></p> <p>First, I modified the <code>Add</code> function to work for any base between 2 and 36:</p> <pre><code>const int MIN_BASE = 2; const int MAX_BASE = 36; static bool tablesInitialised = false; static char tblIntToChar[MAX_BASE] = {0}; static int tblCharToInt[256] = {0}; void InitTables() { if( tablesInitialised ) return; for( int i = 0; i &lt; 10; i++ ) { tblIntToChar[i] = '0' + i; tblCharToInt[tblIntToChar[i]] = i; } for( int i = 0; i &lt; 26; i++ ) { tblIntToChar[i+10] = 'a' + i; tblCharToInt['a' + i] = i + 10; tblCharToInt['A' + i] = i + 10; } tablesInitialised = true; } // Adds two numbers using long addition. string Add( const string&amp; a, const string&amp; b, int base=10 ) { InitTables(); if( base &gt; MAX_BASE || base &lt; MIN_BASE ) return ""; // Reserve storage for the result. string result; result.reserve( 1 + std::max(a.size(), b.size()) ); // Column positions and carry flag. int apos = a.size(); int bpos = b.size(); int carry = 0; // Do long arithmetic. while( carry &gt; 0 || apos &gt; 0 || bpos &gt; 0 ) { if( apos &gt; 0 ) carry += tblCharToInt[(unsigned char)a[--apos]]; if( bpos &gt; 0 ) carry += tblCharToInt[(unsigned char)b[--bpos]]; result.push_back(tblIntToChar[carry%base]); carry /= base; } // The result string is backwards. Reverse and return it. reverse( result.begin(), result.end() ); return result; } // Converts a single value to some base, intended for single-digit conversions. string AsBase( int number, int base ) { InitTables(); if( number &lt;= 0 ) return "0"; string result; while( number &gt; 0 ) { result += tblIntToChar[number%base]; number /= base; } reverse( result.begin(), result.end() ); return result; } </code></pre> <p>And, using all the above, here is a function to convert from one arbitrary base to another:</p> <pre><code>// Converts a number from one base to another. string ConvertBase( const string &amp; number, int oldBase, int newBase ) { InitTables(); string result; for( unsigned digit = 0; digit &lt; number.size(); digit++ ) { int value = tblCharToInt[(unsigned char)number[digit]]; if( result.empty() ) { result = AsBase( value, newBase ); } else { string temp = result; for( int i = 1; i &lt; oldBase; i++ ) { temp = Add( result, temp, newBase ); } result = Add( temp, AsBase(value, newBase), newBase ); } } return result; } </code></pre> <p>This is just something I hacked up using code I mostly had kicking around in my StackOverflow 'for fun' sandbox. It's pretty clunky the way it performs <code>oldBase</code> additions instead of just doing one multiplication. This would be a lot better if you made a <code>Multiply</code> function to do long multiplication.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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