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

Thread: decimal to binary problem

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default decimal to binary problem

    my code:


     
     
    public class d2b {
     
    	private int decnum, modnum, quotnum, count = 0, tot, binarray[] = new int[16], convnum, alter = 1;
     
    	d2b(int decpass){                       //decpass is passed from any other class or main as an integer.
    		decnum = decpass;
    	}
     
    	public void convert(){
     
     
    		while(decnum > 1){
     
    			modnum = decnum % 2;
    			quotnum = decnum / 2;
     
    			decnum = quotnum;
     
    			binarray[count] = modnum;
     
    			count++;
     
    		}
     
    		binarray[count] = decnum;
     
     
    		while(count >= 0){
     
    			if(alter == 1){
     
    				convnum = 1;
    				alter++;
     
    			}
    			else{
     
    				if(binarray[count] == 1){
    					convnum = (convnum * 10) + 1;
    				}
    				else{
    					convnum = convnum * 10;
    				}
     
    			}
     
     
     
     
    			count--;
     
    		}
     
    		System.out.printf("%d", convnum);
     
    	}
     
    }

    What it tries to do:

    I wrote this program as my assignment. I came up with the logic(finally *phew*) for a program to convert decimal to binary and store it as an integer. I tried the algorithm without the arrays but couldn't solve it at all(maybe i am dumb ), so i used arrays and successfully was able to complete this task within minutes. however, now i can only convert the values upto certain numbers....ex, if i pass 999, it will convert the value as 1111100111 but if i pass 9999 it will show -468765865. i have a hunch, that, its because i have to do it as 'long int' data type, am i right? if not where am i going wrong? also when i derived the algo for it i saw that it wouldn't need more than 15 array size or am i wrong there too?

    Please any input would be helpful. Thank you.
    Last edited by mick.bhattarai; December 26th, 2010 at 01:53 PM.


  2. #2
    Member
    Join Date
    Dec 2010
    Posts
    46
    Thanks
    0
    Thanked 10 Times in 10 Posts

    Default Re: decimal to binary problem

    why only size of 16 in binarray[] = new int[16] ??

  3. #3
    Junior Member
    Join Date
    Dec 2010
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: decimal to binary problem

    thank you for your reply,

    i assigned only 16 array size because for now i just want to check the conversion till 4 digit decimal (i.e. 9999) which should ultimately produces a 15 digit binary(but that is not happening as you can read from my above post) so i gave the array size a 16.

  4. #4
    Member
    Join Date
    Dec 2010
    Posts
    46
    Thanks
    0
    Thanked 10 Times in 10 Posts

    Default Re: decimal to binary problem

    i don't know what you doing with the 2nd whle loop, but i can say it unnecessary since you are already doing the divide by 2 method.
    public class d2b {
        private int decnum, modnum, quotnum, count = 0, tot, binarray[] = new int[16], convnum, alter = 1;
        d2b(int decpass){                       //decpass is passed from any other class or main as an integer.
            decnum = decpass;
        }
        public void convert(){
            while(decnum > 1){
                decnum = decnum / 2;
                binarray[count] = decnum %  2;
                count++;
            }
            for(int i= binarray.length-1 ; i>=0; i--){
                System.out.printf("%d", binarray[i] );
            }
            System.out.printf("0\n");
        }
        public static void main(String[] args){
            d2b num = new d2b(9999);
            num.convert();
        }
    }

  5. The Following User Says Thank You to JavaHater For This Useful Post:

    mick.bhattarai (December 26th, 2010)

  6. #5
    Junior Member
    Join Date
    Dec 2010
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: decimal to binary problem

    You do realize that i am trying to store the binary as an integer(even though it seems to be the wrong way to solve this problem), don't you? so the "System.out." method(which seems more like the way this problem should be handled) is just for printing while, i am trying to store it into an integer variable. However i could've used for loop instead i choose while loop so it doesn't matter, i used while because of its ease and control i can get. But the first if() after the while loop was needed(for me, because i dont know any other way) to rectify the problem with the first array(from the last) value being used as it will always be 1, ex 100. when the modulus way is being done binarray[0] = 0, binarray[1] = 0, binarray[2] = 1. So as you can see binarray[2] or any other last binarray will always have value 1 so this created a problem for me. As for the second if()-else, its to now combine those single 0/1 digits into a number. Again i was doing this method to store binary into an integer, if i just wanted to print it , i could have(without even using array). None the less, thank you for your input. As for the problem, it still insists.



    EDIT: found the solution.

    The solution is to use long instead of int.
    Last edited by mick.bhattarai; December 26th, 2010 at 07:08 PM. Reason: found the solution.

  7. #6
    Member
    Join Date
    Dec 2010
    Posts
    46
    Thanks
    0
    Thanked 10 Times in 10 Posts

    Default Re: decimal to binary problem

    try setting the size of your binarry dynamically. for example binarray = new int[ decnum / 2 ] , instead of hard coding 16 as the size.

  8. #7
    Junior Member
    Join Date
    Dec 2010
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: decimal to binary problem

    i dont think initializing binarray dynamically is going to help me with this program, specially [decnum / 2] because the array size is pretty much the number of integers a converted binary will have but thank you for your input. However problem is solved.
    Last edited by mick.bhattarai; December 26th, 2010 at 08:12 PM.

Similar Threads

  1. Converting Hex to Decimal
    By r2ro_serolf in forum Java Theory & Questions
    Replies: 10
    Last Post: September 4th, 2011, 04:29 PM
  2. Left and Right rotation in a balanced binary tree problem
    By szuwi in forum Algorithms & Recursion
    Replies: 2
    Last Post: December 22nd, 2010, 07:20 PM
  3. Problem with binary tree
    By Exoskeletor in forum Object Oriented Programming
    Replies: 2
    Last Post: January 8th, 2010, 01:03 PM
  4. decimal and hexdecimal
    By ran830421 in forum Java Theory & Questions
    Replies: 3
    Last Post: November 21st, 2009, 02:58 AM
  5. decimal to hex
    By rsala004 in forum Algorithms & Recursion
    Replies: 1
    Last Post: November 3rd, 2009, 02:16 AM