fbpx
Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1

TOPIC:

X# Substitutions for EncodeBase64 and DecodeBase64 14 Sep 2020 18:00 #15875

  • ArneOrtlinghaus's Avatar

  • ArneOrtlinghaus

  • Topic Author


  • Posts: 203
  • 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

    Please Log in or Create an account to join the conversation.

    X# Substitutions for EncodeBase64 and DecodeBase64 14 Sep 2020 18:02 #15876

  • robert's Avatar

  • robert


  • Posts: 1862
  • XSharp Development Team
    The Netherlands

    Please Log in or Create an account to join the conversation.

    X# Substitutions for EncodeBase64 and DecodeBase64 15 Sep 2020 06:08 #15877

  • wriedmann's Avatar

  • wriedmann


  • Posts: 2344
  • Hi Arne,

    this is was I'm using:
    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

    www.riedmann.it - docs.xsharp.it

    Please Log in or Create an account to join the conversation.

    X# Substitutions for EncodeBase64 and DecodeBase64 15 Sep 2020 14:52 #15881

  • ArneOrtlinghaus's Avatar

  • ArneOrtlinghaus

  • Topic Author


  • Posts: 203
  • 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)

    Please Log in or Create an account to join the conversation.

    X# Substitutions for EncodeBase64 and DecodeBase64 15 Sep 2020 15:57 #15882

  • wriedmann's Avatar

  • wriedmann


  • Posts: 2344
  • 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

    www.riedmann.it - docs.xsharp.it

    Please Log in or Create an account to join the conversation.

    X# Substitutions for EncodeBase64 and DecodeBase64 15 Sep 2020 16:08 #15883

  • robert's Avatar

  • robert


  • Posts: 1862
  • 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

    Please Log in or Create an account to join the conversation.

    Last edit: by robert.

    X# Substitutions for EncodeBase64 and DecodeBase64 15 Sep 2020 16:12 #15884

  • ArneOrtlinghaus's Avatar

  • ArneOrtlinghaus

  • Topic Author


  • Posts: 203
  • 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

    Please Log in or Create an account to join the conversation.

    X# Substitutions for EncodeBase64 and DecodeBase64 16 Sep 2020 08:03 #15886

  • wriedmann's Avatar

  • wriedmann


  • Posts: 2344
  • 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

    www.riedmann.it - docs.xsharp.it

    Please Log in or Create an account to join the conversation.

    X# Substitutions for EncodeBase64 and DecodeBase64 16 Sep 2020 08:04 #15887

  • wriedmann's Avatar

  • wriedmann


  • Posts: 2344
  • Hi Arne,

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

    Wolfgang
    Wolfgang Riedmann
    Meran, South Tyrol, Italy

    www.riedmann.it - docs.xsharp.it

    Please Log in or Create an account to join the conversation.

    • Page:
    • 1