Conversion from Decimal to octal, binary and hexadecimal in javascript

前端 未结 5 1475
花落未央
花落未央 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:06

    Decimal to hex/oct/bin:

    const hex = (100).toString(16);     // "64"
    const oct = (100).toString(8);      // "144"
    const bin = (100).toString(2);      // "1100100"
    

    and same backwards:

    const dec0 = parseInt("64", 16);     // 100
    const dec1 = parseInt("144", 8);     // 100
    const dec2 = parseInt("1100100", 2); // 100
    

提交回复
热议问题