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.

Student need to do both ways and test them correctly to get 100% credit. If student only did one way or another, the maximum grade is 80%.

Solutions

Expert Solution

SYSTEM OF HEXADECIMAL :

Hexadecimal 0 1 2 3 4 5 6 7 8 9 A B C D E F
Decimal 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Conversion of Hexadecimal to Decimal :

Let the hexadecimal need to be converted be (abc)16 .

Now, moving from right to left increase the power of 16 starting from 0 and multiple with the encountering digits, and each multiples to generate the corresponding decimal number.

For eg.: E7A916 = 14×163+7×162+10×161+9×160= 57344+1792+160+9 =(59305)10

Program to convert binary to decimal:

Easy Way:

import java.util.*;
class easyway
{
public static void main(String args[])
{
Scanner a=new Scanner(System.in);
String s=a.nextLine(); //input line for the binary string
System.out.println("Required decimal number is:");
System.out.println(Integer.parseInt(s,2)); //conversion and printing of required decimal number using built in

// function, Integer.parseInt
}
}

Hard way:

import java.util.*;
class Hardway
{
public static void main(String args[])
{
Scanner a=new Scanner(System.in);
int n=a.nextInt(); //input line
int d=0,p=0; // variable declaration for the calculation of decimal number
while(n!=0) // loop until the complete binary digits are exhausted
{
d+=((n%10)*Math.pow(2,p));
n=n/10;
p++;
}
System.out.println("Required Decimal number is:");
System.out.println(d);
}
}


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....
Write a program in C++ that converts decimal numbers to binary, hexadecimal, and BCD. You are...
Write a program in C++ that converts decimal numbers to binary, hexadecimal, and BCD. You are not allowed to use library functions for conversion. The output should look exactly as follows: DECIMAL      BINARY                     HEXDECIMAL                      BCD 0                      0000 0000                   00                                            0000 0000 0000 1                      0000 0001                   01                                            0000 0000 0001 2                      0000 0010                   02                                            0000 0000 0010 .                       .                                   .                                               . .                       .                                   .                                               . 255                  1111 1111                   FF                                            0010 0101 0101
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)
write a python code to Determine the binary and decimal representations of the following hexadecimal numbers....
write a python code to Determine the binary and decimal representations of the following hexadecimal numbers. Store answers in the appropriate variables in the .py file. (a) 0xfca1 (b) 0xc4d8
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.
Convert the following binary values to hexadecimal and decimal (1 pt each) Write Hex Numbers as...
Convert the following binary values to hexadecimal and decimal (1 pt each) Write Hex Numbers as 0x##(ex 0x0A, 0xFF) Binary Hexadecimal Decimal 0001-1011 0x 0000-1000 0000-0100 0000-1001 0001-1111 1001-1001 0111-1010 1100-0010 1110-0101 1000-1010 0011-0100 0001-1001 0100-0011 1111-1111 1110-0111 0001-0010 0100-1000 0100-1110 1001-0001 0110-1100 Name: Convert the following hexadecimal values to binary and decimal Write binary numbers as 0000-0000 Hexadecimal Binary Decimal 0xf1 0xac 0x56 0x6c 0x32 0x30 0x05 0x28 0xf0 0x07 0x42 0xb9 0x6d 0x2f 0x71 0x0e 0x2d 0xfb 0xba...
Explain the following: a bit, octet, hexadecimal, and decimal value
Explain the following: a bit, octet, hexadecimal, and decimal value
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT