Created
August 18, 2020 14:17
-
-
Save parzibyte/ee9d93b48cbecf4bb71bb656bbd413cd to your computer and use it in GitHub Desktop.
This file contains 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
public static int binaryToDecimal(String binary) { | |
// A este número le vamos a sumar cada valor binario | |
int decimal = 0; | |
int position = 0; | |
// Recorrer la cadena... | |
for (int x = binary.length() - 1; x >= 0; x--) { | |
// Saber si es 1 o 0; primero asumimos que es 1 y abajo comprobamos | |
short digit = 1; | |
if (binary.charAt(x) == '0') { | |
digit = 0; | |
} | |
/* | |
Se multiplica el dígito por 2 elevado a la potencia | |
según la posición; comenzando en 0, luego 1 y así | |
sucesivamente | |
*/ | |
double multiplier = Math.pow(2, position); | |
decimal += digit * multiplier; | |
position++; | |
} | |
return decimal; | |
} | |
public static String decimalToBinary(int decimal) { | |
if (decimal <= 0) { | |
return "0"; | |
} | |
String binary = ""; | |
while (decimal > 0) { | |
short remainder = (short) (decimal % 2); | |
decimal = decimal / 2; | |
// Insertar el dígito al inicio de la cadena | |
binary = String.valueOf(remainder) + binary; | |
} | |
return binary; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment