C# Base64 编码字节数组的有效方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12178495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 22:22:19  来源:igfitidea点击:

An efficient way to Base64 encode a byte array?

c#.net

提问by sternr

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

我有一个byte[],我正在寻找对它进行 base64 编码的最有效方法。

The problem is that the built in .Net method Convert.FromBase64CharArrayrequires a char[]as an input, and converting my byte[]to a char[]just to convert it again to a base64 encoded array seems pretty stupid.

问题是内置的 .Net 方法Convert.FromBase64CharArray需要 achar[]作为输入,而将 my 转换byte[]为 achar[]只是为了将其再次转换为 base64 编码数组似乎很愚蠢。

Is there any more direct way to do it?

有没有更直接的方法来做到这一点?

[[EDIT:]] I'll expaling what I want to acheive better - I have a byte[]and I need to return a new base64 encoded byte[]

[[编辑:]]我会解释我想要更好的实现 - 我有一个byte[],我需要返回一个新的base64编码byte[]

采纳答案by Amit Bens

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

下面是将 base64 直接编码为字节数组的代码(测试为执行 +-10% 的 .Net 实现,但分配了一半的内存):

    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;
    }

回答by Stephan Schinkel

Byte[] -> String:use system.convert.tobase64string

字节[] -> 字符串:使用 system.convert.tobase64string

Convert.ToBase64String(byte[] data)

String -> Byte[]:use system.convert.frombase64string

字符串 -> 字节 []:使用 system.convert.frombase64string

Convert.FromBase64String(string data)

回答by Eren Ers?nmez

Base64 is a way to represent bytes in a textual form (as a string). So there is no such thing as a Base64 encoded byte[]. You'd have a base64 encoded string, which you could decodeback to a byte[].

Base64 是一种以文本形式(作为字符串)表示字节的方法。所以没有 Base64 编码的 byte[] 这样的东西。您将有一个base64 编码的 string,您可以解码byte[].

However, if you wantto end up with a byte array, you could take the base64 encoded string and convert it to a byte array, like:

但是,如果你想得到一个字节数组,你可以使用 base64 编码的字符串并将其转换为一个字节数组,例如:

string base64String = Convert.ToBase64String(bytes);
byte[] stringBytes = Encoding.ASCII.GetBytes(base64String);

This, however, makes no sense because the best way to represent a byte[] as a byte[], is the byte[] itself :)

然而,这是没有意义的,因为将 byte[] 表示为 byte[] 的最佳方式是 byte[] 本身:)

回答by Midrel

You could use the String Convert.ToBase64String(byte[]) to encode the byte array into a base64 string, then Byte[] Convert.FromBase64String(string) to convert the resulting string back into a byte array.

您可以使用 String Convert.ToBase64String(byte[]) 将字节数组编码为 base64 字符串,然后使用 Byte[] Convert.FromBase64String(string) 将结果字符串转换回字节数组。

回答by Simon Whitehead

Based on your edit and comments.. would this be what you're after?

根据您的编辑和评论……这就是您所追求的吗?

byte[] newByteArray = UTF8Encoding.UTF8.GetBytes(Convert.ToBase64String(currentByteArray));

回答by BlueM

byte[] base64EncodedStringBytes = Encoding.ASCII.GetBytes(Convert.ToBase64String(binaryData))

回答by Debashis Nandi

    public void ProcessRequest(HttpContext context)
    {
        string constring = ConfigurationManager.ConnectionStrings["SQL_Connection_String"].ConnectionString;
        SqlConnection conn = new SqlConnection(constring);
        conn.Open();
        SqlCommand cmd = new SqlCommand("select image1 from TestGo where TestId=1", conn);
        SqlDataReader dr = cmd.ExecuteReader();
        dr.Read();
        MemoryStream str = new MemoryStream();

        context.Response.Clear();
        Byte[] bytes = (Byte[])dr[0];
        string d = System.Text.Encoding.Default.GetString(bytes);
        byte[] bytes2 = Convert.FromBase64String(d);
        //context.Response.Write(d);
        Image img = Image.FromStream(new MemoryStream(bytes2));
        img.Save(context.Response.OutputStream, ImageFormat.Png);
        context.Response.Flush();
        str.WriteTo(context.Response.OutputStream);
        str.Dispose();
        str.Close();
        conn.Close();
        context.Response.End();
    }