Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

245
Visualizações
parseInt not converting decimal to binary?

From my understanding the binary number system uses as set of two numbers, 0's and 1's to perform calculations.

Why does:

console.log(parseInt("11", 2)); return 3 and not 00001011? http://www.binaryhexconverter.com/decimal-to-binary-converter

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Use toString() instead of parseInt:

11..toString(2)
var str = "11";
var bin = (+str).toString(2);
console.log(bin)

According JavaScript's Documentation:

The following examples all return NaN:

parseInt("546", 2); // Digits are not valid for binary representations

over 4 years ago · Santiago Trujillo Relatório

0

parseInt(number, base) returns decimal value of a number presented by number parameter in base base.

And 11 is binary equivalent of 3 in decimal number system.

var a = {};

window.addEventListener('input', function(e){
  a[e.target.name] = e.target.value;
  console.clear();
  console.log(  parseInt(a.number, a.base)  );
}, false);
<input name='number' placeholder='number' value='1010'>
<input name='base' placeholder='base' size=3 value='2'>

over 4 years ago · Santiago Trujillo Relatório

0

As stated in the documentation for parseInt: The parseInt() function parses a string argument and returns an integer of the specified radix (the base in mathematical numeral systems).

So, it is doing exactly what it should do: converting a binary value of 11 to an integer value of 3.

If you are trying to convert an integer value of 11 to a binary value than you need to use the Number.toString method:

console.log(11..toString(2)); // 1011
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda