[英]Converting input into hex and binary
我观看了一些关于此的视频,似乎无法转换为十六进制或二进制。
我想接受输入,并记录输入的十六进制和二进制
var hexLetters = "0123456789ABCDEF".split(""); var decimalNum = Number(window.prompt("Enter a decimal number to convert")); var binaryNum= ""; console.log("The number " + decimalNum + " in binary is: ") console.log(Number.parseInt(binaryNum, 2)); // returns an integer of the specified radix or base. var hexNum = ""; console.log("The number " + decimalNum + " in hexadecimal is: ") console.log(Number.parseInt(hexNum, 16));
我会使用toString(base)
而不是Number.parseInt()
:
var hexLetters = "0123456789ABCDEF".split("");
var decimalNum = Number(window.prompt("Enter a decimal number to convert"));
var binaryNum= "";
console.log("The number " + decimalNum + " in binary is: ");
console.log(decimalNum.toString(2)); // returns an integer of the specified radix or base.
var hexNum = "";
console.log("The number " + decimalNum + " in hexadecimal is: ");
console.log(decimalNum.toString(16));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.