简体   繁体   English

在没有Javascript内置函数的情况下将十进制转换为十六进制

[英]Convert decimal to hexadecimal without Javascript builtin functions

Trying to convert a base 10 number to a base 16 number: 尝试将基数10转换为基数16:

            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. 我正在尝试将10,11,12,..替换为A,B,C,...,但是似乎无法正常工作。 I tried using if else statements, however when I run the code it crashes. 我尝试使用if else语句,但是在运行代码时会崩溃。

In your original code you weren't updating num and it looks like you misunderstood how % works, % is modulus not division. 在您的原始代码中,您没有更新num而您似乎误解了%工作原理, %是模数而不是除法。

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. 如果您不确定在碱基之间进行转换的方式,则可以查看此方法 ,也可以仅通过Google进行查看,那里有很多关于该算法的视频和网站。

JavaScript 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;
  }
}

Fiddle 小提琴

Quick Solution 快速解决方案

Add this line in your while loop somewhere after the line hex=(num%16) 在您的while循环中将此行添加在hex=(num%16)行之后的某处

num = Math.floor(num / 16);

Explanation 说明

Your logic is correct, you simply forgot to (or didn't know that % didn't) divide num by 16. 您的逻辑是正确的,您只是忘记了(或不知道%没有)将num除以16。

Look at this line (line number 5 in your code): 查看以下行(代码中的第5行):

hex = (num % 16);     //divides num by 16

It gets the remainder when num is divided by 16, and store it in hex. 当num除以16时,它将得到余数,并将其存储为十六进制。 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" . 但是,在您的注释中,您注意到该行"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. 它仅得到剩余是什么, 如果是16分,你还有做分割自己。

That can be done with this line: 这行可以完成:

num = Math.floor(num / 16);

in your while loop. 在您的while循环中。 Of course you'll still need the line hex=(num%16); 当然,您仍然需要行hex=(num%16); . I recommend adding the new line right after your first the line hex=(num%16); 我建议您在第一行hex=(num%16);之后添加新行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. 但我还是建议amura.cxg的答案,因为amura.cxg显示了一种非常不错的方式来重新格式化代码,从而使代码编写得井井有条,简洁明了。

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. 我只发布我的答案,以便我可以在错误时准确地向您显示代码的位置,因为我认为这些信息对您非常有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM