Page 1 of 1

How to convert this procedures to unicode PB? (base64)

Posted: Thu Mar 04, 2021 7:01 pm
by sartic

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)

Re: How to convert this procedures to unicode PB? (base64)

Posted: Thu Mar 04, 2021 7:22 pm
by STARGÅTE
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).

Re: How to convert this procedures to unicode PB? (base64)

Posted: Thu Mar 04, 2021 7:23 pm
by sartic
This code works in pb 5.46
Fred change something
I cant get it alone.

Re: How to convert this procedures to unicode PB? (base64)

Posted: Thu Mar 04, 2021 7:28 pm
by STARGÅTE
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.

Re: How to convert this procedures to unicode PB? (base64)

Posted: Thu Mar 04, 2021 7:40 pm
by sartic
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

Re: How to convert this procedures to unicode PB? (base64)

Posted: Thu Mar 04, 2021 8:16 pm
by STARGÅTE
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!"))