Overview
Features
Download
Documentation
Community
Add-Ons & Services

Base64 Encoder/Decoder

A general discussion forum.

Base64 Encoder/Decoder

Postby bahadir » 28 Feb 2010, 22:01

Hello,
I try to use Base64 Encoder/Decoder. I encode a sample string("test string"), when I decode it I can't get the original string, last chracter is missing.
What's wrong here? :

Code: Select all
#include <iostream>
#include <sstream>
#include "Poco/Base64Encoder.h"
#include "Poco/Base64Decoder.h"
#include "Poco/StreamCopier.h"

using Poco::Base64Encoder;
using Poco::Base64Decoder;

int main()
{
        std::stringstream lStream,lStream2;
        Base64Encoder encoder(lStream);
        encoder<<"teststring";
        std::string str;
        lStream>>str;
        std::cout<<"encoded:"<<str<<"\n";

        Base64Decoder decoder(lStream2);
        lStream2<<str;
        decoder>>str;
        std::cout<<"decoded:"<<str<<"\n";
}


Result:
------------------
encoded:dGVzdHN0cmlu
decoded:teststrin
bahadir
 
Posts: 17
Joined: 14 Dec 2009, 12:01

Re: Base64 Encoder/Decoder

Postby p47l4f0n » 01 Mar 2010, 13:22

Hm, nice:

input:teststring
encoded:dGVzdHN0cmlu
decoded:teststrin
---
input:teststringA
encoded:dGVzdHN0cmluZ0EK
decoded:teststringA
---
Y35, I'm 4 n00b !
p47l4f0n
 
Posts: 12
Joined: 17 Feb 2010, 22:25

Re: Base64 Encoder/Decoder

Postby guenter » 02 Mar 2010, 07:43

You forgot to call close() on the encoder:

Code: Select all
int main()
{
        std::stringstream lStream,lStream2;
        Base64Encoder encoder(lStream);
        encoder<<"teststring";
        encoder.close();  // !!!!
        std::string str;
        lStream>>str;
        std::cout<<"encoded:"<<str<<"\n";

        Base64Decoder decoder(lStream2);
        lStream2<<str;
        decoder>>str;
        std::cout<<"decoded:"<<str<<"\n";
}
guenter
 
Posts: 1092
Joined: 11 Jul 2006, 16:27
Location: Austria

Re: Base64 Encoder/Decoder

Postby p47l4f0n » 02 Mar 2010, 10:48

I tried the flush method :)
Y35, I'm 4 n00b !
p47l4f0n
 
Posts: 12
Joined: 17 Feb 2010, 22:25

Re: Base64 Encoder/Decoder

Postby bahadir » 02 Mar 2010, 21:38

Thanks, close() solved the problem. It would be nice if that hint is emphasized in the documentation.
bahadir
 
Posts: 17
Joined: 14 Dec 2009, 12:01

Re: Base64 Encoder/Decoder

Postby bahadir » 10 Mar 2010, 00:05

Hi,
I encountered another problem with Base64 Encoding/Decoding. I have problem with strings which includes space characters. Here's my functions:

Code: Select all
std::string Encode(std::string str)
{
   std::stringstream lStream;
   Poco::Base64Encoder encoder(lStream);
   encoder<<str;
   encoder.close();
   std::string res;
   lStream>>res;
   return res;
}
std::string Decode(std::string str)
{
   std::stringstream lStream;
   Poco::Base64Decoder decoder(lStream);
   lStream<<str;
   std::string res;
   decoder>>res;
   return res;
}

std::string txt = "test msg text";
std::string txtencoded = Encode(txt);
std::cout<<"Encoded text is:"<<txtencoded<<"\n"<<
            "Decoded text is:"<<Decode(txtencoded)<<"\n";


The output is:
Encoded text is:dGVzdCBtc2cgdGV4dA==
Decoded text is:test

When it decodes the text, it cuts the characters after the space.
What's wrong with my code?

Thanks
bahadir
 
Posts: 17
Joined: 14 Dec 2009, 12:01

Re: Base64 Encoder/Decoder

Postby guenter » 10 Mar 2010, 00:12

This is not a problem of Base64Decoder, it's a problem of your iostreams usage.

Code: Select all
std::string res;
decoder>>res;


will only read characters up to the next whitespace.
guenter
 
Posts: 1092
Joined: 11 Jul 2006, 16:27
Location: Austria


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 2 guests

cron