An efficient way to Base64 encode a byte array?

前端 未结 8 1838
再見小時候
再見小時候 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:46

    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 decode back to a byte[].

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

    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 :)

提交回复
热议问题