Advertisement
Guest User

Untitled

a guest
Jun 28th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.ArrayList;
  3. import java.util.Collections;
  4.  
  5. public class Conventer {
  6. public static void main()
  7. {
  8. binaryToDecimal();
  9. }
  10. public static void binaryToDecimal()
  11. {
  12. Scanner read = new Scanner(System.in);
  13. //Odebranie od uzytkownika liczby
  14. System.out.println("Please enter the binary code to convert: ");
  15. String binaryCode = read.nextLine();
  16. System.out.println(binaryCode);
  17.  
  18. int binaryCodeLength = binaryCode.length();
  19. int binaryCodeIndex = binaryCode.length() - 1;
  20. int decimalValue = 0;
  21.  
  22. for (int count = 0; count < binaryCodeLength; count++)
  23. {
  24. //Odczytywanie wartosci numerycznej kazdego znaku
  25. int number = Character.getNumericValue(binaryCode.charAt(count));
  26.  
  27. //Przeliczenie liczby w systemie binarnym na dziesiętny.
  28. //decimalPart = wartosc jednego bitu
  29. int bitValue = number * (int) Math.pow(2, binaryCodeIndex);
  30.  
  31. //sumowanie wartosci kazdego bitu
  32. decimalValue += bitValue;
  33.  
  34. //zmiejszanie wartosci indeksu; indeks = numer bitu
  35. binaryCodeIndex -=1;
  36. }
  37.  
  38. System.out.println("Decimal format: " + decimalValue);
  39.  
  40. }
  41.  
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement