pks II - usefull methods


/ Published in: Java
Save to your folder(s)



Copy this code and paste it in your HTML
  1. /**
  2.   * Prevod pola bytov na int
  3.   * velkost pola b musi byt menej ako 4 (int je 4 byte-ovy)!!!!
  4.   * @param b
  5.   * @return
  6.   */
  7. public static int convertByteArrayToInt(byte[] b) {
  8. byte[] byty = new byte[4]; //integer je 4 bytovy
  9. //vynulujem pole - ked na to tak spatne pozeram, tak ....? vsak pole je standardne nulove....
  10. for (int i = 0; i < 4; i++) {
  11. byty[i] = 0;
  12. }
  13. //naplnim
  14. int j = b.length - 1;
  15. for (int i = 3; i >= 4 - b.length; i--) {
  16. byty[i] = b[j];
  17. j--;
  18. }
  19. //konvertujem
  20. int value = 0;
  21. for (int i = 0; i < 4; i++) {
  22. int shift = (4 - 1 - i) * 8;
  23. value += (byty[i] & 0x000000FF) << shift;
  24. }
  25. return value;
  26. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.