Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-22-2011, 04:07 PM   PM User | #1
zverys36
New to the CF scene

 
Join Date: May 2011
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
zverys36 is an unknown quantity at this point
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?
zverys36 is offline   Reply With Quote
Old 05-22-2011, 04:50 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,741
Thanks: 4
Thanked 2,465 Times in 2,434 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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?
Fou-Lu is offline   Reply With Quote
Old 05-22-2011, 06:38 PM   PM User | #3
zverys36
New to the CF scene

 
Join Date: May 2011
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
zverys36 is an unknown quantity at this point
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 
zverys36 is offline   Reply With Quote
Old 05-23-2011, 05:33 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,741
Thanks: 4
Thanked 2,465 Times in 2,434 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 05-24-2011, 04:38 AM   PM User | #5
zverys36
New to the CF scene

 
Join Date: May 2011
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
zverys36 is an unknown quantity at this point
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!
zverys36 is offline   Reply With Quote
Reply

Bookmarks

Tags
binary, integer, java, tobinarystring

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:38 PM.


Advertisement
Log in to turn off these ads.