簡體   English   中英

將輸入轉換為十六進制和二進制

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM