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 1/4] 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]); + } + } +} From 61aaa5925fcf65c310cd2ece8af39ebe79d1cea7 Mon Sep 17 00:00:00 2001 From: The-TJ <32796932+The-TJ@users.noreply.github.com> Date: Wed, 22 Nov 2017 17:38:51 +0000 Subject: [PATCH 2/4] Update HexToOct.java --- Conversions/HexToOct.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Conversions/HexToOct.java b/Conversions/HexToOct.java index 7d5579ff..275fe84e 100644 --- a/Conversions/HexToOct.java +++ b/Conversions/HexToOct.java @@ -4,7 +4,8 @@ Author - Tanmay Joshi*/ import java.util.Scanner; public class HexToOct -{ +{ + //Function that takes the Hexadecimal number as input and returns the decimal form. public static int hex2decimal(String s) { String str = "0123456789ABCDEF"; @@ -17,7 +18,8 @@ public class HexToOct val = 16*val + n; } return val; - } + } + // Main function that gets the hex input from user and converts it into octal. public static void main(String args[]) { String hexadecnum; @@ -39,7 +41,7 @@ public class HexToOct octnum[i++] = decnum%8; decnum = decnum/8; } - + //Print the octal form of the number. System.out.print("Equivalent Octal Number is :\n"); for(j=i-1; j>0; j--) { From 402c4e8738ea86d633a96dad3eee183da05439da Mon Sep 17 00:00:00 2001 From: The-TJ <32796932+The-TJ@users.noreply.github.com> Date: Wed, 22 Nov 2017 17:43:48 +0000 Subject: [PATCH 3/4] Update HexToOct.java --- Conversions/HexToOct.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/Conversions/HexToOct.java b/Conversions/HexToOct.java index 275fe84e..47b8d7c7 100644 --- a/Conversions/HexToOct.java +++ b/Conversions/HexToOct.java @@ -5,10 +5,11 @@ import java.util.Scanner; public class HexToOct { - //Function that takes the Hexadecimal number as input and returns the decimal form. + /*Function that takes the Hexadecimal number as input and returns the decimal form. + The input is recieved as a string and the return type is int*/ public static int hex2decimal(String s) { - String str = "0123456789ABCDEF"; + String str = "0123456789ABCDEF"; s = s.toUpperCase(); int val = 0; for (int i = 0; i < s.length(); i++) @@ -19,20 +20,20 @@ public class HexToOct } return val; } - // Main function that gets the hex input from user and converts it into octal. + // Main method that gets the hex input from user and converts it into octal. public static void main(String args[]) { String hexadecnum; int decnum, i=1, j; - int octnum[] = new int[100]; + int octnum[] = new int[100]; //Array to store the octal from of the hex number. Scanner scan = new Scanner(System.in); System.out.print("Enter Hexadecimal Number : "); - hexadecnum = scan.nextLine(); + hexadecnum = scan.nextLine(); // first convert hexadecimal to decimal - decnum = hex2decimal(hexadecnum); + decnum = hex2decimal(hexadecnum); //Pass the string to the hex2decimal function and get the decimal form in variable decnum // convert decimal to octal From 27839d2f63bee5cca1f5dd888ed37fa30e44d0a8 Mon Sep 17 00:00:00 2001 From: The-TJ <32796932+The-TJ@users.noreply.github.com> Date: Sun, 26 Nov 2017 20:36:39 +0000 Subject: [PATCH 4/4] Update HexToOct.java --- Conversions/HexToOct.java | 60 ++++++++++++++++++++++++++------------- 1 file changed, 41 insertions(+), 19 deletions(-) diff --git a/Conversions/HexToOct.java b/Conversions/HexToOct.java index 47b8d7c7..d00b0a90 100644 --- a/Conversions/HexToOct.java +++ b/Conversions/HexToOct.java @@ -1,12 +1,20 @@ -/* Java Program - Convert Hexadecimal to Octal -Author - Tanmay Joshi*/ - +/** + + * Converts any Hexadecimal Number to Octal + + * + + * @author Tanmay Joshi + + * + + */ import java.util.Scanner; public class HexToOct { - /*Function that takes the Hexadecimal number as input and returns the decimal form. - The input is recieved as a string and the return type is int*/ + /** + + * This method converts a Hexadecimal number to + + * a decimal number + + * + + * @param The Hexadecimal Number + + * @return The Decimal number + + */ public static int hex2decimal(String s) { String str = "0123456789ABCDEF"; @@ -20,12 +28,34 @@ public class HexToOct } return val; } + + /** + + * This method converts a Decimal number to + + * a octal number + + * + + * @param 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. public static void main(String args[]) { String hexadecnum; - int decnum, i=1, j; - int octnum[] = new int[100]; //Array to store the octal from of the hex number. + int decnum,octalnum; Scanner scan = new Scanner(System.in); System.out.print("Enter Hexadecimal Number : "); @@ -34,19 +64,11 @@ public class HexToOct // 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); + - while(decnum != 0) - { - octnum[i++] = decnum%8; - decnum = decnum/8; - } - //Print the octal form of the number. - System.out.print("Equivalent Octal Number is :\n"); - for(j=i-1; j>0; j--) - { - System.out.print(octnum[j]); - } } }