Base64Encoder

Just starting out? Need help? Post your questions and find answers here.
Foz
Addict
Addict
Posts: 1359
Joined: Tue Nov 13, 2007 12:42 pm
Location: Manchester, UK

Base64Encoder

Post by Foz »

Looking at the example in the help with 4.60b3 and the second debug line I just get a line of "?????????" - is this correct? I was expecting, well, an ASCII output!

Code: Select all

  Example.s = "This is a test string!" 
  Decoded.s = Space(1024) 
  Encoded.s = Space(1024) 
    
  Debug Base64Encoder(@Example, StringByteLength(Example), @Encoded, 1024)
  Debug Encoded 
    
  Debug Base64Decoder(@Encoded, StringByteLength(Encoded), @Decoded, 1024)
  Debug Decoded

*** edit: and off topic, finally 1000 posts! Woot!
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: Base64Encoder

Post by Trond »

It does indeed give an ASCII output, but when you compile in UNICODE, the ASCII will look like ????????. Try again with unicode off.

Edit: Or peek the string as ascii. Note that it's still twice as many characters as in ascii mode, because StringByteLength() will return double the amount in unicode mode.

Code: Select all

  Example.s = "This is a test string!" 
  Decoded.s = Space(1024) 
  Encoded.s = Space(1024) 
    
  Debug Base64Encoder(@Example, StringByteLength(Example), @Encoded, 1024)
  Debug PeekS(@Encoded, -1, #PB_Ascii)
    
  Debug Base64Decoder(@Encoded, StringByteLength(Encoded), @Decoded, 1024)
  Debug Decoded
Foz
Addict
Addict
Posts: 1359
Joined: Tue Nov 13, 2007 12:42 pm
Location: Manchester, UK

Re: Base64Encoder

Post by Foz »

LOL I didn't realise I had it checked!

Thanks!


*** edit: perhaps this little nugget of info could be added to the help...
MyTrial
Enthusiast
Enthusiast
Posts: 165
Joined: Thu Nov 30, 2006 11:47 am

Re: Base64Encoder

Post by MyTrial »

Hi Trond
thank you for your informations for that Unicode/ASCII problem. But your demonstration doesn't work correctly. If you compile without Unicode mode it gives a correct output:

Code: Select all

32
VGhpcyBpcyBhIHRlc3Qgc3RyaW5nIQ==
22
This is a test string!
But if you compile with Unicode mode it gives a wrong output:

Code: Select all

60
VABoAGkAcwAgAGkAcwAgAGEAIAB0AGUAcwB0ACAAcwB0AHIAaQBuAGcAIQA=
44
This is a test string!
The Base64 Code is wrong (for an ASCII input)! For an Unicode input the output is ok. But who needs that!? I think that all people need a Base64 encoding of an ASCII input, which PureBasic changes to Unicode when you compile in Unicode mode.
Maybe I have a plank in front of my head - I do not get it to work, but do you have a solution for that?
Sigi
MyTrial
Enthusiast
Enthusiast
Posts: 165
Joined: Thu Nov 30, 2006 11:47 am

Re: Base64Encoder

Post by MyTrial »

Code: Select all

Example.s = "This is a test string!"
Work.s = Space(1024)
Decoded.s = Space(1024)
Encoded.s = Space(1024)
 
PokeS(@Work,Example,Len(Example),#PB_Ascii)
Debug Base64Encoder(@Work, StringByteLength(Work), @Encoded, 1024)
Debug PeekS(@Encoded, -1, #PB_Ascii)
Would work correctly for an ASCII input with and without Unicode mode.
Sigi
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: Base64Encoder

Post by Trond »

The Base64 Code is wrong (for an ASCII input)! For an Unicode input the output is ok.
The Base64 encoder encodes a memory buffer. It doesn't know whether this is ascii or unicode encoded. The memory buffer in your example is a PureBasic string. PureBasic strings are ascii in ascii mode and unicode in unicode mode. Thus what happens is only natural.

Your example isn't the right way. You poke ascii data into a unicode string, which should give you problems at some point. Indeed, you already have a problem because StringByteLength(Work) doesn't return the correct length of ascii data within a unicode string, as this example shows:

Code: Select all

Work.s = Space(1024)
PokeS(@Work, "12345", -1, #PB_Ascii)
Debug StringByteLength(Work) ; wrong in unicode mode
When you have string data that could be of a different format than normal PB strings you should use allocated memory, like this:

Code: Select all

Example.s = "This is a test string!"
*Work = AllocateMemory(1024)
*Encoded = AllocateMemory(1024)
 
PokeS(*Work,Example,Len(Example), #PB_Ascii)
Debug Base64Encoder(*Work, MemoryStringLength(*Work, #PB_Ascii), *Encoded, MemorySize(*Encoded))
Debug PeekS(*Encoded, -1, #PB_Ascii)

FreeMemory(*Work) ; important
FreeMemory(*Encoded) ; important
Post Reply