ByteUtil.java 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. package rs485;
  2. import java.io.ByteArrayInputStream;
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.UnsupportedEncodingException;
  7. import java.nio.ByteBuffer;
  8. public class ByteUtil {
  9. private static ByteBuffer buffer = ByteBuffer.allocate(8);
  10. /**
  11. * int转byte
  12. * @param x
  13. * @return
  14. */
  15. public static byte intToByte(int x) {
  16. return (byte) x;
  17. }
  18. /**
  19. * byte转int
  20. * @param b
  21. * @return
  22. */
  23. public static int byteToInt(byte b) {
  24. //Java的byte是有符号,通过 &0xFF转为无符号
  25. return b & 0xFF;
  26. }
  27. /**
  28. * byte[]转int
  29. * @param b
  30. * @return
  31. */
  32. public static int byteArrayToInt(byte[] b) {
  33. return b[3] & 0xFF |
  34. (b[2] & 0xFF) << 8 |
  35. (b[1] & 0xFF) << 16 |
  36. (b[0] & 0xFF) << 24;
  37. }
  38. public static int byteArrayToInt(byte[] b, int index){
  39. return b[index+3] & 0xFF |
  40. (b[index+2] & 0xFF) << 8 |
  41. (b[index+1] & 0xFF) << 16 |
  42. (b[index+0] & 0xFF) << 24;
  43. }
  44. /**
  45. * int转byte[]
  46. * @param a
  47. * @return
  48. */
  49. public static byte[] intToByteArray(int a) {
  50. return new byte[] {
  51. (byte) ((a >> 24) & 0xFF),
  52. (byte) ((a >> 16) & 0xFF),
  53. (byte) ((a >> 8) & 0xFF),
  54. (byte) (a & 0xFF)
  55. };
  56. }
  57. /**
  58. * short转byte[]
  59. *
  60. * @param b
  61. * @param s
  62. * @param index
  63. */
  64. public static void byteArrToShort(byte b[], short s, int index) {
  65. b[index + 1] = (byte) (s >> 8);
  66. b[index + 0] = (byte) (s >> 0);
  67. }
  68. /**
  69. * byte[]转short
  70. *
  71. * @param b
  72. * @param index
  73. * @return
  74. */
  75. public static short byteArrToShort(byte[] b, int index) {
  76. return (short) (((b[index + 0] << 8) | b[index + 1] & 0xff));
  77. }
  78. /**
  79. * 16位short转byte[]
  80. *
  81. * @param s
  82. * short
  83. * @return byte[]
  84. * */
  85. public static byte[] shortToByteArr(short s) {
  86. byte[] targets = new byte[2];
  87. for (int i = 0; i < 2; i++) {
  88. int offset = (targets.length - 1 - i) * 8;
  89. targets[i] = (byte) ((s >>> offset) & 0xff);
  90. }
  91. return targets;
  92. }
  93. /**
  94. * byte[]转16位short
  95. * @param b
  96. * @return
  97. */
  98. public static short byteArrToShort(byte[] b){
  99. return byteArrToShort(b,0);
  100. }
  101. /**
  102. * long转byte[]
  103. * @param x
  104. * @return
  105. */
  106. public static byte[] longToBytes(long x) {
  107. buffer.putLong(0, x);
  108. return buffer.array();
  109. }
  110. /**
  111. * byte[]转Long
  112. * @param bytes
  113. * @return
  114. */
  115. public static long bytesToLong(byte[] bytes) {
  116. buffer.put(bytes, 0, bytes.length);
  117. buffer.flip();//need flip
  118. return buffer.getLong();
  119. }
  120. /**
  121. * 从byte[]中抽取新的byte[]
  122. * @param data - 元数据
  123. * @param start - 开始位置
  124. * @param end - 结束位置
  125. * @return 新byte[]
  126. */
  127. public static byte[] getByteArr(byte[]data,int start ,int end){
  128. byte[] ret=new byte[end-start];
  129. for(int i=0;(start+i)<end;i++){
  130. ret[i]=data[start+i];
  131. }
  132. return ret;
  133. }
  134. /**
  135. * 流转换为byte[]
  136. * @param inStream
  137. * @return
  138. */
  139. public static byte[] readInputStream(InputStream inStream) {
  140. ByteArrayOutputStream outStream = null;
  141. try {
  142. outStream = new ByteArrayOutputStream();
  143. byte[] buffer = new byte[1024];
  144. byte[] data = null;
  145. int len = 0;
  146. while ((len = inStream.read(buffer)) != -1) {
  147. outStream.write(buffer, 0, len);
  148. }
  149. data = outStream.toByteArray();
  150. return data;
  151. }catch (IOException e) {
  152. return null;
  153. } finally {
  154. try {
  155. if (outStream != null) {
  156. outStream.close();
  157. }
  158. if (inStream != null) {
  159. inStream.close();
  160. }
  161. } catch (IOException e) {
  162. return null;
  163. }
  164. }
  165. }
  166. /**
  167. * byte[]转inputstream
  168. * @param b
  169. * @return
  170. */
  171. public static InputStream readByteArr(byte[] b){
  172. return new ByteArrayInputStream(b);
  173. }
  174. /**
  175. * byte数组内数字是否相同
  176. * @param s1
  177. * @param s2
  178. * @return
  179. */
  180. public static boolean isEq(byte[] s1,byte[] s2){
  181. int slen=s1.length;
  182. if(slen==s2.length){
  183. for(int index=0;index<slen;index++){
  184. if(s1[index]!=s2[index]){
  185. return false;
  186. }
  187. }
  188. return true;
  189. }
  190. return false;
  191. }
  192. /**
  193. * byte数组转换为Stirng
  194. * @param s1 -数组
  195. * @param encode -字符集
  196. * @param err -转换错误时返回该文字
  197. * @return
  198. */
  199. public static String getString(byte[] s1, String encode, String err){
  200. try {
  201. return new String(s1,encode);
  202. } catch (UnsupportedEncodingException e) {
  203. return err==null?null:err;
  204. }
  205. }
  206. /**
  207. * byte数组转换为Stirng
  208. * @param s1-数组
  209. * @param encode-字符集
  210. * @return
  211. */
  212. public static String getString(byte[] s1, String encode){
  213. return getString(s1,encode,null);
  214. }
  215. //测试
  216. public static void main(String[]args){
  217. System.err.println(isEq(new byte[]{1,2},new byte[]{1,2}));
  218. }
  219. /**
  220. * 字节数组转16进制字符串
  221. * @param b
  222. * @return
  223. */
  224. public static String byteArrToHexString(byte[] b){
  225. String result="";
  226. for (int i=0; i < b.length; i++) {
  227. result += Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring(1);
  228. }
  229. return result;
  230. }
  231. /**
  232. * 16进制字符创转int
  233. * @param hexString
  234. * @return
  235. */
  236. public static int hexStringToInt(String hexString){
  237. return Integer.parseInt(hexString,16);
  238. }
  239. /**
  240. * 十进制转二进制
  241. * @param i
  242. * @return
  243. */
  244. public static String intToBinary(int i){
  245. return Integer.toBinaryString(i);
  246. }
  247. /**
  248. * 16进制字符串转换成byte数组 这个转的多了会报错
  249. * */
  250. public static byte[] Hex2Bytes(String hexString){
  251. byte[] arrB = hexString.getBytes();
  252. int iLen = arrB.length;
  253. byte[] arrOut = new byte[iLen / 2];
  254. String strTmp = null;
  255. for (int i = 0; i < iLen; i += 2)
  256. {
  257. strTmp = new String(arrB, i, 2);
  258. arrOut[(i / 2)] = ((byte) Integer.parseInt(strTmp, 16));
  259. }
  260. return arrOut;
  261. }
  262. /**
  263. * 16进制字符串转换成byte数组
  264. * @param hexStr
  265. * @return
  266. */
  267. public static byte[] hexToByteArr(String hexStr) {
  268. String HexStr = "0123456789abcdef";
  269. char[] charArr = hexStr.toCharArray();
  270. byte btArr[] = new byte[charArr.length / 2];
  271. int index = 0;
  272. for (int i = 0; i < charArr.length; i++) {
  273. int highBit = HexStr.indexOf(charArr[i]);
  274. int lowBit = HexStr.indexOf(charArr[++i]);
  275. btArr[index] = (byte) (highBit << 4 | lowBit);
  276. index++;
  277. }
  278. return btArr;
  279. }
  280. /**
  281. * 16进制字符串转换成byte数组
  282. * */
  283. public static byte[] String2Byte(String s) {
  284. s = s.replace(" ", "");
  285. s = s.replace("#", "");
  286. byte[] baKeyword = new byte[s.length() / 2];
  287. for (int i = 0; i < baKeyword.length; i++) {
  288. try {
  289. baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16));
  290. } catch (Exception e) {
  291. e.printStackTrace();
  292. }
  293. }
  294. return baKeyword;
  295. }
  296. public static String hexStr2Str(String hexStr) {
  297. String str = "0123456789abcdef";
  298. char[] hexs = hexStr.toCharArray();
  299. byte[] bytes = new byte[hexStr.length() / 2];
  300. int n;
  301. for (int i = 0; i < bytes.length; i++) {
  302. n = str.indexOf(hexs[2 * i]) * 16;
  303. n += str.indexOf(hexs[2 * i + 1]);
  304. bytes[i] = (byte) (n & 0xff);
  305. }
  306. return new String(bytes);
  307. }
  308. /**
  309. * 字符串转换成为16进制(无需Unicode编码)
  310. * @param str
  311. * @return
  312. */
  313. public static String str2HexStr(String str) {
  314. char[] chars = "0123456789abcdef".toCharArray();
  315. StringBuilder sb = new StringBuilder("");
  316. byte[] bs = str.getBytes();
  317. int bit;
  318. for (int i = 0; i < bs.length; i++) {
  319. bit = (bs[i] & 0x0f0) >> 4;
  320. sb.append(chars[bit]);
  321. bit = bs[i] & 0x0f;
  322. sb.append(chars[bit]);
  323. // sb.append(' ');
  324. }
  325. return sb.toString().trim();
  326. }
  327. }