0

I already tried:

Convert.ToString(123, 3);

but that gives me a System.ArgumentException: Invalid Base

Is there another way to do this?

Paul
  • 119
  • 4
  • 7

1 Answers1

0

You can use this standard algorithm:

public static String ToTrenary(int value) {
  if (value == 0)
    return "";

  StringBuilder Sb = new StringBuilder();
  Boolean signed = false;

  if (value < 0) {
    signed = true; 
    value = -value;
  }

  while (value > 0) {
    Sb.Insert(0, value % 3);
    value /= 3;
  }

  if (signed)
    Sb.Insert(0, '-');

  return Sb.ToString();
}
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215