Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: base conversion

  1. #1
    Junior Member
    Join Date
    Jul 2021
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default base conversion

    hi there,

    i'm currently writing a program to convert numbers between bases and i'm stuck in a spot. anyway, i've just truncated to the part where i'm stuck at and to simply code, values are preset here. In short, this code is suppose to convert the number 71 in base 8 to a decimal number (via Math.pow method). I don't know why it's not working as I have printed out values in intermediate steps to verify. To avoid losing values, i went on and chose instantiate product as a double (it was originally an int). The run gives product = 49.0 and 440 during the loop when it should be 1.0 and 56.0. I appreciate any feedback. Sorry about the spacing, I don't know why the editor removed the indentions.

    UPDATE: found solutions, moderator please delete thread

    import java.util.Scanner;
     
    public class Main {
     
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
            System.out.println("Do you want to convert /from decimal or /to decimal? (To quit type /exit)");
            String reply = scan.nextLine();
            while (!reply.equals("/exit")) {
                if (reply.equals("/from")) {
                    System.out.println("Enter number in decimal system: ");
     
                    int decimal = scan.nextInt();
                    int remainder;
                    StringBuilder converted = new StringBuilder();
     
                    System.out.println("Enter target base: ");
                    int base = scan.nextInt();
     
                    if (base == 2 || base == 8) {
                        while (decimal > 0) {
                            remainder = decimal % base;
                            converted.insert(0, remainder);
                            decimal = decimal / base;
                        }
                    } else {
                        char[] hex = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
                        while (decimal > 0) {
                            remainder = decimal % base;
                            converted.insert(0, hex[remainder]);
                            decimal = decimal / base;
                        }
                    }
                    System.out.println("Conversion result: " + converted);
                }
                else if (reply.equals("/to")) {
                    System.out.println("Enter source number: ");
                    String sourceNumber = scan.next();
                    System.out.println("Enter source base: ");
                    int base = scan.nextInt();
                    int converted = 0;
                    converted = Integer.parseInt(sourceNumber,base);
     
                    System.out.println("Conversion to decimal result: " + (int) converted);
                }
                System.out.println("Do you want to convert /from decimal or /to decimal? (To quit type /exit)");
                reply = scan.next();
            }
        }
    }
    Last edited by learningjava2021; July 13th, 2021 at 09:12 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: base conversion

    Can you post the steps the code should take to convert a number from on base to another?

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    learningjava2021 (July 13th, 2021)

  4. #3
    Junior Member
    Join Date
    Jul 2021
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: base conversion

    I found my answers:
    solution 1: char value conversion using operation char - (char>57?55:48)
    solution 2: use integer.parseint(number, base)

    in the future i will use
    **YOUR CODE GOES HERE**

    thank you moderator Norm, please delete thread

Similar Threads

  1. Replies: 4
    Last Post: July 18th, 2014, 02:04 AM
  2. Replies: 1
    Last Post: July 16th, 2014, 04:16 AM
  3. Base Conversion
    By Pingu in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 22nd, 2011, 03:15 PM
  4. BASE CONVERSION
    By bgwilf in forum Algorithms & Recursion
    Replies: 6
    Last Post: November 13th, 2010, 12:02 PM

Tags for this Thread