I'm trying to convert a Base64 encoded string to text. I'm using the following code:
public static string Base64Decode(string base64EncodedData)
{
var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
return System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
}
Somehow it does work but it puts whitespaces after each character.Furthermore, it adds an invalid character in the beginning of converted string. The content in Base64 string is an XML so when it converts it to text and puts whitespaces, the XML becomes invalid. Is there any alternative to this?
here's a sample output after conversion:
? < ? x m l v e r s i o n = " 1 . 0 " e n c o d i n g = " U T F - 1 6 " s t a n d a l o n e = " n o " ? > < I m p o r t > < o p t i o n s > < P r o c N a m e > E R P N u m b e r < / P r o c N a m e > < J o b I D > A N L 0 0 1 8 5 0 < / J o b I D > < / o p t i o n s > < R o w > < D o c I d / > < E R P N u m b e r / > < / R o w > < / I m p o r t >
base64EncodedDatais actuallyUTF-16encoded. Try usingSystem.Text.Encoding.UTF16.GetStringinstead.MemoryStream, them useXmlReader.Create(Stream)to parse the XML. I think (but have not checked) that theXmlReaderwill interpret the encoding correctly. Or if there is indeed a BOM, then OP can usenew StreamReader(Stream, true)to detect it.