Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# Read the content of a text file then convert it its decimal equivalent

Posted on 2009-04-10
Medium Priority
341 Views
Good morning,
I am very new to java, I am trying to read a binary value from a text file to assign it to a variable in java, to then convert it to its decimal number equivalent.

With the code down below I can do this up to having thirty-one "1" and "0" in my text file:

import java.io.*;
{
public static void main(String[] args)
{
try
{
while (line != null)
{
//String line contains a number in binary format, now convert it
int value = Integer.parseInt(line, 2);

}
input.close();
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
}

I need the ability to convert up to one thousand binary bits not thirty one. with the code down below I can achieve that:
public class test2
{
public static void main(String args[])
{
String bin = "1111111111111111111111111";
double ctr=1, decimal=0;

for (double i = bin.length()-1; i>=0; i--)
{
if(bin.charAt((int) i)=='1')
decimal+=ctr;
ctr*=2;
}
System.out.println ("Decimal: "+decimal);
}
}

In the String bin = "1111111111111111111111111"; location I can put one thousand "1" and "0" and it gives me the right answer.

Could some help me put these together so I can read and convert to decimal a binary number of a length up to one thousand "1" and "0" from the text file.

John
0
Question by:lebouffonvert
[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
• 2
• 2

LVL 13

Expert Comment

ID: 24114907

``````String s="";
try {
is = new DataInputStream(new FileInputStream("1000bytes.txt"));
while (true) { // exception deals catches EOF
s+=((char) ch);
}
} catch (EOFException eof) {
System.out.println(" Finished!");
}
``````
0

LVL 92

Expert Comment

ID: 24114966
long value = Long.parseLong(line, 2);

0

Author Comment

ID: 24115045
Thank you for such a quick reply to both of you.

Michele Marcon,  if I may ask the example you gave me is for reading the file correct? because I can't see where it is manipulating the binary number to convert it to its decimal equivalent.

objects:
long value = Long.parseLong(line, 2);
I tried it and it works well up to 63 bytes but then it stop converting and gives me a read out of all binary numbers
0

LVL 92

Accepted Solution

objects earned 2000 total points
ID: 24115120
use a BigInteger then

BigInteger bi = new BigInteger(line, 2);

0

Author Closing Comment

ID: 31568869
Thank you so much for your help.
0

## Featured Post

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
###### Suggested Courses
Course of the Month10 days, 10 hours left to enroll