4

I'm in this Object Oriented class, but I just don't know how to do this. I know basic fundamentals but nothing like this. I am asked to create a program that converts #'s base 10 to base 2, base 8,and base 16. Then, after we are asked to convert it back from 2,8,16 to base 10. I have gathered some information from other websites and I actually ended up editing a bit of it. Please help! I would prefer it if you guys actually helped and edited yourselves and sent it to me, but if that is too much to ask, please try to guide me through it as I do not know much of Java. So far I have:

import java.util.Scanner;

public class baseconverterr
{

public static void main(String[] args) {

      // Read the conversion choice from the user

      System.out.println("Choose 1 or 2 or 3:");

      System.out.println("1: conversion from base 10 to base 2 ");

      System.out.println("2: conversion from base 10 to base 8");

      System.out.println("3: conversion from base 10 to base 16");
      // do you want 1, 2 , or 3? you have your choice
      Scanner in = new Scanner(System.in);

      int choice = in.nextInt();

      String string = in.nextLine();

      // Read in the number to be converted and do the conversion

      String output= "";

      System.out.println("Please enter the number to be converted:");

      int input = in.nextInt();

      if (choice == 1)
      // if the user chooses choice #1, it will convert from base 10 to base 2
          output = Integer.toString(input, 2);

      else if (choice == 2)

          output = Integer.toString(input, 8);

    // if the user chooses choice #2, it will convert from base 10 to base of 8 
     else if (choice == 3)

          output = Integer.toString(input, 16);
   // if the user chooses choice #3, it will convert from base 10 to base 16
  else

      System.out.println("invalid entry");
      // everything else, it is invalid
      System.out.println("final output=" + output);
      // this prints the final output.
Nathan
  • 95
  • 9
Justin Park
  • 99
  • 1
  • 1
  • 4
  • There's a nice trick that working right to left, the remainder of dividing the number by the new base will be the next digit, with the original dividing by the new base every time. It's pretty handy when you don't want to write a new function for each new base. – chris Apr 15 '13 at 06:13

2 Answers2

32

for a decimal x (base 10) you can use respectively for binary, octal, hex conversion

Integer.toString(x, 2),

Integer.toString(x, 8)

Integer.toString(x, 16).

then to convert it back to decimal, respectively from binary, octal, hex conversion

Integer.valueOf(binary_value, 2)

Integer.valueOf(octal_value, 8)

Integer.valueOf(hex_value, 16)

in your code, change the following:

output = Integer.toString(input, 16) //replace 16 for hex, 8 for octal, 2 for binary 
Ankit
  • 6,554
  • 6
  • 49
  • 71
0

use this

    if (choice == 1)

          output = Integer.toString(input, 2);

      else if (choice == 2)

          output = Integer.toString(input, 8);

    // if the user chooses choice #2, it will convert from base 10 to base of 8 
     else if (choice == 3)

          output = Integer.toString(input, 16);

  else

      System.out.println("invalid entry");
NPKR
  • 5,368
  • 4
  • 31
  • 48
  • where do i put this? in which class or method? update: i got it! it doesn't really work though... – Justin Park Apr 15 '13 at 06:18
  • remove your code after System.out.println("Please enter the number to be converted:"); int input = in.nextInt(); and add given code – NPKR Apr 15 '13 at 06:19
  • now, the i is "wrong" what do i do to stop the error? – Justin Park Apr 15 '13 at 06:20
  • check with latest code – NPKR Apr 15 '13 at 06:21
  • I figured it out. I tried to run the program, but after I select a number, i.e.: 1, which is the base 10 to base 2 converter. but when i put in the number to be converted, it just tells me it's invalid. (by my code) not an error – Justin Park Apr 15 '13 at 06:24
  • And so he has his first taste of the development world. Yep - it's time to google that up. It's time to google that other thing. DYWMTGTFY (Stands for "Do you want me to google that for you?". The meaning is that, in reality, you should probably google it up yourself first. – Rodney P. Barbati Nov 04 '16 at 00:49