Answers for "how to convert base 10 to binary in java"

3

Java program to convert integer value into binary

import java.util.*;
import java.util.Scanner;
public class IntegerToBinary
{
   public static void main(String[] args)
   {
      int num;
      String str = "";
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter the a number : ");
      num = sc.nextInt();
      // convert int to binary java
      while(num > 0)
      {
         int y = num % 2;
         str = y + str;
         num = num / 2;
      }
      System.out.println("The binary conversion is : " + str);
      sc.close();
   }
}
Posted by: Guest on February-08-2021
0

Hexadecimal to Binary conversion with java

public static int hexadecimalToBinary(String hex) {
        // Declaring Hexadecimal variable
        String binary = "";

/**
 * Converting the Hexadecimal value to Binary Equivalent
 * First convert the Hexadecimal to Decimal Equivalent
 * Then to Binary Equivalent
 */
  
        // Converting Binary to decimal
        int number = Integer.parseInt( hex, 16 );
        // Converting Decimal Equivalent to Hexadecimal
        binary = Integer.toBinaryString( number );

        // Returning the Hexadecimal value
        return binary;
    }
Posted by: Guest on October-19-2021

Code answers related to "how to convert base 10 to binary in java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language