.NET是否有Base64Stream? [英] Is there a Base64Stream for .NET?
问题描述
如果我想产生Base64编码的输出,我该如何在.NET中做到这一点?
If I want to produce a Base64-encoded output, how would I do that in .NET?
我知道从.NET 2.0开始,< a href = http://msdn.microsoft.com/en-us/library/system.security.cryptography.icryptotransform.aspx rel = noreferrer> ICryptoTransform 接口和
< a href = http://msdn.microsoft.com/zh-cn/library/system.security.cryptography.tobase64transform.aspx rel = noreferrer> ToBase64Transform()和 FromBase64Transform()实现。
I know that since .NET 2.0, there is the ICryptoTransform interface, and the ToBase64Transform() and FromBase64Transform() implementations of that interface.
但是这些类已嵌入到System.Security命名空间中,并且需要使用TransformBlock,TransformFinalBlock等。
But those classes are embedded into the System.Security namespace, and require the use of a TransformBlock, TransformFinalBlock, and so on.
是否有更简单的方法来对.NET中的数据流进行base64编码?
Is there an easier way to base64 encode a stream of data in .NET?
推荐答案
如果要转换为Base64的流,可以将 ToBase64Transform
放入 CryptoStream
:
If you want a stream that converts to Base64, you can put a ToBase64Transform
into a CryptoStream
:
new CryptoStream(stream, new ToBase64Transform(), CryptoStreamMode.Write)
如果仅要将单字节数组转换为Base64,则只需调用 Convert.ToBase64String(bytes)
。
If you just want to convert a single byte array to Base64, you can simply call Convert.ToBase64String(bytes)
.
在这两种情况下,都可以将 To
替换为 From
。
In both cases, you can replace the word To
with From
.
这篇关于.NET是否有Base64Stream?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!