therrontelford

Untitled

Aug 2nd, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class Program {
  3.  
  4. public static void main(String[] args) {
  5. Scanner input = new Scanner(System.in);
  6. System.out.println("Enter a hex number");
  7. String hex=input.nextLine();
  8.  
  9.  
  10. int sum=0;
  11.  
  12. for (int i=hex.length()-1; i>=0; i--){
  13. int dec=0;
  14. char hexChar=hex.charAt((hex.length()-1)-i);
  15.  
  16. System.out.println(hexChar);
  17.  
  18. if (hexChar>= 'A' && hexChar<='F'){
  19. dec=10+hexChar-'A';
  20.  
  21. }
  22. else{
  23. dec=hexChar-'0';}
  24. System.out.println(dec);
  25. sum=(int) (sum+Math.pow(16.0,i)*dec);
  26. }
  27. System.out.print(sum);
  28.  
  29. }
  30.  
  31. }
Add Comment
Please, Sign In to add comment