http://yk3288.blog.hexun.com/43207862_d.html
http://ilewen.com/questions/821
前不久,写了个小程序,主要是使用Java对数字图像进行处理,里面有许多操作都需要进行byte数组到int型数据的相互转换,最初想的到的方法自然是使用移位的方法,可是这么写好像有点不好理解,而其不是很美观,后来在高人的指导下,使用了Java中的一个流对象,感觉十分好用,特贴出来和同志们分享分享。
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; /** * @author yk3288 * */ public class Test { public static void main(String[] args) throws IOException{ System.out.println("application is start"); Test t = new Test(); t.converIn(); t.converStream(); System.out.println("convert is over!"); } /** * 用移位吧,代码如下,因为int型式4个字节,byte为1个字节,一个字节8位 * 当int型转为byte型时,只截取int型的最后一个字节的数据,因此依次移位0,8,16,24就可以依次将各字节拷贝到byte类型的数组中 * 使用移位的方式进行转换 */ public void converIn(){ Byte[] b = new Byte[]{0,36,0,54}; //左移位 int i = b[3].intValue() | b[2].intValue() << 8 | b[1].intValue() << 16 | b[0].intValue() << 24; System.out.println(i); //右移位 byte[] b1 = new byte[]{ (byte)(i >> 24), (byte)(i >> 16), (byte)(i >> 8), (byte)(i >> 0), }; output(b1); } /** * 使用Java中流的操作进行转换 * * @throws IOException */ public void converStream() throws IOException{ DataInputStream dis = new DataInputStream( new ByteArrayInputStream(new byte[]{0,36,0,54})); int i = dis.readInt(); System.out.println(i); ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(baos); dos.writeInt(i); byte[] b = baos.toByteArray(); output(b); } /** * 输出byte数组 * * @param b */ public void output(byte[] b){ for(int i = 0;i < b.length;i++){ System.out.print(b[i] + "/" ); } System.out.println(); } }