Advertisement
Guest User

Untitled

a guest
Dec 1st, 2015
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class FunHexadecimal{
  3. public static String letShift(String str , int n){
  4. Scanner in = new Scanner(System.in);
  5. int time;
  6. switch(n)
  7. {
  8. case 0 : System.out.println("Why you don't play?");
  9. break;
  10. case 1 : System.out.println("Please input time you wand to shift");
  11. time = in.nextInt();
  12. for(int i = 1 ; i <= time ; i++)
  13. {
  14. str = 0 + str;
  15. }
  16. //return str.substring(0,str.length()-time);
  17. break;
  18. case -1 : System.out.println("Please input time you wand to shift");
  19. time = in.nextInt();
  20. for(int i = 1 ; i <= time ; i++)
  21. {
  22. str = str + 0;
  23. }
  24. //return str.substring(time,str.length());
  25. break;
  26. }
  27. return str;
  28. }
  29. public static String winWin(int x){
  30. String s = "" + x;
  31. if(x == 15) s = "F";
  32. if(x == 14) s = "E";
  33. if(x == 13) s = "D";
  34. if(x == 12) s = "C";
  35. if(x == 11) s = "B";
  36. if(x == 10) s = "A";
  37. return s;
  38. }
  39. public static int funPower(int a , int b){
  40. int sum = 1;
  41. for(int i = 1 ; i <=b ; i++)
  42. {
  43. sum = sum*a;
  44. }
  45. return sum;
  46. }
  47.  
  48. public static void main(String[]args){
  49. Scanner in = new Scanner(System.in);
  50. int hexin = 0,i;
  51. String hex = in.nextLine() , s = "";
  52. int lastdi = 0;
  53. String x = "";
  54. for(i = hex.length() - 1 ; i>=0 ; i-- )
  55. {
  56. s = ""+hex.charAt(i);
  57. if(hex.charAt(i)== 'A') s = "10";
  58. if(hex.charAt(i)== 'B') s = "11";
  59. if(hex.charAt(i)== 'C') s = "12";
  60. if(hex.charAt(i)== 'D') s = "13";
  61. if(hex.charAt(i)== 'E') s = "14";
  62. if(hex.charAt(i)== 'F') s = "15";
  63. hexin = Integer.parseInt(s);
  64. while(hexin > 0)
  65. {
  66. lastdi = hexin % 2;
  67. x = x + lastdi;
  68. hexin = hexin / 2;
  69. }
  70. }
  71. System.out.println(x);
  72. System.out.println("Do you want to shift?\nIf yes and shift right say 1\nIf no say 0\nIf yes and shift left say -1");
  73. int shift = in.nextInt();
  74. x = letShift(x, shift);
  75. int div = x.length()%4;
  76. if(div != 0)
  77. {
  78. for(i = 1 ; i <= 4-div; i++)
  79. {
  80. x = 0 + x;
  81. }
  82. }
  83. System.out.println(x);
  84. int j,bin,power,p =3,sum=0;
  85. String boy = "";
  86. for(i = 0 ; i+3 <= x.length(); i+=4)
  87. {
  88. for(j=0; j <=3 ;j++,p--)
  89. {
  90. power = funPower(2,p);
  91. bin = Integer.parseInt(x.substring(i+j,i+j+1));
  92. sum = sum + (bin*power);
  93. }
  94. boy = boy + winWin(sum);
  95. }
  96. System.out.println(boy);
  97.  
  98.  
  99. }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement