1public class ConvertHexToDecimal
2{
3 public static void main(String[] args)
4 {
5 String strHex = "b";
6 int decimal = Integer.parseInt(strHex, 16);
7 System.out.println("Decimal number : " + decimal);
8 }
9}
1public class DecimalToHexExample2{
2public static String toHex(int decimal){
3 int rem;
4 String hex="";
5 char hexchars[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
6 while(decimal>0)
7 {
8 rem=decimal%16;
9 hex=hexchars[rem]+hex;
10 decimal=decimal/16;
11 }
12 return hex;
13}
14public static void main(String args[]){
15 System.out.println("Hexadecimal of 10 is: "+toHex(10));
16 System.out.println("Hexadecimal of 15 is: "+toHex(15));
17 System.out.println("Hexadecimal of 289 is: "+toHex(289));
18}}
1public class DecimalToBinaryExample2{
2public static void toBinary(int decimal){
3 int binary[] = new int[40];
4 int index = 0;
5 while(decimal > 0){
6 binary[index++] = decimal%2;
7 decimal = decimal/2;
8 }
9 for(int i = index-1;i >= 0;i--){
10 System.out.print(binary[i]);
11 }
12System.out.println();//new line
13}
14public static void main(String args[]){
15System.out.println("Decimal of 10 is: ");
16toBinary(10);
17System.out.println("Decimal of 21 is: ");
18toBinary(21);
19System.out.println("Decimal of 31 is: ");
20toBinary(31);
21}}
1import java.util.Scanner;
2public class DecimalToHexaExample
3{
4 public static void main(String[] args)
5 {
6 Scanner sc = new Scanner(System.in);
7 System.out.println("Please enter decimal number: ");
8 int decimal = sc.nextInt();
9 String strHexadecimal = "";
10 while(decimal != 0)
11 {
12 int hexNumber = decimal % 16;
13 char charHex;
14 if(hexNumber <= 9 && hexNumber >= 0)
15 {
16 charHex = (char)(hexNumber + '0');
17 }
18 else
19 {
20 charHex = (char)(hexNumber - 10 + 'A');
21 }
22 strHexadecimal = charHex + strHexadecimal;
23 decimal = decimal / 16;
24 }
25 System.out.println("Hexadecimal number: " + strHexadecimal);
26 sc.close();
27 }
28}