Reputation: 17
Trying to convert a base 10 number to a base 16 number:
var hex = []; //array for hexadecimals
while (num > 0) { //if num greater than 0 loop will run
hex=(num%16); //divides num by 16
if (hex>9) {
if (hex==10) {
hex.unshift("A")
}
if (hex==11) {
hex.unshift("B")
}
if (hex==12) {
hex.unshift("C")
}
if (hex==13) {
hex.unshift("D")
}
if (hex==14) {
hex.unshift("E")
}
if (hex==15) {
hex.unshift("F")
}
}
if (hex<=9) {
hex.unshift(hex)
}
}
alert("That decimal in hexadecimal is " + hex.join(''));
}
}
For whatever reason this code is not working for me... any thoughts?? I'm trying to replace 10,11,12,.. with A , B , C, ... however it doesn't seem to be working. I tried using if else statements, however when I run the code it crashes.
Upvotes: 0
Views: 440
Reputation: 4555
Add this line in your while loop somewhere after the line hex=(num%16)
num = Math.floor(num / 16);
Your logic is correct, you simply forgot to (or didn't know that % didn't) divide num
by 16.
Look at this line (line number 5 in your code):
hex = (num % 16); //divides num by 16
It gets the remainder when num is divided by 16, and store it in hex. That is correct so far, since you'll need to know what that value is.
However, in your comment you note that the line "divides num by 16"
. This is not quite true. It only gets what the remainder would be, if it was divided by 16. You still have to do the dividing yourself.
That can be done with this line:
num = Math.floor(num / 16);
in your while loop. Of course you'll still need the line hex=(num%16);
. I recommend adding the new line right after your first the line hex=(num%16);
, so its purpose is clear.
Your edited code could loop like this:
var hex = []; // array for hexadecimals
while (num > 0) { // if num greater than 0 loop will run
hex = (num % 16); // Gets remainder when num is divided by 16
num = Math.floor(num / 16) // Actually divides num by 16
if (hex > 9) {
.
.
.
I recommend amura.cxg's answer though, as amura.cxg shows a very nice way to reformat your code so that it is well-written, clear, and concise.
I only posted my answer so that I could show you exactly where your code when wrong, since I thought that information would be very helpful to you.
Upvotes: 1
Reputation: 2428
In your original code you weren't updating num
and it looks like you misunderstood how %
works, %
is modulus not division.
If you aren't sure how converting between bases works you can check out this or just Google it, there are many videos and sites about the algorithm.
JavaScript
var number = 123456; //The input value
var hexCharacters = ["A", "B", "C", "D", "E", "F"]; //Digits for 10-15, eliminates having case statements
var hexString = "";
while (number > 0) {
var mod = number % 16; //Get the remainder
number = Math.floor(number / 16); //Update number
//Prepend the corresponding digit
if (mod > 9) {
hexString = hexCharacters[mod - 10] + hexString; //Get the digits for 10-15 from the array
} else {
hexString = mod.toString() + hexString;
}
}
Upvotes: 2