X# Substitutions for EncodeBase64 and DecodeBase64

This forum is meant for questions and discussions about the X# language and tools
User avatar
ArneOrtlinghaus
Posts: 412
Joined: Tue Nov 10, 2015 7:48 am
Location: Italy

X# Substitutions for EncodeBase64 and DecodeBase64

Post 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
User avatar
robert
Posts: 4529
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

X# Substitutions for EncodeBase64 and DecodeBase64

Post by robert »

XSharp Development Team
The Netherlands
robert@xsharp.eu
User avatar
wriedmann
Posts: 3757
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

X# Substitutions for EncodeBase64 and DecodeBase64

Post 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
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
ArneOrtlinghaus
Posts: 412
Joined: Tue Nov 10, 2015 7:48 am
Location: Italy

X# Substitutions for EncodeBase64 and DecodeBase64

Post 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)
User avatar
wriedmann
Posts: 3757
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

X# Substitutions for EncodeBase64 and DecodeBase64

Post 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
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
robert
Posts: 4529
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

X# Substitutions for EncodeBase64 and DecodeBase64

Post 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
XSharp Development Team
The Netherlands
robert@xsharp.eu
User avatar
ArneOrtlinghaus
Posts: 412
Joined: Tue Nov 10, 2015 7:48 am
Location: Italy

X# Substitutions for EncodeBase64 and DecodeBase64

Post 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
User avatar
wriedmann
Posts: 3757
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

X# Substitutions for EncodeBase64 and DecodeBase64

Post 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
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
wriedmann
Posts: 3757
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

X# Substitutions for EncodeBase64 and DecodeBase64

Post by wriedmann »

Hi Arne,

thanks for your recommendation, I have added that option to my library.

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
ArneOrtlinghaus
Posts: 412
Joined: Tue Nov 10, 2015 7:48 am
Location: Italy

X# Substitutions for EncodeBase64 and DecodeBase64

Post 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?
Post Reply