New type w base/radix 3. How To ??

  • Thread starter Thread starter Eigil Krogh
  • Start date Start date
E

Eigil Krogh

Hello

I'm new to C#.

I need a type with radix (or base) 3 (that is it counts 0 - 1 - 2 - 10 -
11 - 12 - 20 ...)

How do I construct that in C# ?


Thanks in advance
Eigil
 
I'm new to C#.

I need a type with radix (or base) 3 (that is it counts 0 - 1 - 2 - 10 -
11 - 12 - 20 ...)

How do I construct that in C# ?

Types don't really have bases - a number is just a number, until it's
converted to a string form. What you *could* do is write a static
method to convert any integer to a string in base 3. It could be
something like this:

using System;
using System.Text;

public class Test
{
static void Main()
{
Console.WriteLine (IntToCustomBase(12, 3));
}

static string IntToCustomBase(int value, int toBase)
{
// Insert error checking for toBase being invalid here.
// This method can only cope with toBase being in the
// range 2-10.

if (value==0)
{
return "0";
}
bool negate = value < 0;
if (negate)
{
// Fix the '-' character later; deal
// with it as a positive number for now.
value = -value;
}

StringBuilder digits = new StringBuilder();
while (value > 0)
{
digits.Append ((char)('0'+(value%toBase)));
value = value/toBase;
}
if (negate)
{
digits.Append('-');
}
// Now we've got the string the wrong way
// round in the StringBuilder, so reverse it:
StringBuilder ret = new StringBuilder(digits.Length);
for (int i=digits.Length-1; i >= 0; i--)
{
ret.Append(digits);
}
return ret.ToString();
}
}
 
Thank you very much!

(I must have sleept for a while. Of course all integral numbers are stored
as binaries, base/radix 2.)

I'll use your IntToCustomBase(..).

BTW: Why is it called "base". Is it not normal notation to call it "radix"
rather than "base" ? In C# "base" can mean other things to, e.g. the class
from which a class has inherited if I understood it right.

Eigil

Jon Skeet said:
I'm new to C#.

I need a type with radix (or base) 3 (that is it counts 0 - 1 - 2 - 10 -
11 - 12 - 20 ...)

How do I construct that in C# ?

Types don't really have bases - a number is just a number, until it's
converted to a string form. What you *could* do is write a static
method to convert any integer to a string in base 3. It could be
something like this:

using System;
using System.Text;

public class Test
{
static void Main()
{
Console.WriteLine (IntToCustomBase(12, 3));
}

static string IntToCustomBase(int value, int toBase)
{
// Insert error checking for toBase being invalid here.
// This method can only cope with toBase being in the
// range 2-10.

if (value==0)
{
return "0";
}
bool negate = value < 0;
if (negate)
{
// Fix the '-' character later; deal
// with it as a positive number for now.
value = -value;
}

StringBuilder digits = new StringBuilder();
while (value > 0)
{
digits.Append ((char)('0'+(value%toBase)));
value = value/toBase;
}
if (negate)
{
digits.Append('-');
}
// Now we've got the string the wrong way
// round in the StringBuilder, so reverse it:
StringBuilder ret = new StringBuilder(digits.Length);
for (int i=digits.Length-1; i >= 0; i--)
{
ret.Append(digits);
}
return ret.ToString();
}
}
 
Thank you very much!

(I must have sleept for a while. Of course all integral numbers are stored
as binaries, base/radix 2.)

Yes - everything, sooner or later, must end up as binary in a computer
- but most of the time it doesn't actually help to think of it that
way.
I'll use your IntToCustomBase(..).

BTW: Why is it called "base". Is it not normal notation to call it "radix"
rather than "base" ? In C# "base" can mean other things to, e.g. the class
from which a class has inherited if I understood it right.

"Base" is what I knew it as from school - I think it's a more common
term than "radix". I agree that it's somewhat overloaded in this
context...
 
Back
Top