Hexadecimal to decimal number conversion program in java. For example 15 is decimal representation of Hexadecimal number F.
Hexadecimal to decimal in java program
import java.util.Scanner; public class Programming { static int power(int a,int b) { int p=1; while(b>0) { p=p*a; b--; } return p; } public static void main(String[] args) { String hex; int dec=0;; int loop=0; Scanner input=new Scanner(System.in); System.out.print("enter Hexadecimal value:"); hex=input.nextLine(); input.close(); int len=hex.length(); while(len>0) { int p=0; char ch=hex.charAt(len-1); if(ch>=97 && ch<=102) p=ch-87; else if(ch>=65 && ch<=70) p=ch-55; else p=ch-48; dec=dec+p*power(16,loop); loop++; len--; } System.out.println("Decimal Equivalent is :"+dec); } }
OUTPUT: enter Hexadecimal value:FFFF Decimal Equivalent is :65535 ---------------------------- enter Hexadecimal value:A Decimal Equivalent is :10 --------------------------- enter Hexadecimal value:F Decimal Equivalent is :15