Conversion from Decimal to octal, binary and hexadecimal in javascript

前端 未结 5 1470
花落未央
花落未央 2021-02-08 17:23

I have a form where a user enters a decimal number and then from the dropdown menu he chooses if he wants to convert it either to binary, octal or hexadecimal by clicking on a c

5条回答
  •  抹茶落季
    2021-02-08 18:10

    If you want to convert a number to hexadecimal representation, you can use toString method. First argument of toString can be a radix for numbers. Example:

    var n = 12;
    n.toString(); // "c"
    

    If you want to convert back, you can use parseInt...

    var hexnum = "c";
    parseInt(hexnum,16); // 12
    

    These functions works for any radix.

    Here is the full source code:

    
       
           Convertor 
          
       
       
          

    Enter decimal number to convert, select Base and click CONVERT.

提交回复
热议问题