Computers & Internet Logo
Posted on Mar 24, 2009

Java code for adding octal, hexadecimal subtracting binary,

Java codes for adding octal to octal, adding hexadecimal to hexadecimal
and for subtracting binary to binary and subtracting hexadecimal to hexadecimal

1 Answer

Anonymous

Level 1:

An expert who has achieved level 1.

Corporal:

An expert that has over 10 points.

Mayor:

An expert whose answer got voted for 2 times.

Problem Solver:

An expert who has answered 5 questions.

  • Contributor 7 Answers
  • Posted on Mar 25, 2009
Anonymous
Contributor
Level 1:

An expert who has achieved level 1.

Corporal:

An expert that has over 10 points.

Mayor:

An expert whose answer got voted for 2 times.

Problem Solver:

An expert who has answered 5 questions.

Joined: Mar 24, 2009
Answers
7
Questions
0
Helped
15584
Points
26

Hello desireejane,


One method is to do the following

  1. Convert the octal, hexadecimal or binary to decimal.
  2. Add or Subtract the decimal normally
  3. Convert the result back to octal, hexadecimal or binary.

  • Convert the octal to decimal:

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;
}



  • 1 more comment 
  • Anonymous Mar 25, 2009



        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;
    }


  • Anonymous Mar 25, 2009




        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;
    }


  • Anonymous Mar 25, 2009

    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);

    }

    }


×

Add Your Answer

×

Uploading: 0%

my-video-file.mp4

Complete. Click "Add" to insert your video. Add

×

Loading...
Loading...

2,846 views

Ask a Question

Usually answered in minutes!

Top Sun Computers & Internet Experts

Rob Hill
Rob Hill

Level 3 Expert

1483 Answers

Narseman

Level 3 Expert

1092 Answers

Brad Brown

Level 3 Expert

19190 Answers

Are you a Sun Computer and Internet Expert? Answer questions, earn points and help others

Answer questions

Manuals & User Guides

Loading...