public static int ByteArrayToint(byte[] b)
    {
        //byte[] b=new byte[]{1,2,3,4}; 
        int mask  = 0xff;
        int temp  = 0;
        int res   = 0;
        for(int i=0;i<4;i++)
        {
            res<<=8;
            temp  = b[i]&mask;
            res  |= temp;
        }
        return res;
    }
大端可以这样转化小端应该怎么做?

解决方案 »

  1.   


    public class TestByte {
    public static void main(String args[]) {
    byte[] b=TestByte.getbytes("01000000");

    ByteBuffer bb = ByteBuffer.wrap(b);
    int temp = bb.getInt();
    System.out.println(temp);

    System.out.println(Integer.reverseBytes(temp)); }

    // 16进制字符串转byte[]
    public static byte[] getbytes(String data) {
    int len = data.length();
    byte[] ba = new byte[len / 2];
    int i = 0, j = 0, c;
    while (i < len) {
    c = Character.digit(data.charAt(i++), 16) << 4;
    c = c + Character.digit(data.charAt(i++), 16);
    ba[j++] = (byte) c;
    }
    return ba;
    }