How to decode a base64 encoded string containing an XML document containing accented characters (á, é, í, ó, ú) in C #?

How to decode a base64 encoded string containing an XML document that contains latin letters (á,é,í,ó,ú)

?

I know this question How to encode and decode a base64 string? But the solutions provided do not work well with accented letters.

So far I have tried:

xmlBase64 = System.Text.Encoding.ASCII.GetString(System.Convert.FromBase64String(XmlDoc));
xmlBase64 = System.Text.Encoding.Unicode.GetString(System.Convert.FromBase64String(XmlDoc));
xmlBase64 = System.Text.Encoding.UTF8.GetString(System.Convert.FromBase64String(XmlDoc));
xmlBase64 = System.Text.Encoding.UTF32.GetString(System.Convert.FromBase64String(XmlDoc));

      

But in all cases, Latin letters (Spanish characters) are replaced with ?

or similars.

EDIT:

This is a base64 encoded string

This is the Decoded string

+3


source to share


1 answer


It is helpful to see the bytes generated System.Convert.FromBase64String(XmlDoc)

.

I did this and took a look at the word "metálicas" in your original line (it was only the first word I found with an accent). This part of the string is converted to a byte array 6D 65 74 E1 6C 69 63 61 73

.

It's easy to see two things from this byte array:



  • This is a single byte encoding
  • It's not UTF-8: in UTF-8, bytes larger than 7F never appear on their own, always in groups of 2-4.

From there I assumed it would be some form of extended ASCII, Windows-1252 seems to work. Try the following:

xmlBase64 = System.Text.Encoding.GetEncoding(1252).GetString(System.Convert.FromBase64String(XmlDoc));

      

+2


source







All Articles