Monday, March 30, 2020

How To Convert Hexadecimal To Decimal, Binary In Addition To Octal Inward Coffee Plan - Example

Hexadecimal to decimal in addition to binary conversion inward Java
This Java programme volition convert Hexadecimal lay out to decimal, binary in addition to Octal inward Java programming language using JDK measure API methods. For beginners hexadecimal is base of operations xvi number, spell decimal is base of operations 10, Octal is base of operations 8 in addition to binary is base of operations 2 numbers inward Number systems. Binary alone contains 0 in addition to 1 bits, octal contains 0 to 7, decimal contains 0 to nine in addition to Hexadecimal contains 0-9,A,B,C,D,E,F where F stand upwards for 15. Thankfully Java library provides convenient method to convert whatever integer from 1 lay out organization to another. In our final article nosotros accept seen How to convert decimal lay out to binary inward Java in addition to inward this article nosotros volition convert Hexadecimal numbers to binary, octal in addition to decimal numbers.If you lot are going for programming interviews, hence its amend to ready How to do this practice without using Java API every bit well, every bit interviewer normally human face programmer to do in that place ain method during interviews e.g. reversing String without using StringBuffer reverse() method.



Hexadecimal to Decimal, binary in addition to Octal inward Java

convert String to Integer inward Java but likewise allows you lot to specify radix or base. Which agency yesteryear using parseInt() you lot tin convert whatever Hexadecimal String to decimal number inward Java. Same technique tin live on used to convert binary String or Octal String to decimal lay out inward Java. Once you lot got an Integer object representing Decimal lay out you lot tin piece of job Integer.toBinaryString() in addition to Integer.toOctalString() to convert same Hexadecimal lay out into Binary in addition to Octal inward Java. 




here is consummate code example of converting Hexadecimal lay out to Binary, decimal in addition to Octal inward Java.

/**
 *
 * Java programme to convert Hexadecimal to binary, decimal in addition to Octal inward Java.
 * Hexadecimal is base of operations 16, Decimal lay out is base of operations 10, Octal is base of operations 8
 * in addition to Binary is base of operations 2 lay out which has only 2 numbers 0 in addition to 1.
 * @author
 */

public class ConvertHexaToDecimal {

 
    public static void main(String args[]) {
        // Ask user to come inward an Hexadecimal lay out inward Console
        System.out.println("Please come inward Hexadecimal lay out : ");
        Scanner scanner = new Scanner(System.in);
     
        String hexadecimal = scanner.next();
     
        //Converting Hexa decimal lay out to Decimal inward Java
        int decimal = Integer.parseInt(hexadecimal, 16);
     
        System.out.println("Converted Decimal lay out is : " + decimal);
   
        //Converting hexa decimal lay out to binary inward Java      
        String binary = Integer.toBinaryString(decimal);
        System.out.printf("Hexadecimal to Binary conversion of %s is %s %n", hexadecimal, binary );
     
        // Converting Hex String to Octal inward Java
        String octal = Integer.toOctalString(decimal);
        System.out.printf("Hexadecimal to Octal conversion of %s is %s %n", hexadecimal, octal );
    }
}

Output:
Please come inward Hexadecimal lay out :
A
Converted Decimal lay out is : 10
Hexadecimal to Binary conversion of Influenza A virus subtype H5N1 is 1010
Hexadecimal to Octal conversion of Influenza A virus subtype H5N1 is 12


An wages of using Java API for Hexadecimal to Decimal, Binary in addition to Octal conversion is that, Above code tin likewise convert negative Hexadecimal lay out to binary, octal in addition to decimal inward Java every bit shown inward below Output:

Please come inward Hexadecimal lay out :
-B
Converted Decimal lay out is : -11
Hexadecimal to Binary conversion of -B is 11111111111111111111111111110101
Hexadecimal to Octal conversion of -B is 37777777765

That's all on How to convert Hexadecimal lay out to Decimal inward Java. By using similar technique nosotros tin likewise convert Hex numbers to binary in addition to Octal every bit well. Let us know if you lot come upwards across whatever other way to convert hex lay out to binary, decimal in addition to Octal inward Java.

Further Learning
Data Structures in addition to Algorithms: Deep Dive Using Java
How to convert Double to String inward Java

No comments:

Post a Comment