Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created February 21, 2020 18:58
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/f28f095df7dbc0d2c300e4579a06a5eb to your computer and use it in GitHub Desktop.
Save parzibyte/f28f095df7dbc0d2c300e4579a06a5eb to your computer and use it in GitHub Desktop.
public static String decimalAOctal(int decimal) {
String octal = "";// Almacenamos el número octal que será el resultado
String caracteresOctales = "01234567";
while (decimal > 0) {
int residuo = decimal % 8;
// El residuo es lo que se suma, y podemos usarlo como índice
// Recordemos que el carácter se pone "a la izquierda", por eso
// concatenamos el carácter y luego lo que ya lleva sumado el octal
octal = (caracteresOctales.charAt(residuo) + octal);
// Lo vamos dividiendo entre 8 para que en algún momento llegue a 0
decimal /= 8;
}
return octal;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment