From ec8b8c056adf936100fbad67298219a1ca8c799b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Jozef=20Jim=C3=A9nez=20Leandro?= Date: Fri, 9 Jun 2017 16:50:42 -0600 Subject: [PATCH] Conversions Works Finished some unfinished conversions in Octal system. Added Decimal to HexaDecimal conversion. --- Conversions/DecimalToHexaDecimal.java | 33 +++++++++++++++++++++++++++ Conversions/OctalToBinary.java | 21 ++++++++++++++--- Conversions/OctalToDecimal.java | 20 +++++++++++++--- 3 files changed, 68 insertions(+), 6 deletions(-) create mode 100644 Conversions/DecimalToHexaDecimal.java diff --git a/Conversions/DecimalToHexaDecimal.java b/Conversions/DecimalToHexaDecimal.java new file mode 100644 index 00000000..3072dec0 --- /dev/null +++ b/Conversions/DecimalToHexaDecimal.java @@ -0,0 +1,33 @@ +import java.lang.StringBuilder; +import java.util.Scanner; + +class Test { + private static final int sizeOfIntInHalfBytes = 8; + private static final int numberOfBitsInAHalfByte = 4; + private static final int halfByte = 0x0F; + private static final char[] hexDigits = { + '0', '1', '2', '3', '4', '5', '6', '7', + '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' + }; + + public static String decToHex(int dec) { + StringBuilder hexBuilder = new StringBuilder(sizeOfIntInHalfBytes); + hexBuilder.setLength(sizeOfIntInHalfBytes); + for (int i = sizeOfIntInHalfBytes - 1; i >= 0; --i) + { + int j = dec & halfByte; + hexBuilder.setCharAt(i, hexDigits[j]); + dec >>= numberOfBitsInAHalfByte; + } + return hexBuilder.toString(); + } + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("Write your Number to convert into HexaDecimal: ") + int dec = 305445566; + String hex = Integer.toHexString(dec); + String hex = decToHex(dec); + System.out.println(hex); + } +} \ No newline at end of file diff --git a/Conversions/OctalToBinary.java b/Conversions/OctalToBinary.java index 23c9a46a..6a011c09 100644 --- a/Conversions/OctalToBinary.java +++ b/Conversions/OctalToBinary.java @@ -28,7 +28,22 @@ public class OctalToBinary { * @return The binary number */ public static int convertOctalToBinary(int o) { - - } + Scanner scan; + int num; -} + void getVal() { + + System.out.println("Octal to Binary"); + scan = new Scanner(System.in); + // Entering the needed number + System.out.println("\nEnter the number : "); + num = Integer.parseInt(scan.nextLine(), 8); + } + + void convert() { + + String binary = Integer.toBinaryString(num); + System.out.println("Binary Value is : " + binary); + } + } +} \ No newline at end of file diff --git a/Conversions/OctalToDecimal.java b/Conversions/OctalToDecimal.java index fe82b5ff..637b8f2e 100644 --- a/Conversions/OctalToDecimal.java +++ b/Conversions/OctalToDecimal.java @@ -28,7 +28,21 @@ public class OctalToDecimal { * @return The decimal number */ public static int convertOctalToDecimal(int o) { - + System.out.print("Octal Input: "); + // Read the input from the console which we are expecting as an octal number: + Scanner s = new Scanner(System.in); + String inputHex = s.nextLine(); + try{ + // Actual conversion of Octal to Decimal: + Integer outputDecimal = Integer.parseInt(inputHex, 8); + System.out.println("Decimal Equivalent : " + outputDecimal); + } + catch(NumberFormatException ne){ + // Printing a warning message if the input is not a valid octal number: + System.out.println("Invalid Input, Expecting octal number 0-7"); + } + finally{ + s.close(); + } } - -} +} \ No newline at end of file