Question

In: Computer Science

For JavaSript the hexadecimal (base 16) number 9a. In a JavaScript program you would write it...

For JavaSript

the hexadecimal (base 16) number 9a. In a JavaScript program you would write it as: 0x9a. How would you write it in the more familiar base 10? Here is how to solve this.

a.       Recall that a "normal" base 10 number has a ones place, a tens place, a hundreds place, a thousands place, etc. Another way to put it is, it has a 100's place (100=1), a 101's (101=10), a 102's place (102=100), a 103's place (103=1,000), etc. Similarly, a base 16 number has a 160's place (160=1), a 161's (161=16), a 162's place (162=16x16=256), a 163's place (163=16x16x16=4,096), etc.

b.      So the base 16 number 9a has an "a" in the 1s place and a 9 in the 16s place.

c.       It has "a" 1s and 9 16s, so just calculate (a*1)+(9*16).

d.      What number is "a"? Well, base 10 numbers are made with 10 numeral symbols (0 1 2 3 4 5 6 8 9). Similarly, base 16 numbers are made with 16 numeral symbols (0 1 2 3 4 5 6 7 8 9 a b c d e f). An "e" for example has the value of fifteen. Base 2 numbers are made with 2 numeral symbols (0 1). Base 36 uses 36 symbols (0...9 and a...z). In everyday use we get up to base 60 (for measuring time at 60 seconds per minute, 60 minutes per hour) but rather than use 60 different symbols we use other means, as you might have noticed.

e.       You can check your answer in JavaScript using the toString() method:

var num = 128;
num.toString(16);

Next, consider converting base 10 numbers into base 16 or any other base.

Calculate what the base 10 number 1000 is in base 16 (show your work, without using JavaScript). You can check your answer JavaScript using toString() if you like. Here's how to calculate it.

a.       163=4096, and there are no 4096s in 1000, so there is no fourth digit in the answer, since what you really need are the three digits for 162, 161, and 160. How many 162s are in 1000? 162=256, so the question then is how many 256s are in 1000? The answer is 3, with a remainder. So the partial answer is: 3 _ _ . Fill in the 2 blanks.

b.      The 3 is the number of 162s or 256s in 1000, which leaves 1000-3*256 = 232 unaccounted for.

c.       The next digit is for the number of 161=16s we need. We can fit 14 of them into 232: 14*16=224, with a remainder of 8 that is not yet accounted for. 14 is written "e" in base 16, so the answer so far is: 3e_ and we have one blank left to fill.

d.      The 160 or 1s place holds the number of 1s we need. 8 of them are needed to fill up the remainder of 8 that we have to deal with, so the answer is: 3e8. Thus

num = 1000;
num.toString(16);

Solutions

Expert Solution

1. Java script to convert the decimal number to Hexadecimal number is as below:

<!--hexadecimal to decimal-->
<html>
    <script>
        function hexToDec(){
            //var numString = prompt("enter hexadecimal number to convert to decimal"); //If you want to take input from user
                        var numString = 128;   //initialsing value as mentioned in the question
            var numInt = parseInt(numString, 16); //converting to decima fron hexadecimal
            alert('The decimal for given hexadecima is '+numInt); //printing the converted decimal value
        }
    </script>
<body onload="hexToDec()">
</body>
</html>

The screenshot of the output is as below:

2) The javascript to convert decimal to hexadecimal as below:

<!--decimal to hexdecimal-->
<html>
    <script>
        function decToHex(){
            //var numString = parseInt(prompt("enter decimal number to convert to hexdecimal")); //If you want to take input from user
            var num = 1000;  //initialsing value as mentioned in the question
            var res = num.toString(16); //converting to hexadecimal to decimal
            alert('The hexdecimal for given decimal is '+ '0x'+res); //displaying th converted value
        }
    </script>
<body onload="decToHex()">
</body>
</html>

The output screenshot of the above program is :

If you have any queries regarding this answer, please reach out through the comment section.


Related Solutions

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.
Write the below code to use HTML and JavaScript. 1. a) Write a JavaScript program to...
Write the below code to use HTML and JavaScript. 1. a) Write a JavaScript program to display the current day and time. b) Write a JavaScript program to print the contents of the current window.   c) Write a JavaScript program where the program takes a random integer between 1 to 10 d) Write a JavaScript program to calculate multiplication and division of two numbers (input from the user). e)Write a JavaScript program to create a new string from a given...
Convert the hexadecimal number directly to base 4; then convert both the original number and your...
Convert the hexadecimal number directly to base 4; then convert both the original number and your answer to binary to check your result. Please show steps and explain
a. Convert the base 4 number 13023031 base 4 directly to hexadecimal. Check your results by...
a. Convert the base 4 number 13023031 base 4 directly to hexadecimal. Check your results by converting both the original number and your answer to decimal. b. Convert the hexadecimal number 9B62 base 16 directly to base 4; then convert both the original number and your answer to binary to check your result.
Write a c++ program to convert any decimal number to either binary base  or Hex base...
Write a c++ program to convert any decimal number to either binary base  or Hex base number system. Test your program with the followings: Convert 15 from decimal to binary.  Convert 255 from decimal to binary. Convert BAC4 from hexadecimal to binary Convert DEED from hexadecimal to binary.  Convert 311 from decimal to hexadecimal. Convert your age to hexadecimal.
Palindrome Javascript I am trying to write this javascript function to check if a number is...
Palindrome Javascript I am trying to write this javascript function to check if a number is Palindrome.. Palindrome means - a word, phrase, or sequence that reads the same backward as forward, e.g., madam or nurses run. This is the code i have, but it doesnt work. Code: let convertButton = document.getElementsByClassName("btn")[0]; let userInput = document.getElementById("number").value; let results = document.getElementById("result").value; convertButton.addEventListener("click", function (event) { event.preventDefault(); console.log(userInput); if (validatePalidrome(userInput)) document.getElementById("result").innerHTML = "true"; else document.getElementById("result").innerHTML = "false"; }); function validatePalidrome(numbers) { let...
Represent the following in IEEE format. Express your answers in Hexadecimal Format (Base 16) -10.510 9.999...
Represent the following in IEEE format. Express your answers in Hexadecimal Format (Base 16) -10.510 9.999 x 101 + 1.610 x 10-1 0.510 x (-0.4375)10 (1.10 x 1010) x (9.2 x 10-5)
Write the programs in JavaScript: Write a program that reads a text file and outputs the...
Write the programs in JavaScript: Write a program that reads a text file and outputs the text file with line numbers at the beginning of each line.
Python program. Write a python program that can convert any radix-d (arbitrary base) number to the...
Python program. Write a python program that can convert any radix-d (arbitrary base) number to the equivalent radix-e (another arbitrary base) number. Where e and d are members in [2, 16]. Remember, base 16 needs to be calculated as hexadecimal. So, if radix-d is input as a hexadecimal number, it needs to convert and output to desired base. Conversely, if base 16 is the desired output, then the output needs to show a hexadecimal number. Hints: The easiest approach is...
Write a JavaScript program that computes the average of a collection of numbers and then outputs...
Write a JavaScript program that computes the average of a collection of numbers and then outputs the total number of values that are greater than the average. An A grade is any score that is at least 20% greater than the average. The B grade is any score that is not an A, but is at least 10% greater than the average. An F grade is any score that is at least 20% less than the average. The D grade...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT