From 724ff7ef3078b114bf8fa180aab246422aff17e0 Mon Sep 17 00:00:00 2001 From: MohamedBechir Date: Mon, 25 May 2020 03:33:41 +0100 Subject: [PATCH] Add Hexadecimal to binary conversion --- .../com/conversions/HexadecimalToBinary.java | 48 +++++++++++++++++++ .../conversions/HexadecimalToBinaryTest.java | 22 +++++++++ 2 files changed, 70 insertions(+) create mode 100644 src/main/java/com/conversions/HexadecimalToBinary.java create mode 100644 src/test/java/com/conversions/HexadecimalToBinaryTest.java diff --git a/src/main/java/com/conversions/HexadecimalToBinary.java b/src/main/java/com/conversions/HexadecimalToBinary.java new file mode 100644 index 00000000..c30942ad --- /dev/null +++ b/src/main/java/com/conversions/HexadecimalToBinary.java @@ -0,0 +1,48 @@ +package com.conversions; + +public class HexadecimalToBinary { + /** + * This method converts a hexadecimal number to + * a binary number. + * + * @param hexStr The hexadecimal number + * @return The binary number + */ + + public String hexToBin (String hexStr) { + + String binaryString = "", hexaNumbers = "0123456789ABCDEF"; + int indexOfHex, decimalNumber = 0, k = 1; + char letter; + int binaryArray[] = new int [50]; + + hexStr = hexStr.toUpperCase(); + + /** + * Transform the hexadecimal number to decimal number + */ + for ( int i = 0 ; i < hexStr.length(); i++) { + letter = hexStr.charAt(i); + indexOfHex = hexaNumbers.indexOf(letter); + decimalNumber = 16 * decimalNumber + indexOfHex; + } + + /** + * Transform decimal number to binary and put it in an array + */ + while (decimalNumber != 0) { + binaryArray[k++] = decimalNumber % 2; + decimalNumber = decimalNumber / 2; + } + /** + * Put the binary in a string + */ + for ( int j = k-1 ; j>0 ; j--) { + binaryString = binaryString + binaryArray[j]; + } + + return binaryString; + + } + +} diff --git a/src/test/java/com/conversions/HexadecimalToBinaryTest.java b/src/test/java/com/conversions/HexadecimalToBinaryTest.java new file mode 100644 index 00000000..0a169b86 --- /dev/null +++ b/src/test/java/com/conversions/HexadecimalToBinaryTest.java @@ -0,0 +1,22 @@ +package com.conversions; + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +class HexadecimalToBinaryTest { + + @Test + void test() { + HexadecimalToBinary hexadecimalToBinary = new HexadecimalToBinary(); + Assertions.assertEquals("10101011", hexadecimalToBinary.hexToBin("AB"), "Incorrect Conversion"); + Assertions.assertEquals("10101101010101111001101", hexadecimalToBinary.hexToBin("56ABCD"), "Incorrect Conversion"); + Assertions.assertEquals("10011101111011010001001", hexadecimalToBinary.hexToBin("4ef689"), "Incorrect Conversion"); + Assertions.assertEquals("10011101111", hexadecimalToBinary.hexToBin("4EF"), "Incorrect Conversion"); + Assertions.assertEquals("101010111100110111101111", hexadecimalToBinary.hexToBin("ABCDEF"), "Incorrect Conversion"); + //It returns -1 if you enter a wrong hexaDecimal + Assertions.assertEquals("-1", hexadecimalToBinary.hexToBin("K"), "Incorrect Conversion"); + } + +}