Just starting out? Need help? Post your questions and find answers here.
sartic
Enthusiast
Posts: 143 Joined: Thu Aug 26, 2010 8:26 am
Post
by sartic » Thu Mar 04, 2021 7:01 pm
Code: Select all
Procedure.s to64(str.s)
str=Left(str,512)
Encoded.s=Space(1024)
If Base64Encoder(@str, StringByteLength(str), @Encoded, 1024)
ProcedureReturn Encoded
Else
ProcedureReturn "greska, random broj="+Str(Random(999999999))
EndIf
EndProcedure
Procedure.s from64(str.s)
str=Left(str,512)
Decoded.s=Space(1024)
If Base64Decoder(@str, StringByteLength(str), @Decoded, 1024)
ProcedureReturn Decoded
Else
ProcedureReturn "greska, random broj="+Str(Random(999999999))
EndIf
EndProcedure
It works in ascii PB.
// Edit: Added Code-Tags (Kiffi)
Registered user of PB (on Linux Mint 21.1 & Win 10 64bit)
STARGÅTE
Addict
Posts: 2226 Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:
Post
by STARGÅTE » Thu Mar 04, 2021 7:22 pm
I don't know which PB version do you use, but in PB 5.73 the procedure Base64Encoder() has only 2 parameters and Base64Decoder only 3 and they work with strings in general (ascii and unicode).
sartic
Enthusiast
Posts: 143 Joined: Thu Aug 26, 2010 8:26 am
Post
by sartic » Thu Mar 04, 2021 7:23 pm
This code works in pb 5.46
Fred change something
I cant get it alone.
Registered user of PB (on Linux Mint 21.1 & Win 10 64bit)
STARGÅTE
Addict
Posts: 2226 Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:
Post
by STARGÅTE » Thu Mar 04, 2021 7:28 pm
The question is, how should the input string handled? The Base64 function works with a buffer, so the string must be converted into Ascii(), UTF8() or unicode.
From your example it looks like you just want to use ascii characters, but then unicode characters can not be encoded.
sartic
Enthusiast
Posts: 143 Joined: Thu Aug 26, 2010 8:26 am
Post
by sartic » Thu Mar 04, 2021 7:40 pm
if i check with:
https://www.base64encode.org
i get different results
i am stumbled
i just need working code from pb 5.46 that worked in ascii
Registered user of PB (on Linux Mint 21.1 & Win 10 64bit)
STARGÅTE
Addict
Posts: 2226 Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:
Post
by STARGÅTE » Thu Mar 04, 2021 8:16 pm
This code is for ascii strings (i don't mean the compiler format, that is unicode):
Code: Select all
EnableExplicit
Procedure.s to64(str.s)
Protected *buffer = Ascii(str)
Protected result.s = Base64Encoder(*buffer, MemorySize(*buffer)-1, #PB_Cipher_NoPadding)
FreeMemory(*buffer)
ProcedureReturn result
EndProcedure
Procedure.s from64(str.s)
Protected *buffer = AllocateMemory(Len(str)+64)
Protected length.i = Base64Decoder(str, *buffer, MemorySize(*buffer))
Protected result.s = PeekS(*buffer, length, #PB_Ascii)
FreeMemory(*buffer)
ProcedureReturn result
EndProcedure
Debug to64("Hello world!")
Debug from64(to64("Hello world!"))