-3

I am very new to MS VS12 and I'm working a class project that requires me to convert a Base 10 number to Bases 2, 3, 8, and 16. It also asks me to put the conversion in a method and then call those methods using buttons and I am not sure how to get started. Can anyone help me?!

Here is what I have so far (taken some of this from Quickest way to convert a base 10 number to any base in .NET?)

    public static string IntToBinary(int value, char[] basechars)
    {
        string binary = IntToBinary(16, new char[] { '0', '1' });

        string hex = IntToBinary(16, new char[] { '0', '1', '2', '3', '4', '5', '6',
          '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'});

        string result = string.Empty;
        int conversion = basechars.Length;

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

        return result;
      }

I have 5 buttons. A generic 'Convert' that takes a number from a textbox and then a base number (2-16) from another textbox and once the 'Convert' button is clicked it convert the number to the base number that was specified. The other buttons are: Base2, Base3, Base8, Base16. I have to write methods for each of those conversions and call them from their specified buttons

I have to call the methods that pertains to the buttons.

Happy
  • 3
  • 3
  • What is your specific question? – Mike Precup Aug 08 '14 at 23:21
  • First off am I on the right path? Secondly, how do call this method in the button click event? I am sorry if I'm not phrasing this properly. I'm very new to this program. – Happy Aug 08 '14 at 23:23
  • I have 5 buttons. A generic 'Convert' that takes a number from a textbox and then a base number (2-16) from another textbox and once the 'Convert' button is clicked it convert the number to the base number that was specified. The other buttons are: Base2, Base3, Base8, Base16. I have to write methods for each of those conversions and call them from their specified buttons. – Happy Aug 08 '14 at 23:27
  • This is very close to an existing question, there are many answers here. http://stackoverflow.com/questions/923771/quickest-way-to-convert-a-base-10-number-to-any-base-in-net – jtimperley Aug 08 '14 at 23:32
  • I know, but my problem is that I'm not sure on how to get started. I referred to that question in my post. I'm not sure to call the right method for the buttons. – Happy Aug 08 '14 at 23:33
  • As asked this post is exact duplicate. You may be looking for some other advice - please specify it clearly in new question. I.e. "how to add button that calls function with given argument". Note that "I copied some code please make it work for my assignment" is rarely good question for SO, please make sure your post does not look like such request. – Alexei Levenkov Aug 09 '14 at 00:52
  • @AlexeiLevenkov Thank you for the advice. This is my first time posting to StackOverFlow. I do understand this is a duplicate of that question and I apologized for that. I have adjusted my question. – Happy Aug 09 '14 at 00:59

1 Answers1

1

You have two calls to the method itself inside the method, the first would throw it into a loop that would end with a StackOverflowException. Just remove those calls from the method, and perhaps rename it to something that makes more sense as it doesn't only convert to binary:

public static string IntToBase(int value, char[] basechars)
{
    string result = string.Empty;
    int conversion = basechars.Length;

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

    return result;
}

Now you have something that works.

I assume that you have the number to be converted in a text box, I will call that txtNumber. In the handler for the button that should convert the number to binary, you would parse the text from the text box into a number, then call the method to get it as binary:

int num = Int32.Parse(txtNumber.Text);
string binary = IntToBase(num, new char[] { '0', '1' });

The call to the method is the same as the code that I removed from the method, except it uses the variable num instead of 16.


There is actually methods in the framework to do the conversion to different bases (except for base 3), but that is probably not what you are supposed to learn in your course. Anyway, that would be:

public static string IntToBase(int value, int base)
{
  return Convert.ToString(value, base);
}
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • 1
    Please note that Convert.ToString(value, base) only works for base 2,8,10 or 16, so it wont work for the OP as he need base 3 as well – Jose Cherian Aug 08 '14 at 23:55
  • @Manu: Good point, I added a note about that. It's just a side track to the answer anyway. – Guffa Aug 09 '14 at 00:03
  • Okay so that worked converting Base10 to Binary. The answer returned '1100100' It should have returned '01100100' I'll just have to add the beginning zero. – Happy Aug 09 '14 at 00:10
  • I see it keeps getting down voted and I know this has been answered and not explained very well, but I was not sure on how to begin the code. I do appreciate the help with this. I have been struggling with this course all semester and needed some expert help. – Happy Aug 09 '14 at 00:14
  • So I would basically do this same thing when converting Base 10 to Hex, right? Something like this: public static string IntToHex(int value, char[] basechars) { string hex = IntToHex(16, new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}); with the code from the previous in play here? } – Happy Aug 09 '14 at 00:15
  • What about Base 3? I tried searching 'Google' for that and I didn't return any good explanations. – Happy Aug 09 '14 at 00:22
  • @Happy: If you want a specific length for the result, then you should add another parameter for that, and use that to determine the length of the loop instead of looping until the number is zero. If you make a wrapper function named `IntToHex` it would call `IntToBase`, not itself. For base 3 you would just use `string base3 = IntToBase(num, new char[] { '0', '1', '2' });`. – Guffa Aug 09 '14 at 09:43
  • @Guffa Thank you for that! I have all the buttons working like they should be, but now how would I go about putting them all into one method and using a switch statement for each base? Then for the buttons I should be able to call that particular switch statement. – Happy Aug 09 '14 at 16:33
  • @Happy: You don't need a switch, just call the method with an array containing the characters for the base. – Guffa Aug 10 '14 at 09:02
  • So basically just how I have it now? string base3 = IntToBase(num, new char[] { '0', '1', '2' }); – Happy Aug 10 '14 at 13:17
  • @Happy: Yes, that works fine. If you want to improve it you can send a string instead of a char array to the method. To do that you just change the data type of the parameter, the rest of the method is the same, then call it using a string: `string base3 = IntToBase(num, "012");`. – Guffa Aug 10 '14 at 13:38
  • Thank you for that! I like the string parameter better, it makes it looks nicer. I am trying to add and IF statement preferably to the INTToBase method that says if(bases < 2 || bases > 16 { throw error message ("Please enter a base between 2 and 16") } Also trying to figure out how to convert any other bases that are not 2, 3, 8, and 16. Using the same conversion method. I think an IF statement would work, but I am not sure how to code that. – Happy Aug 10 '14 at 15:47
  • @Happy: That would be `if (bases.Length < 2 || bases.Length > 16 { throw new ArgumentException("Please enter a base between 2 and 16"); }`. To convert any other bases you just provide a string with the digits for that base, for example `"01234"` for base 5. – Guffa Aug 10 '14 at 16:48
  • @Guffa Right that would be ideal and that works, but I am trying to minimize the strings because I have to do something different with Base numbers like: 5,6,7,8,9,10,11,12,13,14,15 – Happy Aug 10 '14 at 17:01
  • I am trying to work on a switch statement that would call the Main Bases 2, 3, 8, and 16, but for all other bases they would be classified under default and would also be prefixed with the Base + "x" + then the conversion – Happy Aug 10 '14 at 17:07