ITeye博客

byte int的转换

windywindy · 2010-06-13 16:00 · java基础 · 编程语言 · Markdown

public static byte[] int2Byte(int temp){
		byte b[] = new byte[4];
		for (int i = b.length-1; i >= 0; i--) {
			b[i] = new Integer(temp & 0xff).byteValue();
			temp = temp >> 8;
		}
		return b;
	}

public static int byte2Int(byte[] b){
		int temp=0;
		for(int i=0;i<3;i++){
			if(b[i]<0){
				temp+=(b[i]+256);
			}else{
				temp+=b[i];
			}
			temp*=256;
		}
		temp+=b[3];
		if(b[3]<0){
			temp+=256;
		}
		return temp;
	}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。