In: Computer Science
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);
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.