Page 1 of 2
X# Substitutions for EncodeBase64 and DecodeBase64
Posted: Mon Sep 14, 2020 4:00 pm
by ArneOrtlinghaus
In VO we have used the Runtime functions EncodeBase64 and DecodeBase64 together with temporary files to convert strings to and from Base64. I would like to substitute them by functions without file access. Can somebody suggest me some code?
Arne
X# Substitutions for EncodeBase64 and DecodeBase64
Posted: Mon Sep 14, 2020 4:02 pm
by robert
X# Substitutions for EncodeBase64 and DecodeBase64
Posted: Tue Sep 15, 2020 4:08 am
by wriedmann
Hi Arne,
this is was I'm using:
Code: Select all
public static method Base64Encode( cPlainText as string ) as string
local aBytes as byte[]
local cReturn as string
aBytes := System.Text.Encoding.UTF8:GetBytes( cPlainText )
cReturn := System.Convert.ToBase64String( aBytes )
return cReturn
public static method Base64Decode( cBase64Encoded as string ) as string
local aBytes as byte[]
local cReturn as string
aBytes := System.Convert.FromBase64String( cBase64Encoded )
cReturn := System.Text.Encoding.UTF8:GetString( aBytes )
return cReturn
But you have to make sure the string you are coding/decoding is really UTF8. If you are coding binary content, you should not use these functions.
Wolfgang
X# Substitutions for EncodeBase64 and DecodeBase64
Posted: Tue Sep 15, 2020 12:52 pm
by ArneOrtlinghaus
Thanks, it works.
Base64Decode can generate exceptions in case of undesired characters or missing parts of the text. The VO procedure did sometimes generate an "invalid handle" exception and I did not understand why. So if you have to decode email contents, it is worth verifying this.
(I remember some very enthusiastic programmers on VO/Vulcan conferences 10 years ago who told us, that Dotnet programs can never crash in contrast to VO programs)
X# Substitutions for EncodeBase64 and DecodeBase64
Posted: Tue Sep 15, 2020 1:57 pm
by wriedmann
Hi Arne,
(I remember some very enthusiastic programmers on VO/Vulcan conferences 10 years ago who told us, that Dotnet programs can never crash in contrast to VO programs)
personally, I find the .NET exception handling much more confusing and much more work than the one in VO.
The most interesting approach I have seen until now is the one Robert has used in the RDD - basically using lambda expressions inside try-catch blocks.
Wolfgang
X# Substitutions for EncodeBase64 and DecodeBase64
Posted: Tue Sep 15, 2020 2:08 pm
by robert
Wolfgang,
wriedmann wrote:
The most interesting approach I have seen until now is the one Robert has used in the RDD - basically using lambda expressions inside try-catch blocks.
I did that to standardize the error handling and to make it easier to change/extend this error handling.
And it also makes the "normal" code easier to read since the error handling is not visible everywhere.
The disadvantage is that when debugging you have to be very careful when you use step in and when you use step over. You have to step into the method that evaluates the Lambda expression.
And finally: this only works for "normal" parameters. You can't pass parameters by reference to a lambda expression.
For now I have expanded the whole code in the method. Of course I could have (and should have) used a local and pass that by reference and assign it back after the call to the function that evaluates the lambda expression.
Robert
X# Substitutions for EncodeBase64 and DecodeBase64
Posted: Tue Sep 15, 2020 2:12 pm
by ArneOrtlinghaus
Hi Wolfgang,
I have seen that using exceptions or not can give long taking discussions between programmers. But most important is, that every type of error handling must be done very carefully and in every environment someone can make errors. Here we have not reached "low coding" for everyone. Of course the Dotnet functions are much more reliable and powerful than our old VO functions.
For the Encoding I have seen that it is better to use the following code, that is used in the X#-Core-Dll:
System.Convert.ToBase64String( aBytes,Base64FormattingOptions.InsertLineBreaks )
Arne
X# Substitutions for EncodeBase64 and DecodeBase64
Posted: Wed Sep 16, 2020 6:03 am
by wriedmann
Hi Robert,
I must admit that I don't have found the best error handling now - specially when I'm using classes that are called from different application types.
In these cases in the catch block the an exception handler of the calling object is called, but that creates a lot of redundant code only in the try/catch blocks, and that in nearly every method. Therefore I liked the approach you used in the RDD classes - the less code I have to write the better, as it keeps my code clearer and easier to read.
@all: I would recommend everyone to look at the X# sources, there are many things to learn
Wolfgang
X# Substitutions for EncodeBase64 and DecodeBase64
Posted: Wed Sep 16, 2020 6:04 am
by wriedmann
Hi Arne,
thanks for your recommendation, I have added that option to my library.
Wolfgang
X# Substitutions for EncodeBase64 and DecodeBase64
Posted: Wed Sep 30, 2020 1:34 pm
by ArneOrtlinghaus
I have still problems trying to understand the differences between VO and X#-runtime functions and also the function you use.
Sometimes UTF8-Texts are returned and sometimes ANSI-texts from Base64 sequences. This influences if additional characters like the german äöü are decoded correctly. It seems that it is not a trivial task to do this correctly.
What do you use for Baseencoding texts?