Results 1 to 3 of 3
  1. #1
    Wizard0860 is offline Member
    Join Date
    Nov 2012
    Posts
    29
    Rep Power
    0

    Default Converting Binary

    So I have to make an extension to a program where when the convert button is pressed, the binary will change into a number from 1-255.

    So far, I have the following for this specific part. No idea how to convert.

    Java Code:
    public Display10()
          {
             setLayout(new GridLayout(1, 8));
             bits = new JLabel[8];
             for(int x = 0; x < bits.length; x++)
             {
                bits[x] = new JLabel("", SwingConstants.CENTER);
                bits[x].setFont(new Font("Serif", Font.BOLD, 50));
                add(bits[x]);
             }
             randomize();
          }
    The following is the convert method which I have done so far.

    Java Code:
    public void convert()
          {
             String[] converter=new String[bits.length];
             for(int i=0;i<bits.length;i++)
                converter[i] = bits[i].getText();
          }

  2. #2
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    354
    Rep Power
    5

    Default Re: Converting Binary

    Is your randomize() method responsible for setting the text in each of your JLabels in the bits[] to a "0" or "1"?

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,382
    Rep Power
    5

    Default Re: Converting Binary

    I assume you are talking about converting from a binary string to a decimal value. Well, you could use the following from the JDK.

    Java Code:
    String bitString = "10101";
    int val = Integer.parseInt(bitString, 2);
    Or do it yourself.
    Lets assume this was decimal.

    int n = 0;
    String nStr = "1234";
    1. Remove the left most digit and add its representative value into n
    2. if no digits left, you are done
    3. Multiply n by 10
    4. go to 1.

    Regards,
    Jim
    The Java Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Converting Chars to 8 -bit binary.
    By akeni in forum New To Java
    Replies: 1
    Last Post: 10-27-2011, 02:02 PM
  2. Converting binary to decimal using recursion
    By _inase in forum New To Java
    Replies: 4
    Last Post: 10-14-2011, 02:31 AM
  3. converting binary to string
    By stegano in forum New To Java
    Replies: 5
    Last Post: 03-27-2011, 07:20 PM
  4. converting to binary
    By telltera in forum New To Java
    Replies: 5
    Last Post: 09-02-2010, 02:35 PM
  5. Converting mp3 files to binary
    By kylefrank in forum Advanced Java
    Replies: 4
    Last Post: 06-09-2010, 08:25 PM

Posting Permissions

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