Ejemplo 1: Java convertir hexadecimal a decimal
public class ConvertHexToDecimal
{
public static void main(String[] args)
{
String strHex = "b";
int decimal = Integer.parseInt(strHex, 16);
System.out.println("Decimal number : " + decimal);
}
}
Ejemplo 2: Java convierte octal a decimal
public class OctalToDecimalDemo
{
public static void main(String[] args)
{
String strOctal = "141";
// converting octal to decimal number using Integer.parseInt() method
int decimal = Integer.parseInt(strOctal, 8);
System.out.println(decimal);
}
}
Ejemplo 3: Java convierte binario a decimal
import java.util.Scanner;
public class BinaryToDecimalDemo
{
public static void main(String[] args)
{
int number, decimal = 0, a = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Please enter binary number: ");
String strBinary = sc.nextLine();
number = Integer.parseInt(strBinary);
while(number != 0){
decimal += (number % 10) * Math.pow(2, a);
number = number / 10;
a++;
}
System.out.println("Decimal number: " + decimal);
sc.close();
}
}
Ejemplo 4: convertir decimal a hexadecimal en java
public class DecimalToHexExample2{
public static String toHex(int decimal){
int rem;
String hex="";
char hexchars[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
while(decimal>0)
{
rem=decimal%16;
hex=hexchars[rem]+hex;
decimal=decimal/16;
}
return hex;
}
public static void main(String args[]){
System.out.println("Hexadecimal of 10 is: "+toHex(10));
System.out.println("Hexadecimal of 15 is: "+toHex(15));
System.out.println("Hexadecimal of 289 is: "+toHex(289));
}}
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)