Hex To Oct Algorithm

The Hex To Oct Algorithm is a method used for converting a number expressed in the hexadecimal (base 16) numeral system into its equivalent in the octal (base 8) numeral system. Hexadecimal numbers use 16 different symbols to represent values, including the numbers 0-9 and the letters A-F, where A represents 10, B represents 11, and so on until F representing 15. On the other hand, the octal system uses only the digits 0-7 to represent values. The conversion of a number from its hexadecimal representation to its octal representation involves breaking down the number into its individual digits, converting the digits to their binary equivalents, and then regrouping the binary digits into octal digits. To perform the Hex To Oct Algorithm, one must first convert each hexadecimal digit into its 4-bit binary equivalent. For example, the hex number "1A3" would be broken down into three binary numbers: "0001" for 1, "1010" for A (10), and "0011" for 3. The binary representation of "1A3" would then be "000110100011". Next, the binary number is grouped into consecutive sets of three bits, starting from the rightmost bit. If necessary, leading zeroes can be added to the leftmost group to make it a set of three. In our example, the binary number is grouped as "000", "110", "100", and "011". Finally, the binary groups are converted into their octal equivalents: 0, 6, 4, and 3. Thus, the final octal representation of the hex number "1A3" is "0643". This algorithm allows for an efficient and straightforward conversion between two important numeral systems used in computing and digital electronics.
package Conversions;

import java.util.Scanner;

/**
 * Converts any Hexadecimal Number to Octal
 *
 * @author Tanmay Joshi
 */
public class HexToOct {
    /**
     * This method converts a Hexadecimal number to a decimal number
     *
     * @param s The Hexadecimal Number
     * @return The Decimal number
     */
    public static int hex2decimal(String s) {
        String str = "0123456789ABCDEF";
        s = s.toUpperCase();
        int val = 0;
        for (int i = 0; i < s.length(); i++) {
            char a = s.charAt(i);
            int n = str.indexOf(a);
            val = 16 * val + n;
        }
        return val;
    }

    /**
     * This method converts a Decimal number to a octal number
     *
     * @param q The Decimal Number
     * @return The Octal number
     */
    public static int decimal2octal(int q) {
        int now;
        int i = 1;
        int octnum = 0;
        while (q > 0) {
            now = q % 8;
            octnum = (now * (int) (Math.pow(10, i))) + octnum;
            q /= 8;
            i++;
        }
        octnum /= 10;
        return octnum;
    }

    /**
     * Main method that gets the hex input from user and converts it into octal.
     * @param args arguments
     */
    public static void main(String args[]) {
        String hexadecnum;
        int decnum, octalnum;
        Scanner scan = new Scanner(System.in);

        System.out.print("Enter Hexadecimal Number : ");
        hexadecnum = scan.nextLine();

        // first convert hexadecimal to decimal
        decnum = hex2decimal(hexadecnum);       //Pass the string to the hex2decimal function and get the decimal form in variable decnum

        // convert decimal to octal
        octalnum = decimal2octal(decnum);
        System.out.println("Number in octal: " + octalnum);
        scan.close();
    }
}

LANGUAGE:

DARK MODE: