Solved

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

Posted on 2009-04-10
5
322 Views
Last Modified: 2012-08-13
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 class ReadBin
{  
    public static void main(String[] args)
  {
    try
    {
      BufferedReader input = new BufferedReader(new FileReader("test.txt"));
      String line = input.readLine();  
      while (line != null)
      {
        //String line contains a number in binary format, now convert it  
        int value = Integer.parseInt(line, 2);

        System.out.println("Read value: " + value);
        // Read next line  
        line = input.readLine();
      }  
      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.

Thank you for your help,

John
0
Comment
Question by:lebouffonvert
  • 2
  • 2
5 Comments
 
LVL 13

Expert Comment

by:MicheleMarcon
ID: 24114907

String s="";

    try {

      is = new DataInputStream(new FileInputStream("1000bytes.txt"));

      while (true) { // exception deals catches EOF

        ch = is.readByte();

        s+=((char) ch);

      }

    } catch (EOFException eof) {

      System.out.println(" Finished!");

    }

Open in new window

0
 
LVL 92

Expert Comment

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

0
 

Author Comment

by:lebouffonvert
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

by:
objects earned 500 total points
ID: 24115120
use a BigInteger then

BigInteger bi = new BigInteger(line, 2);

0
 

Author Closing Comment

by:lebouffonvert
ID: 31568869
Thank you so much for your help.
It was really helpfull
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
copyEndy  challenge 15 58
triangle challenge 4 79
stringclean challenge 26 57
parentbit challenge 3 52
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

762 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now