Java codes for adding octal to octal, adding hexadecimal to hexadecimal
and for subtracting binary to binary and subtracting hexadecimal to hexadecimal
Hello desireejane,
One method is to do the following
public static long octalToDecimal(String octal) throws NumberFormatException {
// Initialize result to 0
long res = 0;
// Do not continue on an empty string
if (octal.isEmpty()) {
throw new NumberFormatException("Empty string is not an octal number");
}
// Consider each digit in the string
for (int i = 0; i < octal.length(); i++) {
// Get the nth char from the right (first = 0)
char n = octal.charAt(octal.length() - (i+1));
int f = (int) n - 48;
// Check if it's a valid bit
if (f < 0 || f > 7) {
// And if not, die horribly
throw new NumberFormatException("Not an octal number");
} else {
// Only add the value if it's a 1
res += f*Math.round(Math.pow(2.0, (3*i)));
}
}
return res;
}
Convert Binary to decimal:
public static long binaryToDecimal(String binary) throws NumberFormatException {
// Initialize result to 0
long res = 0;
// Do not continue on an empty string
if (binary.isEmpty()) {
throw new NumberFormatException("Empty string is not a binary number");
}
// Consider each digit in the string
for (int i = 0; i < binary.length(); i++) {
// Get the nth char from the right (first = 0)
char n = binary.charAt(binary.length() - (i+1));
// Check if it's a valid bit
if ((n != '0') && (n != '1')) {
// And if not, die horribly
throw new NumberFormatException("Not a binary number");
} else if (n == '1') {
// Only add the value if it's a 1
res += Math.round(Math.pow(2.0, i));
}
}
return res;
}
Convert hexadecimal to decimal:
public static long hexadecimalToDecimal(String hex) throws NumberFormatException {
// Initialize result to 0
long res = 0;
// Do not continue on an empty string
if (hex.isEmpty()) {
throw new NumberFormatException("Empty string is not a hexadecimal number");
}
// Consider each digit in the string
for (int i = 0; i < hex.length(); i++) {
// Get the nth char from the right (first = 0)
char n = hex.charAt(hex.length() - (i+1));
int f = (int) n - 48;
// Try to fix A-F values
if (f > 9) {
// A-F
f = f - 7;
if (f > 15) {
// a-f
f = f - 32;
}
}
// Check if it's a valid bit
if (f < 0 || f > 15) {
// And if not, die horribly
throw new NumberFormatException("Not a hexadecimal number");
} else {
// Only add the value if it's a 1
res += f*Math.round(Math.pow(2.0, (4*i)));
}
}
return res;
}
After you converted to decimal , add or subtract normally. For example x+y =z or x-y = z
Now take z and convert it back using the following code:
import java.lang.*;
import java.io.*;
public class DecimalToBinary{
public static void main(String args[]) throws IOException{
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the decimal value:");
String hex = bf.readLine();
int i = Integer.parseInt(hex);
String by = Integer.toBinaryString(i); // <---Here you can change the method to "toHex or toOctal
System.out.println("Binary: " + by);
}
}
×
2,821 views
Usually answered in minutes!
×