Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class that define Base 3 number in .net Framework ( .net 4.0 )

Tags:

c#

I looking for some class that i can use to define number that is 3 base ( ternary number )

Is there something that i can use in .net framework or i need to write something ?

Thanks for any help.

like image 579
Yanshof Avatar asked Dec 09 '25 23:12

Yanshof


1 Answers

You can parse using Convert.ToInt32(s,base) and convert to string using Convert.ToString(i,base)

Or if your input consists of integers you can use something like this:

int CombineBase3(params int[] digits)
{
    int result=0;
    int multiplier;
    Debug.Assert(digits.Length<=20);//Floor(32*log(2)/log(3))
    for(int i=0;i<digits.Length;i++)
    {
      Debug.Assert(digits[i]>=0 && digits[i]<3);
      result+=multiplier*digits;
      multiplier*=3;
    }
    return result;
}

Or you can be lazy and just use a byte[] and save the digits in the array elements and forget all the integer encoding stuff.

like image 134
CodesInChaos Avatar answered Dec 11 '25 13:12

CodesInChaos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!