Solved

Binary String Display

Posted on 2001-06-21
4
208 Views
Last Modified: 2010-03-31
I would like to create a Binary String Formatter?

Example:

If i feed it a 2...I would like it to display

00000010

OR

0010

OR

10

Depending on what I want the bit display length to be. I want the final string to be saved in a variable that I can pass to other classes for various displays??



Any suggestions?
0
Comment
Question by:Vernstump
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 

Expert Comment

by:johnbrewer1980
ID: 6216207
how about the following function:
void printBinary(int number, int displayLength) {
   for (int i = displayLength - 1; i >= 0; --i) {
      System.out.print((number >> i) & 1);
   }    
}
0
 
LVL 2

Expert Comment

by:iDeb
ID: 6216208
take your class and override the toString method and put a for loop where you divide your number by 2 and depending upon whether there is a remainder you add a 1 or 0 to a string.
0
 
LVL 2

Expert Comment

by:venkat2000120699
ID: 6216222
Here is a traditional approach to the problem.

public class BinaryFormatter {
    public static void main(String args[]) {
        int a[] = new int[32];
        int n = 31;
        int maximumNumberOfBitsToDisplay = 4;
       
        int number = 10;
       
System.out.println("The bit pattern for " + number + " is:");
       
        while(number > 0) {
            int bit = number % 2;
            a[n--] = bit;
            number = (int)(number / 2);
        }
       
       
        for (int k=a.length - maximumNumberOfBitsToDisplay; k < a.length; k++) {
                 System.out.print(a[k]);
        }
     
    }//end of main()
}//end of class
0
 
LVL 1

Accepted Solution

by:
eattrig earned 50 total points
ID: 6216298
There is a static method in java.lang.Integer called toBinaryString(int).  It returns a String representation of the number (in decimal) which you pass in.  However, there is no parameter where the client can specify how much 0 padding to place on the end.  But you could write the following:

public static String getBinary(int num)
{
    String numStr = Integer.toBinaryString(num);
    // if you wanted a total of 8 digits in the String
    int addZeros = 8 - numStr.length();
    for (int x = 0; x < addZeros; x++)
        numStr = '0' + numStr;
    return numStr;
}
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question