Question

In: Computer Science

(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. This way you need to design the algorithm.

Solutions

Expert Solution

Solution (a) : Binary to Decimal Using In built Java method:

Code:

public class BinToDec {
public static void main(String args []) {
String binary="1010";
int decimal = Integer.parseInt(binary,2);
System.out.println(decimal);
}
}

Output:

Solution (b) : Binary to Decimal without in-built method:

Code:

public class BinToDec {
public static void main(String args []) {String binary="1100";
int binaryNum = Integer.parseInt(binary);
int dec = 0;
int p = 0;
while(true){
if(binaryNum == 0){
break;
} else {
int temp = binaryNum%10;
dec += temp*Math.pow(2, p);
binaryNum = binaryNum/10;
p++;
}
}
System.out.println(dec);
}
}
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....
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 the following binary values to decimal. Do this interpreting the binary as unsigned and...
1) Covert the following binary values to decimal. Do this interpreting the binary as unsigned and signed. a. 0111 1001 b. 1000 0000 c. 1111 1111 PLEASE EXPLAIN IT IN DETAIL
For this 1-2 page assignment, you are going to watch the video of a speech and...
For this 1-2 page assignment, you are going to watch the video of a speech and write a peer review as if the speaker would get the feedback from you in written form. In the review please address the following aspects of the speech, giving clear examples from the speech and then critique the student’s performance. At least three aspects of delivery At least two aspects of content Was the speech effective? Student Speeches for Analysis
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
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
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.
Session 1: Video Discussion Watch Video The Do's and Don'ts in a Professional Workplace Duration: 4:12...
Session 1: Video Discussion Watch Video The Do's and Don'ts in a Professional Workplace Duration: 4:12 User: n/a - Added: 4/13/11 Discussion Question: After watching the video, identify your reactions to the lessons you just learned. Provide a substantive response using a real world example, and relate your answer to a concept you learned from your text.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT