An efficient way to Base64 encode a byte array?

前端 未结 8 1848
再見小時候
再見小時候 2020-11-30 07:18

I have a byte[] and I\'m looking for the most efficient way to base64 encode it.

The problem is that the built in .Net method Convert.FromBase64

8条回答
  •  半阙折子戏
    2020-11-30 07:42

    Here is the code to base64 encode directly to byte array (tested to be performing +-10% of .Net Implementation, but allocates half the memory):

        static public void testBase64EncodeToBuffer()
        {
            for (int i = 1; i < 200; ++i)
            {
                // prep test data
                byte[] testData = new byte[i];
                for (int j = 0; j < i; ++j)
                    testData[j] = (byte)(j ^ i);
    
                // test
                testBase64(testData);
            }
        }
    
        static void testBase64(byte[] data)
        {
            if (!appendBase64(data, 0, data.Length, false).SequenceEqual(System.Text.Encoding.ASCII.GetBytes(Convert.ToBase64String(data)))) throw new Exception("Base 64 encoding failed");
        }
    
        static public byte[] appendBase64(byte[] data
                                  , int offset
                                  , int size
                                  , bool addLineBreaks = false)
        {
            byte[] buffer;
            int bufferPos = 0;
            int requiredSize = (4 * ((size + 2) / 3));
            // size/76*2 for 2 line break characters    
            if (addLineBreaks) requiredSize += requiredSize + (requiredSize / 38);
    
            buffer = new byte[requiredSize];
    
            UInt32 octet_a;
            UInt32 octet_b;
            UInt32 octet_c;
            UInt32 triple;
            int lineCount = 0;
            int sizeMod = size - (size % 3);
            // adding all data triplets
            for (; offset < sizeMod;)
            {
                octet_a = data[offset++];
                octet_b = data[offset++];
                octet_c = data[offset++];
    
                triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c;
    
                buffer[bufferPos++] = base64EncodingTable[(triple >> 3 * 6) & 0x3F];
                buffer[bufferPos++] = base64EncodingTable[(triple >> 2 * 6) & 0x3F];
                buffer[bufferPos++] = base64EncodingTable[(triple >> 1 * 6) & 0x3F];
                buffer[bufferPos++] = base64EncodingTable[(triple >> 0 * 6) & 0x3F];
                if (addLineBreaks)
                {
                    if (++lineCount == 19)
                    {
                        buffer[bufferPos++] = 13;
                        buffer[bufferPos++] = 10;
                        lineCount = 0;
                    }
                }
            }
    
            // last bytes
            if (sizeMod < size)
            {
                octet_a = offset < size ? data[offset++] : (UInt32)0;
                octet_b = offset < size ? data[offset++] : (UInt32)0;
                octet_c = (UInt32)0; // last character is definitely padded
    
                triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c;
    
                buffer[bufferPos++] = base64EncodingTable[(triple >> 3 * 6) & 0x3F];
                buffer[bufferPos++] = base64EncodingTable[(triple >> 2 * 6) & 0x3F];
                buffer[bufferPos++] = base64EncodingTable[(triple >> 1 * 6) & 0x3F];
                buffer[bufferPos++] = base64EncodingTable[(triple >> 0 * 6) & 0x3F];
    
                // add padding '='
                sizeMod = size % 3;
                // last character is definitely padded
                buffer[bufferPos - 1] = (byte)'=';
                if (sizeMod == 1) buffer[bufferPos - 2] = (byte)'=';
            }
            return buffer;
        }
    

提交回复
热议问题