locked
Need to convert a base64 string to a byte array. Covert.FromBase64String not accepting my string RRS feed

  • Question

  • I am attempting to use the Convert.FromBase64String method to covert a base64 encoded image to a byte array. The method does not support my valid Base64 string though. The system says it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters. The string does decode without issue using the following website:

    http://www.freeformatter.com/base64-encoder.html

    Here is my code including the base64 encoded string.

    byte[] imageBytes = Convert.FromBase64String("iVBORw0KGgoAAAANSUhEUgAAALAAAAA8CAYAAADGxTD9AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwAADqYAAAOpgAABdwnLpRPAAAAAlwSFlzAAAF5wAABecBpOi1+QAABrdJREFUeF7tnGeoJFUQhdecUNesqBjAHBazGH4oCmYxCyomzJhzxpxFRNesC6IYkMWEYkQxYMInBsxrQjHyDJjT90kPDmPP2xmmJ6xzCg49Pa+n7p3zqmvqVtXtceMiYSAMhIEwEAbCQBgIA2EgDISBMBAGwkAYCANhIAyEgTAQBsJAGAgDYSAMhIEwEAbCQBgIA2EgDISBMBAGwkAYCANhIAyEgTAQBsJA1xmYnxEOA4eAFbo+WgYIAxUysAC63gTfgbfA9+AhsCaYrsJxoioMdIWBM9H6J9gWzAlWA1eAL8GxXRkxSsNARQwsgZ6fwC0l+tblvRfBbWB8ReNFTRiolIGJaBsFqzTRamxsOHE3mKnSkaMsDHTIwHJ8/htwdYOepTg/HxhKKIYV9wHDihk6HDMfDwOVMXAymv4AKzZofJzzH8A7YLHibwtznALWr2z0KAoDHTCgJ/0QPNqgYy3O3wPLgq/BWXV/P5vXF3cwZj4aBipjYG00/QV2b9CokV5UvHcdx5Hi9ewc7wHnVTaDKAoDHTCgIX4FlqzTMTOvXwLrFO/twtEQY09gfvhe4KIuEgb6ysD0jP5kgfpF2dK89y6YsZjdwRzND5tmOxfM1tdZZ/AwUDCwEMePC6OsJ2UHTp4GFi/0uHpfcUKYCwODxMAEJvMb2BnojV24nQQsJ+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a6hM84UO3gkpMkVWOTg5HcNED+TOnE4uzLFV+1CKPhx6khO8OAS0zKelrD2yoMz7NTHk3N30bNrx1wh5y/KSYPDTUOLoVwXbkwC7FUE4hiJmKJow9Fm1XL3Mc8Cus+1f9JDKqOAVpPTiEGiH8vuEOMpzzwseineP0/fYuCitmYbg0IKGFUCzoqZMj7aaZsAmO9wCKugLizNkRDwjfs9XrjtKE6iHPB2s1Rflz4zznFaXjpxoLD1IsiWRvllkyj/ngzjv0VMMpDUuAFjB+hoYilSoSk0nxg/caJ4sIMJxGBCQiNa3h2GeHvX/YeSY454zF9evDNaR46gcvs5nLcnOZ3etRipWRFeRfGixJb2Rh/ehGXSQx5t4R3UUskJOs0quyPu0e1eCBawKuRpwAFGxW1QlxksOOjIfBAozYSY5l0JTO1jaANIbhVhoItGvi2PPh65tjBm2QH54H1+kXbdHVyuG69AHxdt8kJVIbj4uX11UdCI3F7ztmAfr6JEBzTVagyzAYU8O7lb0tQk4zlnBBwQvD9rQZ07Ihgnh2KW14wR8N3fHyW0nYcUgSDl63wRcgz5ne9VrygIisKguctYkgkpjRF0ODqFJzHycbrqGO1xTyrfnNAuwgM6evHav4JcFQ2Sy0aLA276ZqVlgli2gRV1j1CIymAxGvv4sK950axZgAQiDfb2gL6mEglKDqFGzQLNAs0CzQLNAs0CzQLNAs0CzQLNAs0CzQLNAs0CzQLNAs0CzQLNAs0CzQLNAs0CzQLNAs0CzQLNAs8D8WeB/JH7pq3/xFKYAAAAASUVORK5CYII=");

    Thursday, March 26, 2015 10:39 PM

Answers

  • Somewhere inside that string you have ==, those are padding characters that can only appear at the end of the string. It looks to me that your base64 string is in fact made of 2 concatenated base64 strings. 2 images perhaps?
    • Proposed as answer by cheong00Editor Friday, March 27, 2015 2:00 AM
    • Marked as answer by rmcproit Monday, March 30, 2015 5:01 PM
    Thursday, March 26, 2015 11:10 PM

All replies

  • Somewhere inside that string you have ==, those are padding characters that can only appear at the end of the string. It looks to me that your base64 string is in fact made of 2 concatenated base64 strings. 2 images perhaps?
    • Proposed as answer by cheong00Editor Friday, March 27, 2015 2:00 AM
    • Marked as answer by rmcproit Monday, March 30, 2015 5:01 PM
    Thursday, March 26, 2015 11:10 PM
  • Thursday, March 26, 2015 11:30 PM
  • I guess the online decoder allows for this. It turned out that the image was in the first part of the string and the rest was garbage. The data gets stored with a javascript function and it appears when inserting the data into a javascript variable the data was jumbled without reason. The original data received never contained anything after the first ==. This only happened less than 1% of the time. I also found that javascript could not read the size of the string properly with this specific data when inserting the data into a string and outputting the length. Anyways it is not a problem with the method, thanks for the heads up about the ==.
    Monday, March 30, 2015 5:01 PM
  • Beware that having those == in the middle is a fortunate situation.  A base64 string can end with 2, 1 or 0 equal characters. The case of 0 equal characters can be problematic since there's no way to detect that the string is actually made of 2 separate base64 strings. In that case the whole thing will decode correctly but the resulting binary image data will likely contain some garbage at the end. I'm not 100% sure but I think that image decoders tend to ignore such garbage.
    Tuesday, March 31, 2015 7:04 AM