Convert Text to Binary

In this section, we will learn how to convert Text to Binary. The following program provides you the functionality to convert Text to Binary

Tutorials   
How to convert String Date to Timestamp in Java? Best Java Tutorial for Beginners Read user input in Java using Scanner Java Programming Examples Read file line by line in Java 8 What are the benefits of learning Java Programming? A brief history and future of Java programming language Importance of Java for Mobile Application Development How To Become A Good Software Developer? Learn Java Programming for beginners 5 Reasons Java is Still a Great Language to Boost Your Career Prospect as a Programmer How to learn Java with no programming experience? How to learn Java? What are Classes and Interfaces in Java? Converting a String to Integer in Java How to check if a string exists in a string in JavaScript? Installing JDK 8 on Windows 10 and configuring the settings Video Tutorial of reading file in Java 9 Invincible Reasons to learn Java New to Java programming Java Programming Tips, Articles and Notes How to download JDK 1.6? Encapsulation in Java Programming How to format number in Java? Generics Example Program in Java Java 9 Features Java 9 Tutorial, news and examples How to read content of JPEG file in Java? Image Processing Tutorials in Java Programming Language Introduction to Java Technology Java 8 Consumer Interface with forEach Loop Introduction to Generics in Java Java 8 consumer class(interface) example Oracle Launches Project Valhalla for Java How to read properties file in Java? How to add JDK 8 support in Eclipse? Java 8: Java 8 is officially released and it can be downloaded How to Invoke method using Reflection API of Java? First Lambda Expressions in Java Java 8 expected release date

Ads

Ads

Convert Text to Binary

     

In this section, we will learn how to convert Text to Binary. The following program provides you the functionality to convert Text to Binary.

Code Description:

In the program code below, to take the input from the keyboard we have used BufferReader() method in which we have passed an object of it which is InputStreamReader() as must be knowing. Then we have defined a variable of type int which will convert the input to integer line by line. Now we have used a loop here to check the number we well input. It will take maximum of 24 bytes as we have declared maxBytes=3. Hence it will convert the number into bytes one by one. 

Here is the code of the program:

import java.io.*;

public class TexttoBinary {
  private static final int maxBytes = 3;
  public static void main(String[] args) {
  
  BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  do {
  try {
  System.out.print("Type the number to parse: ");
  int number = Integer.parseInt(in.readLine());
  int Bit;
  String result = "";
  for (int i = maxBytes*8; i >= 0; i--) {
  Bit = << i;
  if (number >= Bit) {
  result += 1;
  number -= Bit;
  }
  else {
  result += 0;
  }
  }
  System.out.println(result);
  }
  catch (NumberFormatException e) {
  System.exit(0);
  }
  catch (IOException e) {
  e.printStackTrace();
  System.exit(1);
  }
  }
  while (true);
  }
}

Output of the program:

C:\unique>javac TexttoBinary.java

C:\unique>java TexttoBinary
Type the number to parse: 417893
0000001100110000001100101
Type the number to parse: 3289
0000000000000110011011001
Type the number to parse:

Advertisements

Ads
Share on Google+Share on Google+

Convert Text to Binary

Posted on: April 17, 2011 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: Convert Text to Binary  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads