C# Convert a Base64 -> byte[]

C#Base64

C# Problem Overview


I have a Base64 byte[] array which is transferred from a stream which i need to convert it to a normal byte[] how to do this ?

C# Solutions


Solution 1 - C#

You have to use Convert.FromBase64String to turn a Base64 encoded string into a byte[].

Solution 2 - C#

This may be helpful

byte[] bytes = System.Convert.FromBase64String(stringInBase64);

Solution 3 - C#

Try

byte[] incomingByteArray = receive...; // This is your Base64-encoded bute[]

byte[] decodedByteArray =Convert.FromBase64String (Encoding.ASCII.GetString (incomingByteArray)); 
// This work because all Base64-encoding is done with pure ASCII characters

Solution 4 - C#

You're looking for the FromBase64Transform class, used with the CryptoStream class.

If you have a string, you can also call Convert.FromBase64String.

Solution 5 - C#

I've written an extension method for this purpose:

public static byte[] FromBase64Bytes(this byte[] base64Bytes)
{
	string base64String = Encoding.UTF8.GetString(base64Bytes, 0, base64Bytes.Length);
	return Convert.FromBase64String(base64String);
}

Call it like this:

byte[] base64Bytes = .......
byte[] regularBytes = base64Bytes.FromBase64Bytes();

I hope it helps someone.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSudantha View Question on Stackoverflow
Solution 1 - C#ZrutyView Answer on Stackoverflow
Solution 2 - C#Selim RezaView Answer on Stackoverflow
Solution 3 - C#YahiaView Answer on Stackoverflow
Solution 4 - C#SLaksView Answer on Stackoverflow
Solution 5 - C#DucoView Answer on Stackoverflow