An efficient way to Base64 encode a byte array?
我有一个
问题是,内置的.NET方法
还有更直接的方法吗?
[[edit:]我会更好地解释我想要实现的内容-我有一个
字节[->字符串:使用system.convert.tobase64字符串
1 | Convert.ToBase64String(byte[] data) |
字符串->字节[]:使用System.Convert.FromBase64字符串
1 | Convert.FromBase64String(string data) |
base64是一种以文本形式(字符串)表示字节的方法。所以不存在base64编码的字节[]。你会得到一个base64编码的字符串,你可以把它解码回一个
但是,如果希望以字节数组结束,可以获取base64编码的字符串并将其转换为字节数组,例如:
1 2 | string base64String = Convert.ToBase64String(bytes); byte[] stringBytes = Encoding.ASCII.GetBytes(base64String); |
但是,这没有意义,因为将字节[]表示为字节[]的最佳方法是字节[]本身:)
下面是base64的代码直接编码到字节数组(测试为执行.NET实现的+-10%,但分配一半内存):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | 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; } |
1 | byte[] base64EncodedStringBytes = Encoding.ASCII.GetBytes(Convert.ToBase64String(binaryData)) |
基于您的编辑和评论..这就是你想要的吗?
1 | byte[] newByteArray = UTF8Encoding.UTF8.GetBytes(Convert.ToBase64String(currentByteArray)); |
可以使用字符串convert.to base64 string(byte[])将字节数组编码为base64字符串,然后使用byte[]convert.frombase64string(string)将生成的字符串转换回字节数组。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | 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(); } |