简体   繁体   English

将输入转换为十六进制和二进制

[英]Converting input into hex and binary

I watched a few videos on this, cannot seem to convert to Hex or Binary.我观看了一些关于此的视频,似乎无法转换为十六进制或二进制。

I want to take the input, and log the hex and the binary of the input我想接受输入,并记录输入的十六进制和二进制

 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));

I would use toString(base) instead of Number.parseInt() :我会使用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