Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Wednesday 20 December 2017

How To Convert Binary to Hexadecimal in Java

Binary To Hexadecimal Conversion

binary to hexadecimal conversion in java

In the previous posts we have discussed some conversion programs like java program to convert decimal to binary, java program to convert binary to decimal, etc. Here we are going to discuss another conversion program which is, how to convert binary to hexadecimal in java.

Let's write a java program to convert a binary number to hexadecimal number with easy examples.


Binary to Hexadecimal Conversion in Java

You can write a program to convert binary to hexadecimal by using 2 ways which is given below.
  • By using predefined method
  • By creating your own logic

Let's see first example of binary to hexadecimal conversion in java by using predefined method.

Java Binary to Hexadecimal Example 1

This is first binary to hexadecimal example where we will take binary number from the user by using Scanner class and then convert this binary number into hexadecimal number by using predefined method.

import java.util.Scanner;
class BinaryToHexadecimal
{
int number;
Scanner s;

void takeBinaryValue()
{
s = new Scanner(System.in);
System.out.println("Enter the binary number");

number = Integer.parseInt(s.nextLine(), 2);
}

void conversion()
{

String hexa = Integer.toHexString(number);
System.out.println("Hexadecimal value is : "+hexa);
}

public static void main(String args[])
{
BinaryToHexadecimal bh = new BinaryToHexadecimal();

bh.takeBinaryValue();
bh.conversion();
}
}

Output: Enter the binary number
             101010
             Hexadecimal value is : 2a

Now we are going to take another example to convert binary to hexadecimal by creating own logic.


Java Binary to Hexadecimal Example 2

This is another simple program to convert binary to hexadecimal in java by using self logic.

import java.util.Scanner;
class BinaryToHexadecimal2
{
public static void main(String args[])
{

int bnum;
int rem;
String hexanum = "";

char hex[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
Scanner sc = new Scanner(System.in);

System.out.println("Enter binary number");
bnum = sc.nextInt();



//logic for converting binary to hexadicmal number
while(bnum > 0)
{
rem = bnum%16;
hexanum = hex[rem] + hexanum;
bnum = bnum/16;
}

System.out.println("hexadecimal value is : "+hexanum);
}
}

Output: Enter binary number
             1010
             hexadecimal value is : 3F2


Here we learned binary to hexadecimal conversion in java with simple examples.

Share:

4 comments:

  1. Your blog is very useful and all the java examples is very well explained.

    ReplyDelete
  2. I believe your second example is converting from decimal to hexadecimal instead of binary to hexadecimal

    ReplyDelete
  3. Thanks for sharing this informative blog java training in chennai

    ReplyDelete

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate