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
15501
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...

Related Questions:

1helpful
1answer

Modem msi m3490 ip

Do you want to find the modem IP address?
If it yes,visit the site IP-Details.com here you can get it with details like Internet Service Provider[ISP],IP Location,Country,Latitude, Longitude,IPv4 Dotted Binary,Octal,Decimal,Hexadecimal Notation.
1helpful
1answer

Casio fx-83GT-plus how to convert binary to hexadecimal

Sorry to be the carrier of bad news but the Casio FX-83 GT Plus does not have a baseN calculation mode. You cannot do any arithmetic in binary, octal or hexadecimal numeration.
1helpful
1answer

Casio fx-83GT plus how to convert binary to hex

Sorry to be the carrier of bad news but the Casio FX-83 GT Plus does not have a baseN calculation mode. You cannot do any arithmetic in binary, octal or hexadecimal numeration.
0helpful
1answer

How to convert a dinary number to a binary number using a casio fx-82MS calculator

Dinary? Never heard that one before. What is a dinary number? Anyway your calculator cannot perform the operations. The Casios can work in Binary (base 2), Octal (base 8), Decimal (base 10) and Hexadecimal (base 16). Some Sharps can do calculations in base 5 (pental).
3helpful
1answer

How to do binary, octal, hexadecimal conversions in fx-991ms

Press MODE 4 to enter the BASE-N mode. Select the input base. Enter the number. Press =. Select the output base and see the result.
2helpful
1answer

How do you convert decimal numbers into binary?

Press MODE 4 to enter the BASE-N mode. Type in the decimal number, then press BIN to convert to binary. Press DEC to switch back to decimal for the next conversion.

You're limited to numbers in the range [-32768...32767] in binary. You can extend the range if you convert to octal or hexadecimal and then mentally convert each digit to three or four bits.
5helpful
2answers

How to convert to binary using casio fx-350MS?

Hello,
In the FX300Ms manual, the same for the FX350MS, there is no way to use base-n (binary, octal, hexadecimal) calculations.
Sorry.
1helpful
1answer

Binary conversion

Hello,
In the FX-300Ms manual, the same for the FX-82MS, there is no way to use base-n (binary, octal, hexadecimal) calculations.
Sorry.
0helpful
1answer

Programming in c

To convert numbers you can use the printf() function
printf("%d", x'FF') hex to decimal
printf("%o", x'FF') hex to octal

If you want to save the value in another variables use sprintf
e.g

a = x'FF';
b = sprintf("%d",a)


Not finding what you are looking for?

2,825 views

Ask a Question

Usually answered in minutes!

Top Sun Computers & Internet Experts

Rob Hill
Rob Hill

Level 3 Expert

1480 Answers

k24674

Level 3 Expert

8093 Answers

Sean Wright
Sean Wright

Level 3 Expert

2045 Answers

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

Answer questions

Manuals & User Guides

Loading...