Created
February 21, 2020 19:02
-
-
Save parzibyte/9e1b4a4623ec5475d9a51913065729d6 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
/* | |
____ _____ _ _ _ | |
| _ \ | __ \ (_) | | | | |
| |_) |_ _ | |__) |_ _ _ __ _____| |__ _ _| |_ ___ | |
| _ <| | | | | ___/ _` | '__|_ / | '_ \| | | | __/ _ \ | |
| |_) | |_| | | | | (_| | | / /| | |_) | |_| | || __/ | |
|____/ \__, | |_| \__,_|_| /___|_|_.__/ \__, |\__\___| | |
__/ | __/ | | |
|___/ |___/ | |
Blog: https://parzibyte.me/blog | |
Ayuda: https://parzibyte.me/blog/contrataciones-ayuda/ | |
Contacto: https://parzibyte.me/blog/contacto/ | |
Copyright (c) 2020 Luis Cabrera Benito | |
Licenciado bajo la licencia MIT | |
El texto de arriba debe ser incluido en cualquier redistribucion | |
*/ | |
public class Main { | |
public static void main(String[] args) { | |
int decimal = 2811; | |
String octal = Integer.toOctalString(decimal); | |
System.out.printf("El decimal %d es %s en octal usando Integer.toOctalString\n", decimal, octal); | |
octal = decimalAOctal(decimal); | |
System.out.printf("El decimal %d es %s en octal usando metodo manual\n", decimal, octal); | |
} | |
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