Throws a System.FormatException saying that the length is not a valid base 64 string. Var elements = (var element in elements)īyte image = Convert.FromBase64String(element.Value) Īfter downloading the XML file and analyzing the value of the media-object node it is clear that it is not a valid base64 string: string value = "PUT HERE THE BASE64 STRING FROM THE XML WITHOUT THE NEW LINES" īyte image = Convert.FromBase64String(value) Better would be some kind of throw new Exception() Regardless what (or what not) you attach to your string! This would always produce an exception!! Int asciiChars = temp.Length - temp.Count(c => Char.IsWhiteSpace(c)) String temp = base64Decoded.TrimEnd('=') Static byte AnotherDecode64(string base64Decoded) Var image = AnotherDecode64(element.Value) Var elements = (XElement element in elements) Just for the bounty, here is my code (not absolute perfect, but enough for a good starting point): -) static void Main(string args) So if you fill up the already existing one with additional equal signs you get an error too! To get this damn thing to work, you have to cut off all existing signs, calculate how much are needed and add them again. Notepad++ has no problem with tons of equal signs, but the Convert function from MS only works with zero, one or two signs. Further investigationsĪfter some coding and reading of the convert function, the problem is a wrong attaching of a equal sign from the producer. The better way would be to count the number of characters (minus CR/LFs!) and add the needed ones in one step. The 'easy' way would be to add the equal sign till the decoding doesn't throw an error. They just don't have the right numbers of equal signs at the end to fill up to a number of signs which can be break into packets of 4. So i would say, there IS something wrong with the data you'll get. jpg and it opens like a charm in any picture viewer. So i just added two equal signs ('=') as placeholder at the end to get the correct length.Īnother retry and it decoded successfully into 'something'. This threw an error about the wrong text length (must be mod 4). I opened the xml file within and copy and pasted the raw base64 content into a new file (without square brackets).Īfterwards I selected everything (Strg-A) and used the option Extensions - Mime Tools - Base64 decode. PLease note the downloadable file is in a bit different schema, since I simplified it in the above example, removing irrelevant stuff.įor a first shot i didn't use any programming language, just Notepad++ Help! Edit:Īdded an actual example file (and a bounty). I have been googling, binging, stackoverflowing and crying - and found no solution. I have tried writing out the contents of mediaObjectNode.InnerText, and it is the base64 encoded data - so the navigating the xml-doc is not the issue. The xml-data is from an enterprise newspaper system, so I am pretty sure the files are ok - and there must be something in the way I process them, that is just wrong. Do stuff with the bytearray to save the image The line below is where the errors occurīyte imageBytes = Convert.FromBase64String(mediaObjectNode.InnerText) Var mediaObjectNode = node.SelectSingleNode("media-object") Var nodes = xd.GetElementsByTagName("media") I marked the line in the code where I get the error in the code.Īnd the code to process like this: var xd = new XmlDocument() However, I get errors like "Invalid length for a Base-64 char array" and "Invalid character in a Base-64 string". Alternatively you can use the keyboard bindings CMD-ALT-C and CTRL-ALT-C for Mac & PC respectively.I receive some xml-files with embedded base64-encoded images, that I need to decode and save as files.Īn unmodified (other than zipped) example of such a file can be downloaded below: To active the command simply launch the command palette ( Shift-CMD-P on OSX or Shift-Ctrl-P on Windows and Linux), then just type Encode/Decode: Convert Selection, then a menu of possible conversions will be displayed. The extension provides a single command to the command palette. String to MD5/SHA1/SHA256/SHA512 (as Base64 or Hex).The Encode/Decode (ecdc) extension allows you to quickly convert one or more selections of text to and from various formats.
0 Comments
Leave a Reply. |