Answers for "java binary to hexadecimal"

1

binary to hexadecimal in java

import java.util.Scanner;
public class BinaryToHexadecimalJava
{
   public static void main(String[] args)
   {
      int[] hexaDecimal = new int[1000];
      int a = 1, b = 0, r, decimal = 0, binary;
      Scanner sc = new Scanner(System.in);
      System.out.print("Please enter binary number: ");
      binary = sc.nextInt();
      while(binary > 0)
      {
         r = binary % 2;
         decimal = decimal + r * a;
         a = a * 2;
         binary = binary / 10;
      }
      a = 0;
      while(decimal != 0)
      {
         hexaDecimal[a] = decimal % 16;
         decimal = decimal / 16;
         a++;
      }
      System.out.print("Equivalent hexadecimal value is: ");
      for(b = a - 1; b >= 0; b--)
      {
         if(hexaDecimal[b] > 9)
         {
            System.out.print((char)(hexaDecimal[b] + 55) + "\n");
         }
         else
         {
            System.out.print(hexaDecimal[b] + "\n");
         }
      }
      sc.close();
   }
}
Posted by: Guest on October-30-2020
0

Binary to hexadecimal conversion with java

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

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

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

Code answers related to "java binary to hexadecimal"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language