Question

In: Computer Science

Covert a Hexadecimal to decimal (there is a video for you to watch too). Then write...

Covert a Hexadecimal to decimal (there is a video for you to watch too). Then write a program that can convert a binary to decimal (only for integer case).

There are two ways to do so.

Easy way: Directly use Java built-in method to do this. In this case, only couple lines of code. Hint: Study Integer class in Java.

Hard way: Write your own code to convert a binary to decimal from scratch. The input is a binary string. The program output is its corresponding decimal value. This way you need to design the algorithm.

Solutions

Expert Solution

//HexToDecimal.java

import java.lang.*;
import java.util.Scanner;
public class HexToDecimal
{
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.print("Enter Hex number : ");
       String hex = sc.nextLine();//Take input from user
       hex = hex.toUpperCase();//To convert all characters to uppercase
       //Direct conversion using built-in function from Integer Class
       int decimal = Integer.parseInt(hex,16);
       System.out.println("conversion hex-decimal using Integer class : "+hex+" -> "+decimal);

       //Building our own method to convert hex to decimal

       String numbers = "0123456789ABCDEF";
       int value = 0;
       for(int index = 0; index < hex.length(); index++){
           char ch = hex.charAt(index);
           int digit = numbers.indexOf(ch);
           value = 16*value + digit;
       }
       System.out.println("conversion hex-decimal using our algorithm : "+hex+" -> "+value);
   }
}

//OUTPUT

//BINARYTODECIMAL.JAVA

import java.lang.*;
import java.util.Scanner;
public class BinaryToDecimal
{
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.print("Enter Binary number : ");
       String binary = sc.nextLine();//Take input from user
       //Direct conversion using built-in function from Integer Class
       int decimal = Integer.parseInt(binary,2);
       System.out.println("conversion binary-decimal using Integer class : "+binary+" -> "+decimal);

       //Building our own method to convert binary to decimal

       int value = 0,power=0;
       for(int index = binary.length()-1; index >=0 ; index--){
           char ch = binary.charAt(index);
           int digit = ch - '0';//To get 0 or 1
           value += digit*Math.pow(2,power);
           power++;
       }
       System.out.println("conversion binary-decimal using our algorithm : "+binary+" -> "+value);
   }
}

//OUTPUT


Related Solutions

Covert a Hexadecimal to decimal (there is a video for you to watch too). Then write...
Covert a Hexadecimal to decimal (there is a video for you to watch too). Then write a program that can convert a binary to decimal (only for integer case). There are two ways to do so. Easy way: Directly use Java built-in method to do this. In this case, only couple lines of code. Hint: Study Integer class in Java. Hard way: Write your own code to convert a binary to decimal from scratch. The input is a binary string....
(1) Covert a Hexadecimal to decimal (there is a video for you to watch too). Then...
(1) Covert a Hexadecimal to decimal (there is a video for you to watch too). Then write a program that can convert a binary to decimal (only for integer case). a) Directly use Java built-in method to do this. In this case, only couple lines of code. Hint: Study Integer class in Java. b) Write your own code to convert a binary to decimal from scratch. The input is a binary string. The program output is its corresponding decimal value....
2. Write a program that asks for hexadecimal number and converts it to decimal. Then change...
2. Write a program that asks for hexadecimal number and converts it to decimal. Then change it to convert an octal number to decimal in perl language.
A) As you watch the Zipcar and Segmentation video, write down the different segments that Zipcar...
A) As you watch the Zipcar and Segmentation video, write down the different segments that Zipcar identifies. B) What is different about how they communicate with each segment? What ideas do you have for Zipcar to reach these segments? C) What other ways of segmenting might Zipcar use? (Think about all of the ways that marketers segment based on the chapter content)
Consider you student ID as a decimal number (Example: MIT123456 should be ‘123456’) and covert to...
Consider you student ID as a decimal number (Example: MIT123456 should be ‘123456’) and covert to binary digits. You may use any online decimal to binary converter to convert this. Now draw the message signal for the binary numbers (consider this as your bit stream) and show ASK, BFSK and BPSK signal accordingly.
Program that does conversions (i.e. decimal to hexadecimal, binary to decimal) but needs to have a...
Program that does conversions (i.e. decimal to hexadecimal, binary to decimal) but needs to have a drop down menu to give you the option to choose with conversion you want to go from and convert into (JOptionPane). Cannot use the conversion tool that is built-in Java. Need to use equations or some sort. Please include comments on what is happening to understand.
Explain the following: a bit, octet, hexadecimal, and decimal value
Explain the following: a bit, octet, hexadecimal, and decimal value
Write a MIPS Assembly Language program which runs interactively to convert between decimal, binary, and hexadecimal....
Write a MIPS Assembly Language program which runs interactively to convert between decimal, binary, and hexadecimal. 1. Request input data type. 2. Request input data. 3. Request output data type. 4. Output the data. Use any algorithm
Watch: Mickey Mouse Monopoly After watching the video, you should write one long paragraph summarizes the...
Watch: Mickey Mouse Monopoly After watching the video, you should write one long paragraph summarizes the video and one long paragraph of commentary discussing the video. Your entry should follow this format: Name of Video: Summary: Commentary:
covert -19.875 to the following: i) Two's complement ii) Octal iii) Hexadecimal iv) Gray code
covert -19.875 to the following: i) Two's complement ii) Octal iii) Hexadecimal iv) Gray code
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT