简体   繁体   English

为什么base64编码Java代码这样做

[英]why is the base64 encode java code doing this

So I'm trying to understand base64 encoding better and I came across this implementation on wikipedia 所以我试图更好地理解base64编码,并且在Wikipedia上遇到了这种实现

private static String base64Encode(byte[] in)       {
        StringBuffer out = new StringBuffer((in.length * 4) / 3);
        int b;
        for (int i = 0; i < in.length; i += 3)  {
            b = (in[i] & 0xFC) >> 2;
            out.append(codes.charAt(b));
            b = (in[i] & 0x03) << 4;
            if (i + 1 < in.length)      {
                b |= (in[i + 1] & 0xF0) >> 4;
                out.append(codes.charAt(b));
                b = (in[i + 1] & 0x0F) << 2;
                if (i + 2 < in.length)  {
                    b |= (in[i + 2] & 0xC0) >> 6;
                    out.append(codes.charAt(b));
                    b = in[i + 2] & 0x3F;
                    out.append(codes.charAt(b));
                } else  {
                    out.append(codes.charAt(b));
                    out.append('=');
                }
            } else      {
                out.append(codes.charAt(b));
                out.append("==");
            }
        }

        return out.toString();
    }

And I'm following along and I get to the line: 我一直在跟着,然后我就上线了:

b = (in[i] & 0xFC) >> 2;

and I don't get it...why would you bitwise and 252 to a number then shift it right 2 ...wouldn't it be the same if you just shifted the byte itself without doing the bitwise operation? 但我不明白...您为什么bitwise252为一个数字然后right 2 ...如果不执行按位运算就直接移动字节本身,会不会一样? example: 例:

b = in[i] >> 2; 

Say my in[i] was the letter e ...represented as 101 or in binary 01100101 . 假设我的in[i]letter e ...,表示为101binary 01100101 If I shift that 2 to the right I get 011001 or 25 . 如果将那个2右移,我得到01100125 If I bitwise & it I get 如果我按位&它得到

01100101
11111100
--------
01100100

but then the shift is going to chop off the last 2 anyway...so why bother doing it? 但是后来这个转变还是要砍掉最后2个...所以为什么还要这么做呢?

Can somebody clarify for me please. 有人可以帮我澄清一下。 Thanks. 谢谢。

IN in[i] >> 2 , in[i] is converted to an int first. IN in[i] >> 2 ,首先将in[i]转换为int If it was a negative byte (with the high bit set) it will be converted to a negative int (with the now-highest 24 bits set as well). 如果它是一个负字节(设置了高位),它将被转换为负整数(也设置了现在的最高24位)。

In (in[i] & 0xFC) >> 2 , in[i] is converted to an int as above, and then & 0xFC makes sure the extra bits are all reset to 0. (in[i] & 0xFC) >> 2 ,将in[i]如上所述转换为int ,然后& 0xFC确保多余的位都被重置为0。

You're partially right, in that (in[i] & 0xFF) >> 2 would give the same result. 您说的很对,因为(in[i] & 0xFF) >> 2会得到相同的结果。 & 0xFF is a common way to convert a byte to a non-negative int in the range 0 to 255. & 0xFF是将字节转换为0到255范围内的非负int一种常用方法。

The only way to know for sure why the original developer used 0xFC , and not 0xFF , is to ask them - but I speculate that it's to make it more obvious which bits are being used. 确切知道为什么原始开发人员为什么使用0xFC而不是0xFF的唯一方法是询问他们-但是我推测这是为了让人们更清楚地知道正在使用哪些位。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM