最全面的Java位元組byte操作,處理Java基本數據的轉換及進位轉換操作工具

  • 2019 年 11 月 1 日
  • 筆記

版權聲明:本文為部落客原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接和本聲明。

本文鏈接:https://blog.csdn.net/eguid_1/article/details/71746198

前言:用於處理Java基本數據的轉換及進位轉換操作工具一、實現功能

1、int與byte互轉

2、int與byte[]互轉

3、short與byte互轉

4、short與byte[]互轉

5、16位short與byte[]互轉

6、long與byte[]互轉

7、byte[]與inputstream互轉

8、byte與String互轉

9、16進位字元轉int

10、十進位轉2進位

11、byte[]轉16進位字元

12、byte[]數組指定位置抽取byte[]

二、程式碼實現

package cc.eguid.util;    import java.io.ByteArrayInputStream;  import java.io.ByteArrayOutputStream;  import java.io.IOException;  import java.io.InputStream;  import java.io.UnsupportedEncodingException;  import java.nio.ByteBuffer;  /**   * 基本數據互轉工具   * @author eguid   eguid的官網:www.eguid.cc   *eguid的csdn部落格:http://blog.csdn.net/eguid_1,部落格園:http://www.cnblogs.com/eguid   */  public class ByteUtil {  	private static ByteBuffer buffer = ByteBuffer.allocate(8);  	 /**  	  * int轉byte  	  * @param x  	  * @return  	  */      public static byte intToByte(int x) {          return (byte) x;      }      /**       * byte轉int       * @param b       * @return       */      public static int byteToInt(byte b) {          //Java的byte是有符號,通過 &0xFF轉為無符號          return b & 0xFF;      }        /**       * byte[]轉int       * @param b       * @return       */      public static int byteArrayToInt(byte[] b) {          return   b[3] & 0xFF |                  (b[2] & 0xFF) << 8 |                  (b[1] & 0xFF) << 16 |                  (b[0] & 0xFF) << 24;      }      public static int byteArrayToInt(byte[] b, int index){      	  return   b[index+3] & 0xFF |                    (b[index+2] & 0xFF) << 8 |                    (b[index+1] & 0xFF) << 16 |                    (b[index+0] & 0xFF) << 24;      }      /**       * int轉byte[]       * @param a       * @return       */      public static byte[] intToByteArray(int a) {          return new byte[] {              (byte) ((a >> 24) & 0xFF),              (byte) ((a >> 16) & 0xFF),              (byte) ((a >> 8) & 0xFF),              (byte) (a & 0xFF)          };      }      /**       * short轉byte[]       *       * @param b       * @param s       * @param index       */      public static void byteArrToShort(byte b[], short s, int index) {          b[index + 1] = (byte) (s >> 8);          b[index + 0] = (byte) (s >> 0);      }      /**       * byte[]轉short       *       * @param b       * @param index       * @return       */      public static short byteArrToShort(byte[] b, int index) {          return (short) (((b[index + 0] << 8) | b[index + 1] & 0xff));      }      /**       * 16位short轉byte[]       *       * @param s       *            short       * @return byte[]       * */      public static byte[] shortToByteArr(short s) {          byte[] targets = new byte[2];          for (int i = 0; i < 2; i++) {              int offset = (targets.length - 1 - i) * 8;              targets[i] = (byte) ((s >>> offset) & 0xff);          }          return targets;      }      /**       * byte[]轉16位short       * @param b       * @return       */      public static short byteArrToShort(byte[] b){      	return byteArrToShort(b,0);      }        /**       * long轉byte[]       * @param x       * @return       */      public static byte[] longToBytes(long x) {          buffer.putLong(0, x);          return buffer.array();      }      /**       * byte[]轉Long       * @param bytes       * @return       */      public static long bytesToLong(byte[] bytes) {          buffer.put(bytes, 0, bytes.length);          buffer.flip();//need flip          return buffer.getLong();      }      /**       * 從byte[]中抽取新的byte[]       * @param data - 元數據       * @param start - 開始位置       * @param end - 結束位置       * @return 新byte[]       */      public static byte[] getByteArr(byte[]data,int start ,int end){  		byte[] ret=new byte[end-start];  		for(int i=0;(start+i)<end;i++){  			ret[i]=data[start+i];  		}  		 return ret;  	}    	/**  	 * 流轉換為byte[]  	 * @param inStream  	 * @return  	 */  	public static byte[] readInputStream(InputStream inStream) {  		ByteArrayOutputStream outStream = null;  		try {  			outStream = new ByteArrayOutputStream();  			byte[] buffer = new byte[1024];  			byte[] data = null;  			int len = 0;  			while ((len = inStream.read(buffer)) != -1) {  				outStream.write(buffer, 0, len);  			}  			data = outStream.toByteArray();  			return data;  		}catch (IOException e) {  			return null;  		} finally {  			try {  				if (outStream != null) {  					outStream.close();  				}  				if (inStream != null) {  					inStream.close();  				}  			} catch (IOException e) {  				return null;  			}  		}  	}  	/**  	 * byte[]轉inputstream  	 * @param b  	 * @return  	 */  	public static InputStream readByteArr(byte[] b){  		return new ByteArrayInputStream(b);  	}  	/**  	 * byte數組內數字是否相同  	 * @param s1  	 * @param s2  	 * @return  	 */  	public static boolean isEq(byte[] s1,byte[] s2){  		int slen=s1.length;  		if(slen==s2.length){  			for(int index=0;index<slen;index++){  				if(s1[index]!=s2[index]){  					return false;  				}  			}  			return true;  		}  		return  false;  	}  	/**  	 * byte數組轉換為Stirng  	 * @param s1 -數組  	 * @param encode -字符集  	 * @param err -轉換錯誤時返回該文字  	 * @return  	 */  	public static String getString(byte[] s1,String encode,String err){  		try {  			return new String(s1,encode);  		} catch (UnsupportedEncodingException e) {  			return err==null?null:err;  		}  	}  	/**  	 * byte數組轉換為Stirng  	 * @param s1-數組  	 * @param encode-字符集  	 * @return  	 */  	public static String getString(byte[] s1,String encode){  		return getString(s1,encode,null);  	}  	//測試  	public static void main(String []args){  		System.err.println(isEq(new byte[]{1,2},new byte[]{1,2}));  	}  	/**  	 * 位元組數組轉16進位字元串  	 * @param b  	 * @return  	 */  	public static String byteArrToHexString(byte[] b){  		  String result="";  		  for (int i=0; i < b.length; i++) {  		    result += Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring(1);  		  }  		  return result;  	}    	/**  	 * 16進位字元創轉int  	 * @param hexString  	 * @return  	 */  	public static int hexStringToInt(String hexString){  		return Integer.parseInt(hexString,16);  	}  	/**  	 * 十進位轉二進位  	 * @param i  	 * @return  	 */  	public static String intToBinary(int i){  		return Integer.toBinaryString(i);  	}  }