Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    May 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy To binary translation

    How can I translate multiple random numbers into binary? I know that to translate one number to binary, this declaration is needed: String myString = Integer.toBinaryString(a number), but what if the number is random?

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    I don't understand your question. Integer.toBinaryString will work on any integer provided for it; it doesn't matter if you type in a Integer.toBinaryString(5); or an Integer.toBinaryString(myInt).
    Can you be more specific on the question?

  • #3
    New to the CF scene
    Join Date
    May 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    I don't understand your question. Integer.toBinaryString will work on any integer provided for it; it doesn't matter if you type in a Integer.toBinaryString(5); or an Integer.toBinaryString(myInt).
    Can you be more specific on the question?
    OK, so I have to read a few numbers using Java. The numbers are not hard-coded - they depend on whichever text file I choose to read. I need to take those numbers that I read and translate them into binary. For example, if a text file contains the numbers:

    PHP Code:
    2
    a string

    I want the numbers to be read and translated into binary (skipping the string).

    PHP Code:
    10
    a string
    101 

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Quote Originally Posted by zverys36 View Post
    OK, so I have to read a few numbers using Java. The numbers are not hard-coded - they depend on whichever text file I choose to read. I need to take those numbers that I read and translate them into binary. For example, if a text file contains the numbers:

    PHP Code:
    2
    a string

    I want the numbers to be read and translated into binary (skipping the string).

    PHP Code:
    10
    a string
    101 
    Gotcha.
    The easiest way would be to read the data in as a string, parse it as an integer and display it. Using a try/catch would suit for this.

    PHP Code:
    BufferedReader br;
    try
    {
        
    br  = new BufferedReader(new Filereader("yourfile.txt"));
        
    String sLine null;
        while (
    null != (sLine br.readLine()))
        {
            
    String sDisplay "";
            try
            {
                
    sDisplay Integer.toBinaryString(Integer.parseInt(sLine).intValue());
            }
            catch (
    NumberFormatException ex)
            {
                
    sDisplay sLine;
            }
            
    System.out.println("Line: " sDisplay);
        }
    }
    catch (
    IOException ex)
    {
        
    System.out.println("Failed to open file.");
    }
    finally
    {
        try
        {
            
    br.close();
        }
        catch (
    Exception ex)
        {
        }

    Untested, but something like that should work.

  • #5
    New to the CF scene
    Join Date
    May 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile

    Quote Originally Posted by Fou-Lu View Post
    Gotcha.
    The easiest way would be to read the data in as a string, parse it as an integer and display it. Using a try/catch would suit for this.

    PHP Code:
    BufferedReader br;
    try
    {
        
    br  = new BufferedReader(new Filereader("yourfile.txt"));
        
    String sLine null;
        while (
    null != (sLine br.readLine()))
        {
            
    String sDisplay "";
            try
            {
                
    sDisplay Integer.toBinaryString(Integer.parseInt(sLine).intValue());
            }
            catch (
    NumberFormatException ex)
            {
                
    sDisplay sLine;
            }
            
    System.out.println("Line: " sDisplay);
        }
    }
    catch (
    IOException ex)
    {
        
    System.out.println("Failed to open file.");
    }
    finally
    {
        try
        {
            
    br.close();
        }
        catch (
    Exception ex)
        {
        }

    Untested, but something like that should work.
    Thanks, it worked!


  •  

    Tags for this Thread

    Posting Permissions

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