Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created February 11, 2020 16:37
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/be1902ca099c6e8503f74d8a8e8176e0 to your computer and use it in GitHub Desktop.
Save parzibyte/be1902ca099c6e8503f74d8a8e8176e0 to your computer and use it in GitHub Desktop.
/*
Programado por Luis Cabrera Benito
____ _____ _ _ _
| _ \ | __ \ (_) | | |
| |_) |_ _ | |__) |_ _ _ __ _____| |__ _ _| |_ ___
| _ <| | | | | ___/ _` | '__|_ / | '_ \| | | | __/ _ \
| |_) | |_| | | | | (_| | | / /| | |_) | |_| | || __/
|____/ \__, | |_| \__,_|_| /___|_|_.__/ \__, |\__\___|
__/ | __/ |
|___/ |___/
Blog: https://parzibyte.me/blog
Ayuda: https://parzibyte.me/blog/contrataciones-ayuda/
Contacto: https://parzibyte.me/blog/contacto/
*/
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Escribe el octal:");
String octal = sc.nextLine();
long decimal = octalADecimal(octal);
System.out.printf("El octal %s es %d en decimal", octal, decimal);
}
public static long octalADecimal(String octal) {
long decimal = 0;
int potencia = 0;
for (int x = octal.length() - 1; x >= 0; x--) {
int valorActual = Character.getNumericValue(octal.charAt(x));
long elevado = (long) Math.pow(8, potencia) * valorActual;
decimal += elevado;
potencia++;
}
return decimal;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment