简体   繁体   English

需要帮助编写代码以将十进制转换为二进制而不使用 toString

[英]Need help writing code to convert decimal to binary without the use of the toString

I'm trying to create my own decimal to binary converter with the method of decrementing the inputted variable (decimal value), by dividing it by 2 and storing the remainder (like 2nd grade math remainder), which is always either 0 or 1. Each of the remainder values i thin should be stored in an array and I think maybe put in backwards so that the most significant digit is first in the array (this is because when decrementing the remainer values are filled in left to right).我正在尝试使用递减输入变量(十进制值)的方法创建自己的十进制到二进制转换器,方法是将其除以 2 并存储余数(如二年级数学余数),余数始终为 0 或 1。我精简的每个余数值都应该存储在一个数组中,我认为可能会向后放置,以便最高有效位在数组中的第一个(这是因为当递减时,剩余值从左到右填充)。 Soooo yea i dont really know how to store the remainder values in an array using a function Thanks in advance and if something is confusing then feel free to ask because im not even sure if this is the best method of doing this its just what i came up with Soooo 是的,我真的不知道如何使用函数将余数存储在数组中提前致谢,如果有什么令人困惑的地方,请随时提问,因为我什至不确定这是否是最好的方法,这正是我来的跟上

function decimalToBinary(num) {
  var bin = 0;
  while (num > 0) {
  bin = num % 2 + bin;
  num >>= 1; // basically /= 2 without remainder if any
  }
  alert("That decimal in binary is " + bin);
}

Your code is almost correct.你的代码几乎是正确的。 The main problem is that bin starts out as 0 ;主要问题是bin0开始; when you add a digit, they are added numerically, so your code ends up just counting the binary 1 s: in this manner, 10 is initial 0 , and +1+0+1+0 , resulting in 2 .当您添加一个数字时,它们会以数字形式添加,因此您的代码最终只计算二进制1 :以这种方式, 10是初始0+1+0+1+0 ,结果为2 You want to handle it as a string: ""+1+0+1+0 results in 1010 .您想将其作为字符串处理: ""+1+0+1+0结果为1010 So, the only needed change is:所以,唯一需要的改变是:

var bin = "";

If you want to solve it using arrays, with minimal changes to your code, it would be:如果您想使用数组解决它,对代码进行最少的更改,它将是:

function decimalToBinary(num) {
  var bin = [];
  while (num > 0) {
  bin.unshift(num % 2);
  num >>= 1; // basically /= 2 without remainder if any
  }
  alert("That decimal in binary is " + bin.join(''));
}

Here, I use .unshift to add an element to the head of the array (and renumbering the remaining elements);在这里,我使用.unshift将一个元素添加到数组的头部(并对剩余元素重新编号); .join() to collect them all into a string. .join()将它们全部收集到一个字符串中。

Or this:或这个:

function decimalToBinary(num) {
  var bin = [];
  while (num > 0) {
  bin[bin.length] = num % 2;
  num >>= 1; // basically /= 2 without remainder if any
  }
  alert("That decimal in binary is " + bin.reverse().join(''));
}

This is not as good, but illustrates some more things you can do with arrays: taking their length, setting an arbitrary element, and flipping them around.这不是那么好,但说明了你可以用数组做的更多事情:获取它们的长度,设置任意元素,并翻转它们。

I have written a custom Decimal to Binary method:我写了一个自定义的十进制到二进制的方法:

function toBinary (input) {
  let options = [1];
  let max = 0;
  let i = 1;

  while(i) {
    max = Math.pow(2, i);
    if (max > input) break;

    options.push(max);
    i++;
  }

  let j = options.length;
  let result = new Array(j);
  result.fill("0");
  while(j >= 0) {
    if (options[j] <= input) {
      result[j] = "1"
      input = input - options[j];
    }
    j--;
  }
  return [...result].reverse().join("");
}




//Test the toBin method with built-in toString(2)

toBinary(100) === (100).toString(2) // true
toBinary(1) === (1).toString(2)     // true
toBinary(128) === (128).toString(2) // true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM