Log in
updated 11:29 AM UTC, May 4, 2016

How to convert Integer to Hexadecimal/Octal/Binary in Java

How to convert Integer to Hexadecimal/Octal/Binary in Java.

This is one of the frequently asked Interview questions (converting from One number system to another) even though there are many API's to do it easily , Its quite important to know the logic of converting from One form to another. This also tests the candidates ability to form algorithms to solve logical problems.

Here is a program which converts the Integer to Hexadecimal.

This uses logic to convert rather than using the Java provided API's like Integer.toHexString().

 This program converts Integer to Hexadecimal:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 *
 * @author M.Vasudevarao
 */
public class DecToHexConverter
{
    public String convertToHex(int decimalVal)
    {
        String hexValue = "";
        int reminder = 0;
        do
        {
            reminder = decimalVal % 16;
            hexValue = (reminder <= 9 && reminder >= 0) ? ((char) ('0' + reminder) + hexValue) : ((char) ('A' + (reminder - 10)) + hexValue);
            decimalVal = decimalVal / 16;

        } while (decimalVal != 0);
        return hexValue;
    }

    public static void main(String args[]) throws IOException
    {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String intValInStr;
        System.out.println("Enter the integer val: ");
        intValInStr = reader.readLine();
        DecToHexConverter converter = new DecToHexConverter();
        Integer intVal = Integer.parseInt(intValInStr);
        if (intVal.intValue() < 0)
        {
            System.out.println("Negative Value can't be converted");
            return;
        }
        System.out.println("HexValue = " + converter.convertToHex(intVal));
    }
}

 

This program is to convert Decimal to Octal:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 *
 * @author M.Vasudevarao
 */
public class DecToOctConverter
{
    public String convertToOct(int decimalVal)
    {
        String octValue = "";
        int reminder = 0;
        do
        {
            reminder = decimalVal % 8;
            octValue = reminder + octValue;
            decimalVal = decimalVal / 8;

        } while (decimalVal != 0);
        return octValue;
    }

    public static void main(String args[]) throws IOException
    {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String intValInStr;
        System.out.println("Enter the integer val: ");
        intValInStr = reader.readLine();
        DecToOctConverter converter = new DecToOctConverter();
        Integer intVal = Integer.parseInt(intValInStr);
        if (intVal.intValue() < 0)
        {
            System.out.println("Negative Value can't be converted");
            return;
        }
        System.out.println("OctValue = " + converter.convertToOct(intVal));
    }
}

This Program converts Integer to Binary:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package NumConverter;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 *
 * @author M.Vasudevarao
 */
public class DecToBinaryConverter
{
    public String convertToBinary(int decimalVal)
    {
        String binaryValue = "";
        int reminder = 0;
        do
        {
            reminder = decimalVal % 2;
            binaryValue = reminder + binaryValue;
            decimalVal = decimalVal / 2;

        } while (decimalVal != 0);
        return binaryValue;
    }

    public static void main(String args[]) throws IOException
    {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String intValInStr;
        System.out.println("Enter the integer val: ");
        intValInStr = reader.readLine();
        DecToBinaryConverter converter = new DecToBinaryConverter();
        Integer intVal = Integer.parseInt(intValInStr);
        if (intVal.intValue() < 0)
        {
            System.out.println("Negative Value can't be converted");
            return;
        }
        System.out.println("BinaryValue = " + converter.convertToBinary(intVal));
    }
}


Powered by Bullraider.com
Follow Us on Twitter
Find Us on Facebook
Follow Us on Google
Follow Us on Pinterest