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

Just starting out? Need help? Post your questions and find answers here.
sartic
Enthusiast
Enthusiast
Posts: 143
Joined: Thu Aug 26, 2010 8:26 am

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

Post 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)
Registered user of PB (on Linux Mint 21.1 & Win 10 64bit)
User avatar
STARGÅTE
Addict
Addict
Posts: 2226
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

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

Post 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).
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
sartic
Enthusiast
Enthusiast
Posts: 143
Joined: Thu Aug 26, 2010 8:26 am

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

Post by sartic »

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)
User avatar
STARGÅTE
Addict
Addict
Posts: 2226
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

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

Post 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.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
sartic
Enthusiast
Enthusiast
Posts: 143
Joined: Thu Aug 26, 2010 8:26 am

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

Post 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
Registered user of PB (on Linux Mint 21.1 & Win 10 64bit)
User avatar
STARGÅTE
Addict
Addict
Posts: 2226
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

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

Post 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!"))
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
Post Reply