1. 程式人生 > >java int型別轉byte陣列

java int型別轉byte陣列

這裡只寫了幾個 轉換程式     主要是java和其他語言互動時的大小端問題    

//高前低後
	public static byte[] intToByteArray( int integer) {
		int byteNum = (40 -Integer.numberOfLeadingZeros (integer < 0 ? ~integer : integer))/ 8;
		byte[] byteArray = new byte[4];

		for (int n = 0; n < byteNum; n++)
		byteArray[3 - n] = (byte) (integer>>> (n * 8));

		return byteArray;
		}
	/**
	 * 將int轉為低位元組在前,高位元組在後的byte陣列
	 */
	public static byte[] tolh(int n) {
		byte[] b = new byte[4];
		b[0] = (byte) (n & 0xff);
		b[1] = (byte) (n >> 8 & 0xff);
		b[2] = (byte) (n >> 16 & 0xff);
		b[3] = (byte) (n >> 24 & 0xff);
		return b;
	}
	/**  
	  * 將int轉為高位元組在前,低位元組在後的byte陣列  
	  * @param n int  
	  * @return byte[]  
	  */  
	public static byte[] toHH(int n) {  
	  byte[] b = new byte[4];  
	  b[3] = (byte) (n & 0xff);  
	  b[2] = (byte) (n >> 8 & 0xff);  
	  b[1] = (byte) (n >> 16 & 0xff);  
	  b[0] = (byte) (n >> 24 & 0xff);  
	  return b;  
	}   
	/**
	 * 將byte陣列轉化成String
	 */
	public static String toStr(byte[] valArr,int maxLen) {
		int index = 0;
		while(index < valArr.length && index < maxLen) {
			if(valArr[index] == 0) {
				break;
			}
			index++;
		}
		byte[] temp = new byte[index];
		System.arraycopy(valArr, 0, temp, 0, index);
		return new String(temp);
	}
	
	/**
	 * 將低位元組在前轉為int,高位元組在後的byte陣列
	 */
	public static int vtolh(byte[] bArr) {
		int n = 0;
		for(int i=0;i<bArr.length&&i<4;i++){
			int left = i*8;
			n+= (bArr[i] << left);
		}
		return n;
	}
	

//高前低後
	public static int bytesToInt2(byte[] src, int offset) {  
	    int value;    
	    value = (int) ( ((src[offset] & 0xFF)<<24)  
	            |((src[offset+1] & 0xFF)<<16)  
	            |((src[offset+2] & 0xFF)<<8)  
	            |(src[offset+3] & 0xFF));  
	    return value;  
	}  


全部轉換可參考 :http://blog.csdn.net/u011068702/article/details/51857557