Tuesday, April 30, 2013

How to Convert from binary to hex in java




Simple One line code to convert String containing a binary value to Hex


String bin = Integer.toHexString(Integer.parseInt(binOutput, 2));


Full Code for conversion


import java.io.*;
import java.lang.*;



public class  BinaryToHexadecimal{
  public static void main(String[] args)throws IOException{
  BufferedReader bf= new BufferedReader(new InputStreamReader(System.in));
  System.out.println("Enter the Binary number:");
  String hex = bf.readLine();
  long num = Long.parseLong(hex);
  long rem;
  while(num > 0){
  rem = num % 10;
  num = num / 10;
  if(rem != 0 && rem != 1){
  System.out.println("This is not a binary number.");
  System.out.println("Please try once again.");
  System.exit(0);
  }
  }
  int i= Integer.parseInt(hex,2);
  String hexString = Integer.toHexString(i);
  System.out.println("Hexa decimal: " + hexString);
  }
}

run:
Enter the Binary number:
101010
Hexa decimal: 2a

----------------------------------------------
java - Translating a String containing a binary value to Hex
java - efficiently converting hex to binary
Binary to Hexadecimal Conversion w/o using predefined functions
Convert from binary to hex in java?
Convert binary to hex java programming?
Java program to convert binary,octal,decimal to ...
Write a program in java to convert binary numbers ...
HOW TO CONVERT BINARY TO DECIMAL ...
Convert Hexadecimal To Binary Using Java 
How to convert Hexadecimal to Decimal, Binary and Octal in Java

Converting binary to hex, any tips on how to do this?


String containing a binary value to Hex
IT Certification Category (English)640x480

Partner Sites

VideoToGifs.com

EasyOnlineConverter.com

SqliteTutorials.com


Top Online Courses From ProgrammingKnowledge

Python Course http://bit.ly/2vsuMaS
Java Coursehttp://bit.ly/2GEfQMf
Bash Coursehttp://bit.ly/2DBVF0C
Linux Coursehttp://bit.ly/2IXuil0
C Course http://bit.ly/2GQCiD1
C++ Coursehttp://bit.ly/2V4oEVJ
PHP Coursehttp://bit.ly/2XP71WH
Android Coursehttp://bit.ly/2UHih5H
C# Coursehttp://bit.ly/2Vr7HEl
JavaFx Coursehttp://bit.ly/2XMvZWA
NodeJs Coursehttp://bit.ly/2GPg7gA
Jenkins Course http://bit.ly/2Wd4l4W
Scala Coursehttp://bit.ly/2PysyA4
Bootstrap Coursehttp://bit.ly/2DFQ2yC
MongoDB Coursehttp://bit.ly/2LaCJfP
QT C++ GUI Coursehttp://bit.ly/2vwqHSZ