X# Substitutions for EncodeBase64 and DecodeBase64

This forum is meant for questions and discussions about the X# language and tools
User avatar
wriedmann
Posts: 3756
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

X# Substitutions for EncodeBase64 and DecodeBase64

Post by wriedmann »

Hi Arne,
I have too some problems with these encodings....but I try to not use the VO compatible functions when working with encodings, but using instead own functions where I have the encoding under control.
The main problem is that in VO you can use such strings also to save and restore bitmaps or other binary data where cou cannot in X#.
Personally I do think that the development team cannot do anything here - the Ansi vs Unicode problem is not easy to solve (or not possible to solve at all without touching code).
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 »

Finally I could better understand, what is needed for Base64 Encoding or Decoding correctly:
You have to know, what characterset should be inside a Base64 decoded string. The original VO functions DecodeBase64 and EncodeBase64 made a coding to the ANSI character set.
Nowadays often an encoding to UTF8 is made, for example in the email raw text to be independant of a national character set.
So if someone has used the VO functions, he should implement both, ANSI and UTF8.
This is the code to have both conversions:

- Encode
local aBytes as byte[]
aBytes := System.Convert.FromBase64String( cValue )
if lutf8
cRetVal := System.Text.Encoding.UTF8:GetString( aBytes )
else
cRetVal := System.Text.Encoding.GetEncoding(1252):GetString( aBytes )
endif

- Decode
local aBytes as byte[]
if lutf8
aBytes := System.Text.Encoding.UTF8:GetBytes( cValue )
else
aBytes := System.Text.Encoding.GetEncoding(1252):GetBytes( cValue )
endif
c := System.Convert.ToBase64String( aBytes, Base64FormattingOptions.InsertLineBreaks )
Post Reply