3

I'm trying to convert 12122 ( base 3) to int value ,

However I saw in reflector - the supported bases are 2,8,10,16

public static int ToInt32(string value, int fromBase)
{
    if (((fromBase != 2) && (fromBase != 8)) && ((fromBase != 10) && (fromBase != 0x10)))
    {
        throw new ArgumentException(Environment.GetResourceString("Arg_InvalidBase"));
    }
    return ParseNumbers.StringToInt(value, fromBase, 0x1000);
}

(I think they missed the 4 between 2-8 but nevermind.... )

So , how can I convert base 3 to base 10 ? ( Why didn't they give the option anyway?...)

Tilak
  • 30,108
  • 19
  • 83
  • 131
Royi Namir
  • 144,742
  • 138
  • 468
  • 792
  • 3
    Implement it yourself, it's not too hard – harold May 10 '12 at 14:01
  • @harold well it isnt. But I was hoping for an unknwon(by me) solution – Royi Namir May 10 '12 at 14:02
  • 2
    0x10==16 (hexadecimal numer), so the 4th _is_ there – Attila May 10 '12 at 14:02
  • 2
    "why didnt they give the option anyway ?" - well, do you really have data in base-3 ? And if so, why??? – H H May 10 '12 at 14:05
  • 1
    @HenkHolterman we have switches in our work . each can have 3 states .... this is where it statrted.... we are saving the int value... and need to go back....( really , why does it matter why I have it ? I just have it... ist started before i started working here :) ) – Royi Namir May 10 '12 at 14:07
  • "Switches with 3 states" -> use a string. And that _is_ an answer to why base-3 is not supported. – H H May 10 '12 at 14:10
  • 4
    @HenkHolterman again, the field of saving is int. ( historical issues...) , also consider *9999* in base 10 , in base 2 it will be "10011100001111" - so your solution saving as string is not efficient – Royi Namir May 10 '12 at 14:12
  • By the way, if you're going to convert it to an int, why does your question say you want to convert to base 10? – harold May 10 '12 at 14:14
  • @RoyiNamir - but do you expect the .NET library to cater for historical mistakes in your (and everybody else's) company? – H H May 10 '12 at 14:16
  • harold - currently we have 2 functions ( we wrote) - which does that. i was hoping for something ready for both directions. – Royi Namir May 10 '12 at 14:17
  • @HenkHolterman no . im not. there is nothing wrong of storing base 3 numbers - and so ive asked if there is a solution for a convertor ( i feel like explaining this thing for the fourth time) – Royi Namir May 10 '12 at 14:20
  • The missing bases are not really used. 2,8,10,16 are all defined in the computer world. So if you want to use Base 3, go ahead, but it is abnormal. – Security Hound May 10 '12 at 14:24
  • @Ramhound IMHO 10 doesnt belong to the computer world.... but really - lets say you have 10 boxes. each box can have 3 states. how wold you save the whole series state ? string ? no ! its too long ( see the 9999 example above) - so we save int value ( base 10) so that it will be shorter.... do you have other solution ? ( to my scenario !) – Royi Namir May 10 '12 at 14:25
  • _"consider 9999 in base 10 , in base 2 it will be "10011100001111"_ - that really has no bearing on this discussion, does it? – H H May 10 '12 at 14:52
  • @HenkHolterman base 2 will be longer ( i just made a simpler sample ) - if we talk about base 3 - so "9999"" will be "111201100" as a string - which occupies more than 4 byte - as in int it will occupy only 4 byte ( int). – Royi Namir May 10 '12 at 14:59

3 Answers3

4

From this link

public static string IntToString(int value, char[] baseChars)
{
    string result = string.Empty;
    int targetBase = baseChars.Length;

    do
    {
        result = baseChars[value % targetBase] + result;
        value = value / targetBase;
    } 
    while (value > 0);

    return result;
}

Use like following

    string binary = IntToString(42, new char[] { '0', '1' });

    string base3 = IntToString(42, new char[] { '0', '1', '2' });

    // convert to hexadecimal
    string hex = IntToString(42, 
        new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                     'A', 'B', 'C', 'D', 'E', 'F'});
Community
  • 1
  • 1
Tilak
  • 30,108
  • 19
  • 83
  • 131
0

This should work with bases between 1 and 9 and positive and in int range...

Also add some validation if necessary

int ConvertToInt(int number, int fromBase)
    {
        // Perform validations if necessary

        double result = 0;
        int digitIndex = 0;

        while (number > 0)
        {
            result += (number % 10) * Math.Pow(fromBase, digitIndex);

            digitIndex++;
            number /= 10;
        }

        return (int)result;
    }
Amir
  • 589
  • 1
  • 5
  • 18
0

There's nothing in .NET for that.

You could use this (untested)

static int ParseIntBase3(string s)
{
    int res = 0;
    for (int i = 0; i < s.Length; i++)
    {
        res = 3 * res + s[i] - '0';
    }
    return res;
}
harold
  • 61,398
  • 6
  • 86
  • 164