Question about Sun Java Programming Language (cdj-275)

Posted by on

### 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

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

Posted on Mar 25, 2009

• 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

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{

System.out.println("Enter the decimal value:");

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

}

}

×

×

my-video-file.mp4

×

## Related Questions:

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

Nov 06, 2013 | Arris 2246N-VGX Router

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

Oct 06, 2013 | Casio Office Equipment & Supplies

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

Oct 06, 2013 | Casio Office Equipment & Supplies

### 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).

Feb 19, 2013 | Office Equipment & Supplies

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

Jun 11, 2012 | Casio FX-115MS Plus Calculator

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

Dec 01, 2010 | Casio FX-115ES Scientific Calculator

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

Mar 06, 2009 | Casio FX-300MS Calculator

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

Feb 22, 2009 | Casio FX-300MS Calculator

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

Jan 15, 2009 | Computers & Internet

2,581 views

Level 3 Expert

Level 3 Expert

Level 3 Expert