Base64Encoder vs PHP online

Just starting out? Need help? Post your questions and find answers here.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Base64Encoder vs PHP online

Post by Dude »

Here's a (modified) example from the PureBasic manual:

Code: Select all

*Text = UTF8("This is an encoded string")

Encoded$ = Base64Encoder(*Text, MemorySize(*Text))
Debug "Encoded: " + Encoded$

*DecodedBuffer = AllocateMemory(1024)
Base64Decoder(Encoded$, *DecodedBuffer, 1024)
Debug "Decoded: " + PeekS(*DecodedBuffer, -1, #PB_UTF8)
When run, it outputs this for the encoded string:

Code: Select all

VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZwA=
But the end of it differs from http://php.net/manual/en/function.base64-encode.php :

Code: Select all

VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==
Is this a bug with PureBasic? If not, how can I make PureBasic match the PHP version? I need them to match, because my website's PHP script also outputs the same code as the PHP.net website above. PureBasic is the one out of the three that is wrong. :(

Here's my website's PHP script if it helps:

Code: Select all

<?php
$text = "This is an encoded string";
$text = base64_encode($text);
echo $text;
?>
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Base64Encoder vs PHP online

Post by srod »

Try removing the terminating null from the memory being encoded.

Code: Select all

Encoded$ = Base64Encoder(*Text, MemorySize(*Text)-1)
I may look like a mule, but I'm not a complete ass.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Base64Encoder vs PHP online

Post by Dude »

Thanks srod, that works. Seems the manual example has a bug, then. Reported.
infratec
Always Here
Always Here
Posts: 6869
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Base64Encoder vs PHP online

Post by infratec »

I don't think that i is a bug :mrgreen:

Base64Encoder() does what it should.
If you include an additional 0 in the buffer, then it is your fault. :wink:
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Base64Encoder vs PHP online

Post by Dude »

I didn't include it, the example in the manual did. Like I said, it's a manual bug.
infratec
Always Here
Always Here
Posts: 6869
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Base64Encoder vs PHP online

Post by infratec »

It is also no manual/help bug.
If the writer wanted to include the trailing 0 it is 100% Ok.

It is not written that this example produces the same output as php.

So a new php user can also say the help of php is wrong, because it does not include a trailing 0 like a C or PB example code.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Base64Encoder vs PHP online

Post by Dude »

infratec wrote:It is not written that this example produces the same output as php.
Well, I think it's quite obvious and commonsense that the output of it will be compared with other external results, just as I did. Every single PHP Base64 site that I tested showed the same result, with PureBasic being the only one different. The manual example should match the other site results to avoid this type of confusion. IMO, anyway. Obviously not yours. ;)
Post Reply