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

Showing posts with label JAVA NUMBER CONVERSION. Show all posts
Showing posts with label JAVA NUMBER CONVERSION. Show all posts

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:

Sunday, 8 October 2017

How to Convert Binary to Decimal in Java

Binary to Decimal Conversion in Java

java programs to convert binary to decimal

In this article, We will see how to convert binary to decimal in java with the help of simple examples. Binary to decimal conversion in java is the most asked program in java written interviews.

In the last articles we saw java programs to covert decimal to binary number and decimal number to hexadecimal and hexadecimal to decimal conversion.

Let's start with java program to convert binary to decimal.

As we know, Binary number is a number which is represented in the form of 0 and 1. In other words you can say, true/false or on/off.

Decimal number is number which starts from 0 to 9 with the base 10.

There are two ways to convert binary number to decimal number in java.

  • By using Integer.parseInt() method of Integer class.
  • By creating your own logic without using any predefined method. 

Let's start with first method which is using Integer.parseInt() method to converting binary to decimal in java.



(1) Binary to decimal conversion program in java by using Integer.parseInt() method.

This is the first example of binary to decimal conversion by using Integer.parseInt() method.

class Example1
{
public static void main(String args[])
{
//declaring string containing binary number
String str = "101101";//binary number 
int decimalNo = Integer.parseInt(str, 2);
System.out.println("Decimal no is: "+decimalNo);
}
}

Output: Decimal no is: 45

When we will convert binary number which is 101101 into decimal number it will produce 45.


(2) Binary to decimal conversion program in java by using Integer.parseInt() method but with Scanner class.

This is another example of Integer.parseInt() method but with Scanner class because here we are going to take input from the user from console and then convert binary to decimal number.

import java.util.*;
class Example2
{
public static void main(String args[])
{
//declaring Scanner class
Scanner sc = new Scanner(System.in);
System.out.println("Enter your binary number: ");
String str = sc.nextLine();
System.out.println("Decimal number is: "+Integer.parseInt(str,2));
}
}

Output: Enter your binary number
             1100
             Decimal number is: 12

Now moving on second method which is create your own logic for converting binary to decimal conversion program in java.


(3) Binary to decimal conversion program in java without using any predefined method.

In this example we are not going to use any predefined method just like Integer.parseInt() of Integer wrapper class but here we will create own logic for conversion of binary to decimal.

import java.util.Scanner;
class Example3
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter your binary number: ");
int binaryno = sc.nextInt();
int binary = binaryno;
int decimal = 0;
int power = 0;
while(true)
{
if(binary == 0)
{
break;
}
else
{
int temp = binary%10;
decimal += temp*Math.pow(2,power);
binary = binary/10;
power++;
}
}
System.out.println("Decimal number is: "+decimal);
}
}

Output: Enter your binary number:
             110
             Decimal number is: 6

Now above all the examples of binary to decimal conversion programs in java are quite useful for both fresher and experienced for core java interviews. 

Share:

Wednesday, 27 September 2017

How to Convert Decimal to Hexadecimal in Java


In the last post we saw how to convert decimal number into binary number in java but here we will learn how to convert decimal to hexadecimal in java.
How to convert decimal to hexadecimal in java


Let's start java program to convert decimal to hexadecimal.

Convert Decimal to hexadecimal in java

We can convert decimal number to hexadecimal number in java by two ways and these are given below.
  • By using toHexString() method of Integer class which is wrapper class.
  • By writing your own logic, without using any predefined method.

(1) Let's write a java program to convert a decimal number to hexadecimal number using "toHexString()" method of Integer class.

class DecimalToHexadecimal
{
public static void main(String args[])
{
int i = 32;

String str = Integer.toHexString(i);
System.out.println("After conversion hexadecimal number is: "+str);
}
}

Output: After conversion hexadecimal number is 20

                                                       Or

This is the same example as above but here we will use Scanner class so that we can take input from the users.

import java.util.*;
class Simple
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
System.out.println("Enter a decimal number: ");
int number = input.nextInt();
//using toHexString() method of Integer class
String str = Integer.toHexString(number);
System.out.println("Hexadecimal number is: "+str);
}
}

Output: Enter a decimal number:
             45
             Hexadecimal number is: 2d

(2) Program to convert decimal to hexadecimal in java without using any ready made method. Here we will put own logic for conversion.

import java.util.Scanner.*;
class Simple
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
//taking input from the user
System.out.println("Enter decimal number: ");
int number = input.nextInt();
int rem;//for storing remainder
String str = "";//for storing result
char hexa[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
while(number>0)
{
rem = number%16;

str = hexa[rem]+str;
number = number/16;
}
System.out.println("Hexadecimal Number is: "+str);
}
}

Output: Enter decimal number:
             45
             Hexadecimal Number is: 2D


How to convert Hexadecimal to decimal number in java?

This is the simple example where we are going to convert hexadecimal number to Decimal number in java. In this example we will use parseInt() method of Integer wrapper class.

class HexaToDecimal
{
public static void main(String args[])
{
//String containing hexadecimal number
String hexanumber = "20";
//we will use parsInt() method of Integer wrapper class
int decimalnumber = Integer.parseInt(hexanumber, 16);//pass 16
System.out.println("hexadecimal number will be convert into decimal");
System.out.println("Decimal number is: "+decimalnumber);
}
}

Output: hexadecimal number will be convert into decimal
             Decimal number is: 32

Above all the examples are quite simple and easy and it is very useful examples. There are many java number conversion interview questions are asked in java or core java interviews e.g.... 

how to convert decimal to binary number with example
how to convert decimal to hexadecimal number with example.
how to convert decimal to octal number in java with example.
how to convert binary to decimal with example.
how to convert binary to hexadecimal. 
how to convert binary to octal with example.
how to convert hexadecimal to binary, decimal, octal.

and many more...but above we saw simple examples of how to convert decimal to hexadecimal and conversion of hexadecimal to decimal number in java.
Share:

Thursday, 21 September 2017

How to Convert Decimal to Binary in Java

Decimal to Binary Conversion in Java

Java Decimal to Binary Conversion

Here we will learn, How to convert decimal to binary in java with easy examples. Java decimal to binary conversion is the most important core java interview question.

There are many number conversion interview questions which is asked in java interviews like java program to convert decimal to hexadecimal, java program to convert decimal to octal, java program to convert binary to decimal, etc. But here we learn only decimal to binary conversion program in java. Let's start...

First, Let us understand what is decimal and binary number.

Decimal Numbers - Decimal number is the real number using the base 10. There are only 10 digits which represents numbers and starting from 0 to 9.

Binary Numbers - Binary number is number which contains only two digits 0 and 1 which know as bits.

Binary numbers in computer language i. e 0 and 1 represents true/false or on/off.

There are 3 ways, we can convert decimal to binary in java programs.

  • First is, Using toBinaryString() method of Integer class.
  • Second is, Using you own logic without any predefined method.
  • Third is, Using Stack. 


1) Decimal to Binary Example Using toBinaryString() method

class DecimalBinary
{
public static void main(String args[])
{
System.out.println("Binary representation of 1: ");
System.out.println(Integer.toBinaryString(1));
System.out.println("Binary representation of 6: ");
System.out.println(Integer.toBinaryString(6));
System.out.println("Binary representation of 12: ");
System.out.println(Integer.toBinaryString(12));
System.out.println("Binary representation of 45: ");
System.out.println(Integer.toBinaryString(45));
}
}

Output: Binary representation of 1:
              1
             Binary representation of 6:
             110
             Binary representation of 12:
             1100
             Binary representation of 45:
             101101


2) Decimal to Binary Example and count the number of 1s in binary numbers

In this program, we will take the input from user and convert it decimal to binary number and count 1s.

import java.util.Scanner;
class Counts
{
public static void main(String args[])
{
int n, a, count = 0;
String s = "";
Scanner sc = new Scanner(System.in);
System.out.println("Enter decimal number ");
n = sc.nextInt();
while(n>0)
{
a = n%2;
if(a == 1)
{
count++;
}
s = s+" "+a;
n = n/2;
}
System.out.println("Binary number: "+s);
System.out.println("No. of 1s: "+count);
}
}

Output: Enter decimal number
             45
             Binary number: 101101
             No of 1s: 4


3) Write a java program to convert decimal to binary without using predefined method

class Test
{
public void binaryConvert(int num)
{
int bin[] = new int[45];
int index = 0;
while(num > 0)
{
bin[index++] = num%2;
num = num/2;
}
for(int i = index-1; i >= 0; i--)
{
System.out.print(bin[i]);
}
}
public static void main(String args[])
{
Test t = new Test();
System.out.println("Binary representation of 123: ");
t.binaryConvert(123);
}
}

Output: Binary representation of 123:
             1111011


4) Another Example of java decimal to binary conversion

In this program, we create stack object and take input from the user for converting decimal to binary.

import java.util.*;
class Demo
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
//creating Stack object
Stack<Integer> stack = new Stack<Integer>();
System.out.println("Enter the decimal number: ");
int num = sc.nextInt();
while(num != 0)
{
int b = num % 2;
stack.push(b);
num /= 2;
}
System.out.print("\n Binary is: ");
while(!(stack.isEmpty()))
{
System.out.print(stack.pop());
}
System.out.println();
}
}

Output: Enter the decimal number: 
             65
             Binary is: 1000001

Read More:

How to Convert Binary to Decimal in Java.
How to convert Decimal to Hexadecimal in Java.
How to Convert Binary to Hexadecimal in Java.


Above all the examples of decimal to binary conversion programs in java are mostly asked in java interviews.

Share:

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Link

Translate