ByteUtils.java 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. package com.its.app.utils;
  2. public class ByteUtils {
  3. private static final int[] BYTE_MASKED_ARRAY = { 1, 2, 4, 8, 16, 32, 64, 128 };
  4. public static int byteToInt(byte srcValue)
  5. {
  6. return srcValue & 0xFF;
  7. }
  8. public static int shortToInt(short srcValue)
  9. {
  10. return srcValue & 0xFFFF;
  11. }
  12. public static long intToLong(int srcValue)
  13. {
  14. return srcValue & 0xFFFFFFFF;
  15. }
  16. public static int longToInt(long srcValue)
  17. {
  18. return (int)(srcValue & 0xFFFFFFFF);
  19. }
  20. public static String byteToBitString(byte srcValue)
  21. {
  22. return String.format("%8s", Long.toBinaryString(srcValue & 0xFF)).replace(' ', '0');
  23. }
  24. public static String shortToBitString(short srcValue)
  25. {
  26. return String.format("%16s", Long.toBinaryString(srcValue & 0xFF)).replace(' ', '0');
  27. }
  28. public static String intToBitString(int srcValue)
  29. {
  30. return String.format("%32s", Integer.toBinaryString(srcValue & 0xFF)).replace(' ', '0');
  31. }
  32. public static String longToBitString(long srcValue)
  33. {
  34. return String.format("%64s", Long.toBinaryString(srcValue & 0xFF)).replace(' ', '0');
  35. }
  36. public static int extractBits(byte srcValue, int beginBitDigit, int endBitDigit)
  37. {
  38. int maskedValue = 0;
  39. for (int idx = beginBitDigit; idx <= endBitDigit; idx++) {
  40. maskedValue += BYTE_MASKED_ARRAY[idx];
  41. }
  42. return (srcValue & maskedValue) >> beginBitDigit;
  43. }
  44. public static int extractBit(byte srcValue, int bitDigit)
  45. {
  46. return (srcValue & BYTE_MASKED_ARRAY[bitDigit]) == BYTE_MASKED_ARRAY[bitDigit] ? 1 : 0;
  47. }
  48. public static int compareMaskedValue(byte srcValue, int maskValue, int compareValue)
  49. {
  50. int resultValue = byteToInt(srcValue) & maskValue;
  51. return resultValue == compareValue ? 0 : resultValue > compareValue ? 1 : -1;
  52. }
  53. public static String convertCustomDateTime(String strDate)
  54. {
  55. String convertDate = strDate;
  56. if (convertDate.lastIndexOf(".") != -1) {
  57. convertDate = convertDate.substring(0, convertDate.lastIndexOf("."));
  58. }
  59. return convertDate.replaceAll("-", "").replaceAll(":", "").replaceAll(" ", "");
  60. }
  61. public static byte[] hexToByteArray(String hex)
  62. {
  63. if ((hex == null) || (hex.length() == 0)) {
  64. return null;
  65. }
  66. byte[] ba = new byte[hex.length() / 2];
  67. for (int i = 0; i < ba.length; i++) {
  68. ba[i] = ((byte)Integer.parseInt(hex.substring(2 * i, 2 * i + 2), 16));
  69. }
  70. return ba;
  71. }
  72. public static String byteArrayToHex(byte[] ba)
  73. {
  74. return byteArrayToHex(ba, null);
  75. }
  76. public static byte[] intToShortBytes(int length)
  77. {
  78. byte[] b3 = new byte[2];
  79. if (length > 255)
  80. {
  81. b3[0] = ((byte)(length / 256));
  82. b3[1] = ((byte)(length % 256));
  83. }
  84. else
  85. {
  86. b3[0] = 0;
  87. b3[1] = ((byte)length);
  88. }
  89. return b3;
  90. }
  91. public static int getIntOnBit(int n, int offset, int length)
  92. {
  93. return n >> 32 - offset - length & (-1 << length ^ 0xFFFFFFFF);
  94. }
  95. public static String byteArrayToHex(byte[] ba, String seperator)
  96. {
  97. if ((ba == null) || (ba.length == 0)) {
  98. return null;
  99. }
  100. StringBuffer sb = new StringBuffer(ba.length * 2);
  101. for (int x = 0; x < ba.length; x++)
  102. {
  103. String hexNumber = "0" + Integer.toHexString(0xFF & ba[x]).toUpperCase();
  104. sb.append(hexNumber.substring(hexNumber.length() - 2));
  105. //if (seperator != null && !seperator.isEmpty()) {
  106. sb.append(seperator);
  107. //}
  108. }
  109. return sb.toString();
  110. }
  111. static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
  112. public static final int BIT_1 = 1;
  113. public static final int BIT_2 = 3;
  114. public static final int BIT_3 = 7;
  115. public static final int BIT_4 = 15;
  116. public static final int BIT_5 = 31;
  117. public static final int BIT_6 = 63;
  118. public static final int BIT_7 = 127;
  119. public static final int BIT_8 = 255;
  120. public static final int BIT_9 = 511;
  121. public static final int BIT_10 = 1023;
  122. public static final int BIT_11 = 2047;
  123. public static final int BIT_12 = 4095;
  124. public static final int BIT_13 = 8191;
  125. public static final int BIT_14 = 16383;
  126. public static final int BIT_15 = 32767;
  127. public static final int BIT_16 = 65535;
  128. public static String bytesToHex(byte[] bytes, int bundleSize, char seperator)
  129. {
  130. char[] hexChars = new char[bytes.length * 2 + bytes.length / bundleSize];
  131. int j = 0;
  132. for (int k = 1; j < bytes.length; k++)
  133. {
  134. int v = bytes[j] & 0xFF;
  135. int start = j * 2 + j / bundleSize;
  136. hexChars[start] = HEX_ARRAY[(v >>> 4)];
  137. hexChars[(start + 1)] = HEX_ARRAY[(v & 0xF)];
  138. if (k % bundleSize == 0) {
  139. hexChars[(start + 2)] = seperator;
  140. }
  141. j++;
  142. }
  143. return new String(hexChars).trim();
  144. }
  145. public static String bytesToHex(byte[] bytes, int bundleSize)
  146. {
  147. return bytesToHex(bytes, bundleSize, ' ');
  148. }
  149. public static String byteToHex(byte b)
  150. {
  151. String hexNumber = "0" + Integer.toHexString(0xFF & b).toUpperCase();
  152. return hexNumber.substring(hexNumber.length() - 2);
  153. }
  154. public static int convertNotNullStringToInt(String data)
  155. {
  156. if (data == null || data.trim().length() == 0)
  157. return 0;
  158. return Long.valueOf(data).intValue();
  159. }
  160. public static double convertNotNullStringToDouble(String data)
  161. {
  162. if (data == null || data.trim().length() == 0)
  163. return 0.0D;
  164. return Double.valueOf(data).doubleValue();
  165. }
  166. public static String extractFilePath(String fullPath)
  167. {
  168. String resultPath = fullPath;
  169. int lastPathIndex = resultPath.lastIndexOf("/") != -1 ? resultPath.lastIndexOf("/") : resultPath.lastIndexOf("\\");
  170. return lastPathIndex != -1 ? resultPath.substring(0, lastPathIndex + 1) : resultPath;
  171. }
  172. public static String extractFileName(String fullPath)
  173. {
  174. String resultPath = fullPath;
  175. int lastPathIndex = resultPath.lastIndexOf("/") != -1 ? resultPath.lastIndexOf("/") : resultPath.lastIndexOf("\\");
  176. return lastPathIndex != -1 ? resultPath.substring(lastPathIndex + 1) : resultPath;
  177. }
  178. public static String lpad(String str, int len, String pad)
  179. {
  180. String result = str;
  181. int templen = len - result.length();
  182. for (int i = 0; i < templen; i++) {
  183. result = pad + result;
  184. }
  185. return result;
  186. }
  187. public static int getBitField(byte b, int field)
  188. {
  189. return (b & 1 << field) > 0 ? 1 : 0;
  190. }
  191. public static int getBitField(byte b, int field, int count)
  192. {
  193. if (count == 1) {
  194. count = 1;
  195. } else if (count == 2) {
  196. count = 3;
  197. } else if (count == 3) {
  198. count = 7;
  199. } else if (count == 4) {
  200. count = 15;
  201. } else if (count == 5) {
  202. count = 31;
  203. } else if (count == 6) {
  204. count = 63;
  205. } else if (count == 7) {
  206. count = 127;
  207. } else if (count == 8) {
  208. count = 255;
  209. } else if (count == 9) {
  210. count = 511;
  211. } else if (count == 10) {
  212. count = 1023;
  213. } else if (count == 11) {
  214. count = 2047;
  215. } else if (count == 12) {
  216. count = 4095;
  217. } else if (count == 13) {
  218. count = 8191;
  219. } else if (count == 14) {
  220. count = 16383;
  221. } else if (count == 15) {
  222. count = 32767;
  223. } else if (count == 16) {
  224. count = 65535;
  225. }
  226. return b >> field & count;
  227. }
  228. public static byte setBitField(byte b, int field, int value)
  229. {
  230. if ((value == 0) || (value == 1)) {
  231. b = (byte)(b | value << field);
  232. }
  233. return b;
  234. }
  235. public static byte setBitField(byte b, int field, int count, int value)
  236. {
  237. if ((field + count <= 8) && (value < Math.pow(2.0D, count))) {
  238. if (count == 1) {
  239. count = 1;
  240. } else if (count == 2) {
  241. count = 3;
  242. } else if (count == 3) {
  243. count = 7;
  244. } else if (count == 4) {
  245. count = 15;
  246. } else if (count == 5) {
  247. count = 31;
  248. } else if (count == 6) {
  249. count = 63;
  250. } else if (count == 7) {
  251. count = 127;
  252. } else if (count == 8) {
  253. count = 255;
  254. } else if (count == 9) {
  255. count = 511;
  256. } else if (count == 10) {
  257. count = 1023;
  258. } else if (count == 11) {
  259. count = 2047;
  260. } else if (count == 12) {
  261. count = 4095;
  262. } else if (count == 13) {
  263. count = 8191;
  264. } else if (count == 14) {
  265. count = 16383;
  266. } else if (count == 15) {
  267. count = 32767;
  268. } else if (count == 16) {
  269. count = 65535;
  270. }
  271. }
  272. b = (byte)(b & (count << field ^ 0xFFFFFFFF));
  273. b = (byte)(b | (value & count) << field);
  274. return b;
  275. }
  276. }