Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: To binary translation

1. ## 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?

• 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?

• Originally Posted by Fou-Lu
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

• Originally Posted by zverys36
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.

• Originally Posted by Fou-Lu
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!

•

#### Posting Permissions

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