Last active
March 14, 2020 15:52
-
-
Save kubrick06010/a99c01a7b95820ac3d0abfcf4b5c99ac to your computer and use it in GitHub Desktop.
Conversión de números decimales (dec) a números binarios (bin)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ' Conversion de números decimales (dec) a números binarios (bin). | |
| Public Function bin2dec(bin) | |
| len_ec = Len(bin) ' Se mide la longitud del numero entero | |
| ReDim matriz(0, len_ec - 1) | |
| indice = 0 | |
| potencia = len_ec - 1 | |
| For Each elemento In matriz | |
| matriz(0, indice) = Mid(bin, indice + 1, 1) * (2 ^ potencia) | |
| indice = indice + 1 | |
| potencia = potencia - 1 | |
| Next | |
| bin2dec = Application.WorksheetFunction.Sum(matriz) | |
| End Function | |
| Sub Bin2DecTest() | |
| Debug.Assert bin2dec("0") = "0" | |
| Debug.Assert bin2dec("1") = "1" | |
| Debug.Assert bin2dec("10") = "2" | |
| Debug.Assert bin2dec("11") = "3" | |
| Debug.Assert bin2dec("100") = "4" | |
| Debug.Assert bin2dec("101") = "5" | |
| Debug.Assert bin2dec("110") = "6" | |
| Debug.Assert bin2dec("111") = "7" | |
| Debug.Assert bin2dec("1000") = "8" | |
| Debug.Assert bin2dec("1001") = "9" | |
| Debug.Assert bin2dec("1010") = "10" | |
| Debug.Assert bin2dec("1011") = "11" | |
| Debug.Assert bin2dec("1100") = "12" | |
| Debug.Assert bin2dec("1101") = "13" | |
| Debug.Assert bin2dec("1110") = "14" | |
| Debug.Assert bin2dec("1111") = "15" | |
| End Sub |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/ruby | |
| # Conversión de número binarios (bin) a números decimales (dec). | |
| bin = ARGV[0] | |
| len_ec = bin.length | |
| #puts "El string mide:#{len_ec}" | |
| resultado = 0 | |
| indice = 0 | |
| potencia = len_ec.to_i - 1 | |
| matriz = bin.split('') | |
| #puts matriz | |
| #puts "La potencia es:#{potencia}" | |
| matriz.each do |valor| | |
| calculo = valor.to_i * (2 ** potencia) | |
| resultado = resultado + calculo | |
| indice = indice + 1 | |
| potencia = potencia - 1 | |
| #puts "El valor es #{valor}, el calculo es:#{calculo}, la potencia es:#{potencia}" | |
| end | |
| puts "#{resultado}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment