Question about Sun Java Programming Language (cdj-275)

1 Answer

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

Posted by desireejane on

Ad

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

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




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

    }

    }


×

Ad

Add Your Answer

Uploading: 0%

my-video-file.mp4

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

×

Loading...
Loading...

Related Questions:

1 Answer

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

1 Answer

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

1 Answer

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

1 Answer

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

1 Answer

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

1 Answer

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

2 Answers

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

1 Answer

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

1 Answer

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

Not finding what you are looking for?
Computers & Internet Logo

2,537 people viewed this question

Ask a Question

Usually answered in minutes!

Top Sun Computers & Internet Experts

Rob Hill
Rob Hill

Level 3 Expert

1414 Answers

Sunny Solanki

Level 2 Expert

264 Answers

Ekse

Level 3 Expert

13434 Answers

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

Answer questions

Manuals & User Guides

Loading...