For the first shot, I did not use any programming language, just Notepad ++
I opened the xml file inside and copied and pasted the original base64 content into a new file (without square brackets).
Then I selected everything (Strg-A) and used the Extensions - Mime Tools - Base64 decode option. This gave rise to an error about the wrong length of the text (should be mod 4). So I just added two equal signs ('=') as a placeholder at the end to get the correct length.
Try again and it is successfully decoded into "something." Just save the file as .jpg and it opens like a charm in any image viewer application.
So, I would say that something is wrong with the data you receive. They simply do not have the right number of equal characters at the end to fill in a series of characters that can be broken into packets of 4.
An “easy” way would be to add an equal sign until decoding produces an error. It would be best to count the number of characters (minus CR / LFs!) And add the needed ones in one step.
Further research
After some coding and reading the conversion function, the problem is the incorrect attachment of the equal sign from the manufacturer. Notepad ++ has no problems with tons of identical characters, but MS's Convert function works only from scratch, with one or two characters. Therefore, if you fill out an existing one with additional equal signs, you will also get an error! To make this damn job work, you need to turn off all existing signs, calculate how much is needed and add them again.
For generosity only, here is my code (not absolute, but enough for a good starting point) :; -)
static void Main(string[] args) { var elements = XElement .Load("test.xml") .XPathSelectElements("//media/media-object[@encoding='base64']"); foreach (XElement element in elements) { var image = AnotherDecode64(element.Value); } } static byte[] AnotherDecode64(string base64Decoded) { string temp = base64Decoded.TrimEnd('='); int asciiChars = temp.Length - temp.Count(c => Char.IsWhiteSpace(c)); switch (asciiChars % 4) { case 1:
source share