refactor: HexToOct
(#5377)
This commit is contained in:
parent
44d7cbbaf4
commit
aefc8fd4b8
@ -1,7 +1,5 @@
|
||||
package com.thealgorithms.conversions;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* Converts any Hexadecimal Number to Octal
|
||||
*
|
||||
@ -12,64 +10,53 @@ public final class HexToOct {
|
||||
}
|
||||
|
||||
/**
|
||||
* This method converts a Hexadecimal number to a decimal number
|
||||
* Converts a Hexadecimal number to a Decimal number.
|
||||
*
|
||||
* @param s The Hexadecimal Number
|
||||
* @return The Decimal number
|
||||
* @param hex The Hexadecimal number as a String.
|
||||
* @return The Decimal equivalent as an integer.
|
||||
*/
|
||||
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;
|
||||
public static int hexToDecimal(String hex) {
|
||||
String hexDigits = "0123456789ABCDEF";
|
||||
hex = hex.toUpperCase();
|
||||
int decimalValue = 0;
|
||||
|
||||
for (int i = 0; i < hex.length(); i++) {
|
||||
char hexChar = hex.charAt(i);
|
||||
int digitValue = hexDigits.indexOf(hexChar);
|
||||
decimalValue = 16 * decimalValue + digitValue;
|
||||
}
|
||||
return val;
|
||||
|
||||
return decimalValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method converts a Decimal number to a octal number
|
||||
* Converts a Decimal number to an Octal number.
|
||||
*
|
||||
* @param q The Decimal Number
|
||||
* @return The Octal number
|
||||
* @param decimal The Decimal number as an integer.
|
||||
* @return The Octal equivalent as an integer.
|
||||
*/
|
||||
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++;
|
||||
public static int decimalToOctal(int decimal) {
|
||||
int octalValue = 0;
|
||||
int placeValue = 1;
|
||||
|
||||
while (decimal > 0) {
|
||||
int remainder = decimal % 8;
|
||||
octalValue += remainder * placeValue;
|
||||
decimal /= 8;
|
||||
placeValue *= 10;
|
||||
}
|
||||
octnum /= 10;
|
||||
return octnum;
|
||||
|
||||
return octalValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Main method that gets the hex input from user and converts it into octal.
|
||||
* Converts a Hexadecimal number to an Octal number.
|
||||
*
|
||||
* @param args arguments
|
||||
* @param hex The Hexadecimal number as a String.
|
||||
* @return The Octal equivalent as an integer.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
String hexadecnum;
|
||||
int decnum;
|
||||
int 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();
|
||||
public static int hexToOctal(String hex) {
|
||||
int decimalValue = hexToDecimal(hex);
|
||||
return decimalToOctal(decimalValue);
|
||||
}
|
||||
}
|
||||
|
@ -5,10 +5,29 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class HexToOctTest {
|
||||
@Test
|
||||
public void testHexToDecimal() {
|
||||
assertEquals(255, HexToOct.hexToDecimal("FF"));
|
||||
assertEquals(16, HexToOct.hexToDecimal("10"));
|
||||
assertEquals(0, HexToOct.hexToDecimal("0"));
|
||||
assertEquals(4095, HexToOct.hexToDecimal("FFF"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHexToOct() {
|
||||
assertEquals(110, HexToOct.decimal2octal(HexToOct.hex2decimal("48")));
|
||||
assertEquals(255, HexToOct.decimal2octal(HexToOct.hex2decimal("AD")));
|
||||
public void testDecimalToOctal() {
|
||||
assertEquals(110, HexToOct.decimalToOctal(HexToOct.hexToDecimal("48")));
|
||||
assertEquals(255, HexToOct.decimalToOctal(HexToOct.hexToDecimal("AD")));
|
||||
assertEquals(377, HexToOct.decimalToOctal(255));
|
||||
assertEquals(20, HexToOct.decimalToOctal(16));
|
||||
assertEquals(0, HexToOct.decimalToOctal(0));
|
||||
assertEquals(7777, HexToOct.decimalToOctal(4095));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHexToOctal() {
|
||||
assertEquals(377, HexToOct.hexToOctal("FF"));
|
||||
assertEquals(20, HexToOct.hexToOctal("10"));
|
||||
assertEquals(0, HexToOct.hexToOctal("0"));
|
||||
assertEquals(7777, HexToOct.hexToOctal("FFF"));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user