"Base64 Convert" is broken
I use Base64 converter to decode a base64-encoded string. When I encode the result, it differs from the original. Sample code below.
//Base64: codeunit "Base64 Convert"; Base64String := 'iVBORw0KGgoAAAANSUhEUgAAAAUAAAAHCAIAAABPxRC5AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAAEnQAABJ0Ad5mH3gAAAAgSURBVBhXY/iPCkB8BhhA8EFSYECkPIQDBAgWCPz/DwAmK02zB8YDEgAAAABJRU5ErkJggg=='; Message(Base64String); lTxtType := base64.FromBase64(Base64String); message(base64.ToBase64(lTxtType));
The latter message gives us: 77+9UE5HDQoaCgAAAA1JSERSAAAABQAAAAcIAgAAAE/vv70Q77+9AAAAAXNSR0IA77+977+9HO+/vQAAAARnQU1BAADvv73vv70L77+9YQUAAAAJcEhZcwAAEnQAABJ0Ae+/vWYfeAAAACBJREFUGFdj77+977+9CkB8BhhA77+9QVJgQO+/vTzvv70DBAgWCO+/ve+/vQ8AJitN77+9B++/vQMSAAAAAElFTkTvv71CYO+/vQ==
BC 15.2
As a further clarification, we want to write the base64 string as binary data into a MediaSet, but I have not found a way to do this with the very limited toolset (dotnet wrapper codeunits) available to us.
@JesperSchulz
Anything ?
I've looked into our Base64 module and it looks as if it's sufficiently tested: https://github.com/microsoft/ALAppExtensions/blob/master/Modules/System%20Tests/Base64%20Convert/src/Base64ConvertTest.Codeunit.al
In that test codeunit, you will also find the following test, which kind of does the same as you are doing:
[Test]
procedure ToAndFromBase64Test()
var
ConvertedText: Text;
ConvertedBackText: Text;
begin
// [SCENARIO] The convertion to base-64 string and then from it back results in the initial string.
// [WHEN] The string is converted to base-64
ConvertedText := Base64Convert.ToBase64(SampleTxt);
// [WHEN] The string is converted back from base-64
ConvertedBackText := Base64Convert.FromBase64(ConvertedText);
// [THEN] The value is the same as the initial one
Assert.AreEqual(SampleTxt, ConvertedBackText, 'The converted back value is not the same as the initial one.');
end;
We take a string, Base64 convert it and covert it back, which yields the same result. I would hence claim, that the implementation isn't broken. But we'll look into your specific issue to see what's going on there.
Good day and thanks for your reply.
Yes, converting to base64 and then back does seem to work, at least for a non-binary string, but converting from 64 to binary and then back does not. If this relates to how text variables work and is impossible to do, we would appreciate help in how to write the binary representation of the base64 string into a mediaset.
@navdotnetreqs can you try using the overloads of ToBase64 and FromBase64 that accep streams (InStream and OutStream respectively)?
This are the functions that are supposed to be used for binary content.
Hi and thanks for your reply.
Using FromBase64(Text,OutStream) I managed to solve the problem in question by writing the base64 data to a blob field in a temporary record (company information picture), and then creating an instream to the same blob, and then using mediaset.ImportStream.
Thank you for your help.
I am experiencing the same issue. So the TempBlob table was removed... and to achieve the same functionality as before we need to "abuse" another table with a blob field to mimic the old TempBlob behaviour. This sounds like a bad joke.
Nevermind, actually simple... if you know how:
procedure ImportLabelFromBase64(Base64Text: Text; Description: Text)
var
TempBlob: Codeunit "Temp Blob";
Base64Convert: Codeunit "Base64 Convert";
LabelInStream: InStream;
BlobOutStream: OutStream;
begin
TempBlob.CreateOutStream(BlobOutStream);
Base64Convert.FromBase64(Base64Text, BlobOutStream);
TempBlob.CreateInStream(LabelInStream);
Label.ImportStream(LabelInStream, Description);
end;