Conversion to binary with javascript (logical doubt)

1

Recently I started with all this programming, at this moment I am making a script that transforms a decimal number into binary without using the & or the String. The error that presents me is that giving it integer values brings me closer to the highest value, but using a floating value would not throw me the result. an example

If I use a while (a > = 2) when I enter the 24 It makes me all right until it reaches 3 because 3% 2 = 1 and up there well but when dividing 3/2 = 1.5 (1.5 approaches it to 2) and it generates an additional binary; but if I only use (a > 2) it works well with the number 24 but at the moment of entering another number like the 16 it would be missing a binary that should be done doing the operation of a% 2 when a == 2 ..

<script>
    var listabinario = new Array();
    alert("Bienvenido al programa");
    a = parseInt(prompt("ingrese el numero decimal: "));
    while (a > 2) {
        numero = parseInt(a / 2);
        b = parseInt(a % 2);
        alert(a);
        alert(b);
        listabinario.push(b);
        a = a - numero;
    }
    c = parseInt(a / 2);
    listabinario.push(c);
    alert(listabinario);

</script>
    
asked by Julián Franco 28.05.2018 в 18:09
source

1 answer

-4

What you are looking for I have done in VB.NET and not JAVASCRIPT Here is the code in case you want to translate it and below some examples in java to make conversions ...

Public Function OfOBumber (ByVal Number As Integer) As String         'If Number < 1 Then OfBuyNumber="0": Exit Function         On Error Resume Next         Dim ResultBin As String = vbNullString         Dim ResTempo As String = vbNullString         Dim ResTempoStr As String=""         If Number > 1 Then             Net result = vbNullString             Do While Number > 0                 ResTempo = Number / 2                 ResTempoStr="" + ResTempo                 If Right (ResTempoStr, 1)="5" Then                     ResultBin="1" + ResultBin                     Number = ResTempo - 0.5                 Else                     ResultBin="0" + ResultBin                     Number = ResTempo                 End If

            If Numero = 0 Then
                ResultadoBin = "0" + ResultadoBin
                Exit Do
            End If
            If Numero = 1 Then
                ResultadoBin = "1" + ResultadoBin
                Exit Do
            End If
            If Numero = 0.5 Then
                ResultadoBin = "1" + ResultadoBin
                Exit Do
            End If
        Loop
    Else
        ResultadoBin = Numero
    End If
    DeNumeroABinario = ResultadoBin
End Function

function ConvertirBinario(Texto){
        var TextoConvertir = Texto;
        var Resultado = "";
        var Man = 0; 
        var Cuenta = TextoConvertir.length;
        var TempoLetra = "";
        for (Man = 0; Man < Cuenta; Man++){
            TempoLetra = CharsLeft(TextoConvertir, 1);
            Resultado = "" + Resultado +  CerosToLeft(NumeroABinario(CharToUnicode(TempoLetra)), 8);
            TextoConvertir = CharsRight(TextoConvertir, TextoConvertir.length - 1);
        }    
        return Resultado;
    }
    function CharsLeft(TextChars , NumChars){
    if ( typeof(TextChars) == "string" ){
        if ( typeof(NumChars) == "number" ){
           if (TextChars == ""){ exit(); }
           if (TextChars.length >= NumChars && NumChars >= 1 ){
               var Retorno = TextChars.slice(0, NumChars);
               return Retorno;
           }else{
               return TextChars;
           }
        }
    }    
}
function CharsRight(TextChars , NumChars){
    if ( typeof(TextChars) == "string" ){
        if ( typeof(NumChars) == "number" ){
           if (TextChars == ""){ exit(); }
           if (TextChars.length >= NumChars && NumChars >= 1 ){
               var Retorno = TextChars.slice(TextChars.length - NumChars,TextChars.length);
               return Retorno;
           }else{
               return TextChars;
           }
        }
    }    
}
function CerosToLeft(Number , CountCeros){
    if ( typeof(Number) == "string" ){
        if ( typeof(CountCeros) == "number" ){
            var StringNumber = "";
            var Longitud = 0;
            var Resultado = "";
            StringNumber = "" + Number;
            Longitud = StringNumber.length;
            if ( Longitud < CountCeros ){
                var Man = 0;
                Resultado = "" + StringNumber;
                for (Man=0;Man<CountCeros - Longitud ; Man++ ){
                    Resultado = "0" + Resultado; 
                }
                return Resultado;
            }else{
                Resultado = "" + StringNumber;
                return Resultado;
            }
        }
    }
}
function NumeroABinario(Numero){
    return Numero.toString(2);
}
    
answered by 29.05.2018 в 14:29