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 4 of 4

Thread: Base Changing - Is there an easier method?

  1. #1
    Member Staticity's Avatar
    Join Date
    Jul 2011
    Location
    Texas
    Posts
    105
    My Mood
    Inspired
    Thanks
    3
    Thanked 5 Times in 5 Posts

    Default Base Changing - Is there an easier method?

    So, as my first attempt to make a method that changes a base 10 number into another Base.. I came up with this:
    	public StringBuffer toBaseSixteen(long x){ 
    		boolean solving = true;
    		String s = "";
    		long remain = 0;
    		int i = 16;
    		String [] list = {"A", "B", "C", "D", "E", "F"};
     
    		while(solving){
    			remain = x % i;
    			if(remain > 9)
    				s += list[(int) (remain-10)];
     
    			if(remain < 9)
    			s += remain + "";
     
    			x /= i;
    			new StringBuffer(s).reverse();
    			if(x<1){
    				solving = false;
    			}
    		}
    		return new StringBuffer(s).reverse();
    	}

    Has anyone come up with a faster method? If so, could you point me in your direction of thought.. Or maybe even comment with your method?
    Simplicity calls for Complexity. Think about it.


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Base Changing - Is there an easier method?

    Have you seen the methods in java.lang.Integer that do this? Integer.parseInt(String, int) and Integer.toString(int, int)

  3. #3
    Member Staticity's Avatar
    Join Date
    Jul 2011
    Location
    Texas
    Posts
    105
    My Mood
    Inspired
    Thanks
    3
    Thanked 5 Times in 5 Posts

    Default Re: Base Changing - Is there an easier method?

    Yes, I have! I just prefer to create these methods for practice, rather than use Java's library.
    Simplicity calls for Complexity. Think about it.

  4. #4
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Base Changing - Is there an easier method?

    'toBaseSixteen' has a trivial solution because you're not converting 'a decimal number', but an integer that's represented by binary bits. Base 2 to Base 2^N can be done by taking the N least significant bits, converting them to a base-N digit, and shifting the original number right N places.

    If you want to make your own generic base-changing code, you should use the template from Integer and implement parseInt(String, int) and toString(int, int), so that you can 'eat your own dog food' (convert an integer to a String, convert that String back to the original integer) to test that it works.

Similar Threads

  1. Issue with Base Changing..
    By Staticity in forum What's Wrong With My Code?
    Replies: 6
    Last Post: September 5th, 2011, 01:01 PM
  2. Beginner Question Here (Trying to make things easier)
    By beer-in-box in forum AWT / Java Swing
    Replies: 2
    Last Post: June 1st, 2011, 11:48 AM
  3. How much easier is Java compared to C++
    By Psychotron in forum Java Theory & Questions
    Replies: 6
    Last Post: May 24th, 2011, 01:15 PM
  4. data base
    By abcdmmkk in forum Member Introductions
    Replies: 1
    Last Post: March 23rd, 2011, 09:57 AM
  5. BASE CONVERSION
    By bgwilf in forum Algorithms & Recursion
    Replies: 6
    Last Post: November 13th, 2010, 12:02 PM