Yanshof
Yanshof

Reputation: 9926

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

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.

Upvotes: 3

Views: 781

Answers (1)

CodesInChaos
CodesInChaos

Reputation: 108790

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.

Upvotes: 1

Related Questions