Results 1 to 6 of 6
  1. #1
    mephisto772 is offline Member
    Join Date
    Feb 2009
    Posts
    3
    Rep Power
    0

    Red face Convert decimal to binary..pls help..newbie here

    Problem: Write a program that will accept a positive integer number with a range of 8 to 256. The program should convert it to binary number. Display the result.

    output:
    Enter Number: 8
    Binary Equivalent: 1000

    Thanks!

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    try it first, then we'll help.

  3. #3
    wolfcro is offline Member
    Join Date
    Feb 2009
    Location
    Italy
    Posts
    51
    Rep Power
    0

    Default

    if you know the math algorithm then it should be easy ;)
    but if you have problems with your code post it

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Quote Originally Posted by mephisto772 View Post
    Problem: Write a program that will accept a positive integer number with a range of 8 to 256. The program should convert it to binary number. Display the result.

    output:
    Enter Number: 8
    Binary Equivalent: 1000

    Thanks!
    Take a look at the Integer class and it's toBinaryString() method. ;-)

    Edit: Although I'm sure, as this is homework, you are meant to do it on your own.

    In any case, give it a try, and as already suggested, post your code if you have problems, we are not, however, going to do it for you, just so you know.
    Last edited by masijade; 02-10-2009 at 09:43 AM.

  5. #5
    mephisto772 is offline Member
    Join Date
    Feb 2009
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by masijade View Post
    Take a look at the Integer class and it's toBinaryString() method. ;-)

    Edit: Although I'm sure, as this is homework, you are meant to do it on your own.

    In any case, give it a try, and as already suggested, post your code if you have problems, we are not, however, going to do it for you, just so you know.
    i've used binarystring but not really the requirement...:

    import java.io.*;

    public class DecimalToBinary{
    public static void main(String args[]) throws IOException{
    BufferedReader num = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter a decimal value (8 to 256):");
    String x = num.readLine();
    int i = Integer.parseInt(x);
    String bin = Integer.toBinaryString(i);
    System.out.println("Binary Equivalent: " + bin);

    }
    }

    it should be the long cut method using functions like this:

    function x [args]
    args = args.toString();
    if (args.length == 0)
    return false;

    for (var i = 0;i<args.length;i++)
    {
    if (args.substring(i,i+1) < "0" || args.substring(i, i+1) > "9")
    {
    return false;
    }
    }

    return true;
    }


    {
    res1 = 999;
    args = arg;
    while(args>1)
    {
    arg1 = parseInt(args/2);
    arg2 = args%2;
    args = arg1;
    if(res1 == 999)
    {
    res1 = arg2.toString();
    }
    else
    {
    res1 = arg2.toString()+res1.toString();
    }
    }
    if(args == 1 && res1 != 999)
    {
    res1 = args.toString()+res1.toString();
    }
    else if(args == 0 && res1 == 999)
    {
    res1 = 0;
    }
    else if(res1 == 999)
    {
    res1 = 1;
    }
    var ll = res1.length;
    while(ll%4 != 0)
    {
    res1 = "0"+res1;
    ll = res1.length;
    }
    return res1;
    }


    {
    var sd = name.value;
    if(isNum(sd))
    {
    var result = deciToBin(sd);
    document.first.deciBin.value = result;
    }
    else
    {
    document.first.deci.value = sd.substring(0,sd.length-1) ;
    }
    }

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Please repost your code (from your editor not by copying what you have already posted, as that has already had the formatting ruined) using code tags so the formatting is preserved. That, as it is, is only a pain to attempt to read, and can easily lead to misunderstood code, and, ipso facto, advice.

Similar Threads

  1. Binary IO help
    By kosok in forum New To Java
    Replies: 2
    Last Post: 12-25-2008, 07:58 AM
  2. convert a java binary/executable to a intermediate language
    By sumit_400461 in forum Advanced Java
    Replies: 1
    Last Post: 07-13-2008, 07:04 PM
  3. get binary value
    By rushenas in forum New To Java
    Replies: 2
    Last Post: 06-14-2008, 01:51 PM
  4. newbie needs help...
    By vicky08 in forum New To Java
    Replies: 2
    Last Post: 03-31-2008, 04:26 PM
  5. Use recursion to convert binary to...
    By coco in forum Advanced Java
    Replies: 1
    Last Post: 08-07-2007, 07:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •