Matthew Ellen .co .uk

Useful code snippets


//A function for converting unsigned integers into trinary numbers. Returns a string.
//max number of digits of trinary string for any 32 bit number is 21 but I'll use 32 as its a nice round number
static const int MAX_STRING_SIZE = 32;
static char stringBuffer[MAX_STRING_SIZE];
void myIntToTri(unsigned int input, string& result)
div_t divResult;
int numberDigits = 0;
divResult = div(input, BASE);
input = divResult.quot;
stringBuffer[numberDigits] = '0' + divResult.rem;

//result in working buffer is back to front so reverse into result string
//make sure result is big enough in one go.
for( int i = 0; i < numberDigits; ++i)
result[i] = stringBuffer[numberDigits-1-i];
else if (cur_fraction >= 1)
//By David Woo

Back to the top

Back to code snippets