美文网首页
java byte[] 转 int

java byte[] 转 int

作者: 爱哭的笨小孩 | 来源:发表于2023-04-08 01:54 被阅读0次
        byte[] bytes = new byte[]{(byte) 19, (byte) 247, (byte) 11};

        System.out.println("for循环:");
        int iooo = 0;
        for (int j = 0; j < bytes.length; j++) {
            iooo = iooo | (bytes[j] & 0xFF) << (bytes.length - j - 1) * 8;
        }
        System.out.println(iooo);
        System.out.println(Integer.toBinaryString(iooo));

        System.out.println("拼接:");
        int i1 = (bytes[0] & 0xFF) << 16 | (bytes[1] & 0xFF) << 8 | bytes[2] & 0xFF;
        System.out.println(i1);
        System.out.println(Integer.toBinaryString(i1));

        System.out.println("BigInteger:");
        BigInteger bigInteger = new BigInteger(1, bytes);
        System.out.println(bigInteger.toString(10));
        System.out.println(bigInteger.toString(2));

输出:

for循环:
1308427
100111111011100001011
拼接:
1308427
100111111011100001011
BigInteger:
1308427
100111111011100001011

相关文章

网友评论

      本文标题:java byte[] 转 int

      本文链接:https://www.haomeiwen.com/subject/wfmmddtx.html