Note that there are some explanatory texts on larger screens.

plurals
  1. POConvert an number with arbitrary length from decimal to hex in c++
    primarykey
    data
    text
    <p>I've seen several examples for converting a number from decimal to hex (or base 10 to base 16), but I have a few restrictions for what I'm trying to do. I need to be able to convert a string with a decimal number into another string as a hex number. The number is potentially too big to fit inside of any primitive data types - ie: can't use ints, unsigned ints, doubles, etc...</p> <p>This should be able to perform the same calculations as listed on this page. <a href="http://www.kaagaard.dk/service/convert.htm" rel="nofollow">http://www.kaagaard.dk/service/convert.htm</a></p> <p>I've tried this, but it hasn't worked for me.</p> <p>The call to the function:</p> <pre><code>const int maxLen = 256; char destination[maxLen]; int retVal = convertBase(destination, maxLen, "123487032174829820348320429437483266812812"); </code></pre> <p>The function definition:</p> <pre><code>int convertBase(char* dest, int maxDestLength, const char* inputInBase10) { const char lookUpTable[] = { "0123456789abcdef" }; const std::string input = inputInBase10; const unsigned int inputSize = input.length(); std::string output; output.reserve(2 * inputSize); for(unsigned int i = 0; i &lt; inputSize; ++i) { const unsigned char c = input[i]; output.push_back(lookUpTable[c &gt;&gt; 4]); output.push_back(lookUpTable[c &amp; 15]); } if(output.length() &lt; maxDestLength) strcpy_s(dest, output.length(), output.c_str()); else strcpy_s(dest, maxDestLength, output.c_str()); cout &lt;&lt; dest &lt;&lt; endl; return strlen(dest); } </code></pre> <p>The expected hex number: "16ae5514d07e120126dfbcb3073fddb2b8c"</p> <p>The actual hex number generated: "313233343837303332313734383239383230333438333230343239343337343833323636383132383132"</p> <p>Also, I keep getting an error that the buffer is too small when passing back into a char* (repeated below)</p> <pre><code> if(output.length() &lt; maxDestLength) strcpy_s(dest, output.length(), output.c_str()); else strcpy_s(dest, maxDestLength, output.c_str()); </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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