Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created August 18, 2020 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/dd00b87c0f1bbba06411236e65b1d149 to your computer and use it in GitHub Desktop.
Save parzibyte/dd00b87c0f1bbba06411236e65b1d149 to your computer and use it in GitHub Desktop.
public static String textToBinary(String originalText) {
String binaryText = "";
for (int i = 0; i < originalText.length(); i++) {
// Primero obtenemos la letra o carácter actual
char currentChar = originalText.charAt(i);
// Necesitamos obtener su representación entera ASCII
int ascii = (int) currentChar;
// Una vez que ya tenemos el entero, lo convertimos a binario
String binary = decimalToBinary(ascii);
// Lo agregamos a la cadena resultante agregando además un espacio
binaryText += binary + " ";
}
// Finalmente regresamos el texto
return binaryText;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment