From 37838f6237aaa79b882f1476634495af093893e6 Mon Sep 17 00:00:00 2001 From: The-TJ <32796932+The-TJ@users.noreply.github.com> Date: Fri, 17 Nov 2017 12:32:45 +0000 Subject: [PATCH] Create HextoDec.java Program that converts Hexadecimal numbers to decimal. --- Conversions/HexToOct.java | 49 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 Conversions/HexToOct.java diff --git a/Conversions/HexToOct.java b/Conversions/HexToOct.java new file mode 100644 index 00000000..7d5579ff --- /dev/null +++ b/Conversions/HexToOct.java @@ -0,0 +1,49 @@ +/* Java Program - Convert Hexadecimal to Octal +Author - Tanmay Joshi*/ + +import java.util.Scanner; + +public class HexToOct +{ + 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; + } + public static void main(String args[]) + { + String hexadecnum; + int decnum, i=1, j; + int octnum[] = new int[100]; + Scanner scan = new Scanner(System.in); + + System.out.print("Enter Hexadecimal Number : "); + hexadecnum = scan.nextLine(); + + // first convert hexadecimal to decimal + + decnum = hex2decimal(hexadecnum); + + // convert decimal to octal + + while(decnum != 0) + { + octnum[i++] = decnum%8; + decnum = decnum/8; + } + + System.out.print("Equivalent Octal Number is :\n"); + for(j=i-1; j>0; j--) + { + System.out.print(octnum[j]); + } + } +}