Tag: java conversion program

  • Java Program to Convert Binary to Hexadecimal

    In this tutorial, we will write a Java Program to Convert a Binary Number to Hexadecimal Number. Before that, you must have knowledge of the following topics in java.

    Binary number

    The binary numbers are based on 0 and 1, so it is a base 2 number. They are the combination of 0 and 1. For example, 1001, 110101, etc.

    Hexadecimal number

    The hexadecimal number is represented with a base of 16. It has digits from 0 to 15 to represent, However after 9 the values are represented in Alphabet till 15 such as 10 is represented as A, 11 as B, 12 as C, 13 as D, 14 as E, and 15 as F.

    Input: 11011111
    Output: DF

    Input: 10001101
    Output: 8D

    Now let us go through a program for Binary to hexadecimal conversion in java.


    Java Program to Convert Binary to Hexadecimal using While loop

    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String[] args)
      {
        int[] hexaDecnum = new int[1000];
        int i = 1, j = 0, rem, decNum = 0, binary;
    
        Scanner in = new Scanner(System.in);
    
        System.out.print("Enter a Binary Number: ");
        binary = in .nextInt();
    
        while (binary > 0)
        {
          rem = binary % 2;
          decNum = decNum + rem * i;
          i = i * 2;
          binary /= 10;
        }
    
        i = 0;
    
        while (decNum != 0)
        {
          hexaDecnum[i] = decNum % 16;
          decNum = decNum / 16;
          i++;
        }
    
        System.out.print("Equivalent Hexadecimal value: ");
        for (j = i - 1; j >= 0; j--)
        {
          if (hexaDecnum[j] > 9)
            System.out.print((char)(hexaDecnum[j] + 55) + "\n");
          else
            System.out.print(hexaDecnum[j] + "\n");
        }
      }
    }

    Output:

    Enter a Binary Number: 1010
    Equivalent Hexadecimal value: A


  • Java Program to Convert Hexadecimal to Binary

    In this tutorial, we will write a Java Program to Convert a Hexadecimal Number to Binary Number. Before that, you must have knowledge of the following topics in java.

    Binary number

    The binary numbers are based on 0 and 1, so it is a base 2 number. They are the combination of 0 and 1. For example, 1001, 110101, etc.

    Hexadecimal number

    The hexadecimal number is represented with a base of 16. It has digits from 0 to 15 to represent, However after 9 the values are represented in Alphabet till 15 such as 10 is represented as A, 11 as B, 12 as C, 13 as D, 14 as E, and 15 as F.

    Now let us go through a program for hexadecimal to binary conversion in java.


    Java Program to Convert Hexadecimal to Binary using While loop

    import java.util.Scanner;
    
    public class Main
    {
      public static int conversionFunc(String hexa)
      {
        String digits = "0123456789ABCDEF";
        hexa = hexa.toUpperCase();
        int result = 0;
        for (int i = 0; i < hexa.length(); i++)
        {
          char c = hexa.charAt(i);
          int d = digits.indexOf(c);
          result = 16 *result + d;
        }
    
        return result;
      }
    
      public static void main(String args[])
      {
        String hexDecNum;
        int decnum, i = 1, j;
        int binnum[] = new int[100];
        Scanner scan = new Scanner(System.in);
    
        System.out.print("Enter Hexadecimal Number: ");
        hexDecNum = scan.nextLine();
    
        //hexadecimal to decimal
        decnum = conversionFunc(hexDecNum);
    
        //decimal to binary
        while (decnum != 0)
        {
          binnum[i++] = decnum % 2;
          decnum = decnum / 2;
        }
    
        System.out.print("Equivalent Binary Value: ");
        for (j = i - 1; j > 0; j--)
        {
          System.out.print(binnum[j]);
        }
      }
    }

    Output:

    Enter Hexadecimal Number: AD
    Equivalent Binary Value: 10101101


  • Java Program to Convert Hexadecimal to Decimal

    This is a Java Program to Convert a Hexadecimal Number to its decimal Equivalent. Before that, you must have knowledge of the following topics in Java.

    Hexadecimal number

    The hexadecimal number is represented with a base of 16. It has digits from 0 to 15 to represent, However after 9 the values are represented in Alphabet till 15 such as 10 is represented as A, 11 as B, 12 as C, 13 as D, 14 as E, and 15 as F.

    Decimal Number

    These are the numbers with a base of 10, which ranges from 0 to 9. These numbers are formed with the combination of 0 to 9 digits such as 24, 345, etc.

    Now let us go through a program for Hexadecimal to decimal conversion in java.


    Java Program to Convert Hexadecimal to Decimal using for loop

    import java.util.Scanner;
    
    public class Main
    {
      public static int conversionFunc(String hexa)
      {
        String digits = "0123456789ABCDEF";
        hexa = hexa.toUpperCase();
        int val = 0;
    
        for (int i = 0; i < hexa.length(); i++)
        {
          char c = hexa.charAt(i);
          int d = digits.indexOf(c);
          val = 16 *val + d;
        }
    
        return val;
      }
    
      public static void main(String args[])
      {
        String hexadeci;
        int decimal;
        Scanner scan = new Scanner(System.in);
    
        System.out.print("Enter a Hexadecimal Number: ");
        hexadeci = scan.nextLine();
    
        decimal = conversionFunc(hexadeci);
    
        System.out.print("Equivalent Decimal Value: " + decimal);
      }
    }

    Output:

    Enter a Hexadecimal Number: 7D
    Equivalent Decimal Value: 125


  • Java Program to Convert Decimal to Hexadecimal

    This is a Java Program to Convert a Decimal Number to its Hexadecimal Equivalent. Before that, you must have knowledge of the following topics in Java.

    Hexadecimal number

    The hexadecimal number is represented with a base of 16. It has digits from 0 to 15 to represent, However after 9 the values are represented in Alphabet till 15 such as 10 is represented as A, 11 as B, 12 as C, 13 as D, 14 as E, and 15 as F.

    Decimal Number

    These are the numbers with a base of 10, which ranges from 0 to 9. These numbers are formed with the combination of 0 to 9 digits such as 24, 345, etc.

    Now let us go through a program for decimal to hexadecimal conversion in java.


    Java Program to Convert Decimal to Hexadecimal using while loop

    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String args[])
      {
        int decimal, rem;
        String hexdeci = "";
    
        char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
    
        Scanner scan = new Scanner(System.in);
    
        System.out.print("Enter a Decimal Number: ");
        decimal = scan.nextInt();
    
        while (decimal > 0)
        {
          rem = decimal % 16;
          hexdeci = hex[rem] + hexdeci;
          decimal /= 16;
        }
    
        System.out.print("Equivalent Hexadecimal Value: " + hexdeci);
      }
    }

    Output:

    Enter a Decimal Number: 125
    Equivalent Hexadecimal Value: 7D


  • Java Program to Convert Decimal to Hexadecimal using Recursion

    In this tutorial, we will write a Java Program for the conversion of decimal to hexadecimal using recursion. Before that, you must have knowledge of the following topic in Java.

    Hexadecimal number

    The hexadecimal number is represented with a base of 16. It has digits from 0 to 15 to represent, However after 9 the values are represented in Alphabet till 15 such as 10 is represented as A, 11 as B, 12 as C, 13 as D, 14 as E, and 15 as F.

    Decimal Number

    These are the numbers with a base of 10, which ranges from 0 to 9. These numbers are formed with the combination of 0 to 9 digits such as 24, 345, etc.

    Now let us go through a program for decimal to hexadecimal conversion in java using recursion.


    Java Program to Convert Decimal to Hexadecimal using Recursion

    import java.util.Scanner;
    
    public class Main
    {
      char[] charHexa = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
      int num;
      String hexaDeci = "";
    
      public static void main(String[] args)
      {
        Main obj = new Main();
        int decimal;
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter decimal number: ");
        decimal = sc.nextInt();
    
        String hex = obj.hexadecimal(decimal);
        System.out.println("Equivalent Hexadecimal Value: " + hex);
    
      }
    
      String hexadecimal(int deci)
      {
        if (deci != 0)
        {
          num = deci % 16;
          hexaDeci = charHexa[num] + hexaDeci;
          deci /= 16;
          hexadecimal(deci);   //recursion call
        }
    
        return hexaDeci;
      }
    }

    Output:

    decimal to hexadecimal in java using recursion

  • Java Program to Convert Octal Number to Decimal Number

    In this tutorial, we will write a program to convert octal to decimal in java. Before that, you must have knowledge of the following topics in C++.

    Octal number

    The octal numbers are the numbers with base 8 and use the digits 0 to 7. Example: 8 in decimal is represented as 10 in octal, 25 as 31, and so on.

    Decimal Number

    These are the numbers with a base of 10, which ranges from 0 to 9. These numbers are formed with the combination of 0 to 9 digits such as 24, 345, etc.

    Conversion example:

    Input: 23
    Output: 19

    Let us go through a program for the octal to decimal Conversion in java.


    Java Program to Convert Octal to Decimal using while loop

    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String[] args)
      {
        int octalNum, decimalNumber = 0, i = 0;
        Scanner scan = new Scanner(System.in);
    
        System.out.print("Enter an Octal Number: ");
        octalNum = scan.nextInt();
    
        while (octalNum != 0)
        {
          decimalNumber += (octalNum % 10) *Math.pow(8, i);
          ++i;
          octalNum /= 10;
        }
    
        System.out.printf("Equivalent Decimal Number: %d", decimalNumber);
      }
    }

    Output:

    Enter an Octal Number: 23
    Equivalent Decimal Number: 19


  • Java Program to Convert Decimal Number to Octal Number

    In this tutorial, we will write a program to convert decimal to octal in java. Before that, you must have knowledge of the following topics in C++.

    Octal number

    The octal numbers are the numbers with base 8 and use the digits 0 to 7. Example: 8 in decimal is represented as 10 in octal, 25 as 31, and so on.

    Decimal Number

    These are the numbers with a base of 10, which ranges from 0 to 9. These numbers are formed with the combination of 0 to 9 digits such as 24, 345, etc.

    Conversion example:

    Input: 19
    Output: 23

    Let us go through a program for the Decimal to Octal Conversion in java.


    Java Program to Convert Decimal to Octal using while loop

    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String[] args)
      {
        int decNum, octalNumber = 0, i = 1;
        Scanner scan = new Scanner(System.in);
    
        System.out.print("Enter a Decimal Number: ");
        decNum = scan.nextInt();
    
        while (decNum != 0)
        {
          octalNumber += (decNum % 8) *i;
          decNum /= 8;
          i *= 10;
        }
    
        System.out.printf("Equivalent Octal value: %d", octalNumber);
      }
    }

    Output:

    Enter a Decimal Number: 19
    Equivalent Octal value: 23


  • Java Program to Convert Binary to Octal

    In this tutorial, we will write a java program to convert a binary number into octal using while loop. Before that, you must have knowledge of the following topics in C++.

    Binary number

    The binary numbers are based on 0 and 1, so it is a base 2 number. They are the combination of 0 and 1. For example, 1001, 110101, etc.

    Octal number

    The octal numbers are the numbers with base 8 and use the digits 0 to 7. Example: 8 in decimal is represented as 10 in octal, 25 as 31, and so on.

    Let us go through a program for the Binary to Octal Conversion in java.


    Java Program to Convert Binary to Octal using while loop

    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String args[])
      {
        int bin, rem, i = 0;
        int octalNum = 0, deciNum = 0;
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter a Binary Number: ");
        bin = sc.nextInt();
    
        while (bin != 0)
        {
          deciNum += (bin % 10) *Math.pow(2, i);
          ++i;
          bin /= 10;
        }
    
        i = 1;
    
        while (deciNum != 0)
        {
          octalNum += (deciNum % 8) *i;
          deciNum /= 8;
          i *= 10;
        }
        
        System.out.print("Equivalent Octal Value: " + octalNum);
        
      }
    }

    Output:

    Enter a Binary Number: 1010
    Equivalent Octal Value: 12


  • Java Program to Convert Octal to Binary Number

    In this tutorial, we will write a java program to convert a octal number into binary using while loop. Before that, you must have knowledge of the following topics in C++.

    Binary number

    The binary numbers are based on 0 and 1, so it is a base 2 number. They are the combination of 0 and 1. For example, 1001, 110101, etc.

    Octal number

    The octal numbers are the numbers with base 8 and use the digits 0 to 7. Example: 8 in decimal is represented as 10 in octal, 25 as 31, and so on.

    Let us go through a program for the Ocatal to Binary Conversion in java.


    Java Program to Convert Octal number to Binary Number

    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String args[])
      {
        int octalNum, rem, i = 0;
        int bin = 0, deciNum = 0;
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter a Octal Number: ");
        octalNum = sc.nextInt();
    
        while (octalNum != 0)
        {
          deciNum += (octalNum % 10) *Math.pow(8, i);
          ++i;
          octalNum /= 10;
        }
    
        i = 1;
    
        while (deciNum != 0)
        {
          bin += (deciNum % 2) *i;
          deciNum = deciNum / 2;
          i *= 10;
        }
    
        System.out.print("Equivalent Binary Value: " + bin);
    
      }
    }

    Output:

    Enter a Octal Number: 12
    Equivalent Binary Value: 1010


  • Java Program to Convert Binary to Decimal

    In this tutorial, we will write a java program to convert binary to decimal using while loop. Before that, you must have knowledge of the following topics in java.

    Binary number

    The binary numbers are based on 0 and 1, so it is a base 2 number. They are the combination of 0 and 1. For example, 1001, 110101, etc.

    Decimal Number

    These are the numbers with a base of 10, which ranges from 0 to 9. These numbers are formed by the combination of 0 to 9 digits such as 24, 345, etc.

    Here is the chart where for the equivalent value of decimal and binary numbers.

    Decimal to binary

    Example:

    Input: 101
    Output: 5

    Input: 111
    Output: 7

    Now let us go through a program for the conversion of binary to decimal in java using while loop.


    Before using a while loop, let us find the conversion of binary number to decimal number by using the parseInt() method of the Integer class.

    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String args[])
      {
        String bin;
        Scanner sc = new Scanner(System.in);
    
        System.out.println("Enter a binary number: ");
        bin = sc.nextLine();
    
        System.out.println("Equivalent Decimal Value: " + Integer.parseInt(bin, 2));
      }
    }

    Enter a binary number:
    1001
    Equivalent Decimal Value: 9


    Java Program to Convert Binary to Decimal using while loop

    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String args[])
      {
        int bin, decimal = 0, i = 0;
        Scanner sc = new Scanner(System.in);
    
        System.out.println("Enter a binary number: ");
        bin = sc.nextInt();
    
        while (bin != 0)
        {
          decimal += ((bin % 10) *Math.pow(2, i));
          bin /= 10;
          i++;
        }
    
        System.out.println("Equivalent Decimal Value: " + decimal);
      }
    }

    Output:

    Enter a binary number:
    1010
    Equivalent Decimal Value: 10