base64->wstring decoding

Hi, I need to write a std::wstring which can contain binary data like \0 to a xml attribute, so I'm converting it to base64 but I have problems decoding it: typedef transform_width<binary_from_base64<std::string::const_iterator>,32,6> decode; std::wostringstream out; std::copy(decode(in.begin()),decode(in.end()),std::ostream_iterator<wchar_t,wchar_t>(out)); is this a correct way to decode base64? it works, but only if (bits % 6 == 0). otherwise the program is abort()ed due to an exception. the string was encoded by base64_from_binary<transform_width<std::wstring::const_iterator,6,32> > Thanks, -- Stefan Strasser

This problem came about due to the fact I couldn't figure out how to implement (in your code) decode(in.end()). I really did try but concluded that I couldn't do with out losing the ablity to buid up my string translators by stepwise composition of inline code. This latter was important to me to guarentee optimal code generation so I wouldn't be tempted to make a bunch of translators for every combination. After much frustation I came to realized that it was easier to just avoid using (decode(in.end())). See test_iterators_base64 so use something like So your case might be altered to interate not on the input characters but rather on the output
typedef transform_width<binary_from_base64<std::(w?)string::const_iterator>,32,6> decode;
std::wostringstream out;
std::ostream_iterator<wchar_t,wchar_t> oit(out); decode iit(s); bits_size = in.size (32 / 6 ) // ? while(bit_size-- > 0){ *(oit++) = decode(*iit); Other misc observations: a) i notices you're using a 32 bit wchar_t - note that this is non-portable. I would think about using std::vector<boost::int_least32>. or maybe instead of using the "magic" number 32 you might want to try something like (sizeof(wchar_t) * 8). b) I'm not sure I remember where I handled the padding. you should double check that your if your original binary string isn't modulo 6 bits you might find it filled with 0 after a round trip through base64. Robert Ramey Stefan Strasser wrote:
Hi,
I need to write a std::wstring which can contain binary data like \0 to a xml attribute, so I'm converting it to base64 but I have problems decoding it: std::copy(decode(in.begin()),decode(in.end()),);
is this a correct way to decode base64? it works, but only if (bits % 6 == 0). otherwise the program is abort()ed due to an exception.
the string was encoded by base64_from_binary<transform_width<std::wstring::const_iterator,6,32>
Thanks,

Robert Ramey schrieb:
So your case might be altered to interate not on the input characters but rather on the output
Thanks! iterating on output works. in case someone else on the list is interested, below's the code.
a) i notices you're using a 32 bit wchar_t - note that this is non-portable. I would think about using std::vector<boost::int_least32>. or maybe instead of using the "magic" number 32 you might want to try something like (sizeof(wchar_t) * 8).
32: yes, I've sent example code. int_least32: wstring/wchar_t is ok, it's a representation of a c++ wide string literal. (google for "metac++" for a reason)
b) I'm not sure I remember where I handled the padding. you should double check that your if your original binary string isn't modulo 6 bits you might find it filled with 0 after a round trip through base64.
I'm not sure what you mean here, how can an unencoded string whose CharT is always > 6 bits be filled with 0 after round trip? base64 can, and that's fine. thanks again! typedef transform_width<binary_from_base64<std::string::const_iterator>,sizeof(wchar_t)*8,6> base64; std::size_t destsize=enc.size() * 6 / (8 * sizeof(wchar_t)); tthis->resize(destsize); base64 it(enc.begin()); for(unsigned int c=0;c<destsize;++c,++it) (*this)[c]=*it; --
Robert Ramey
Stefan Strasser wrote:
Hi,
I need to write a std::wstring which can contain binary data like \0 to a xml attribute, so I'm converting it to base64 but I have problems decoding it: std::copy(decode(in.begin()),decode(in.end()),);
is this a correct way to decode base64? it works, but only if (bits % 6 == 0). otherwise the program is abort()ed due to an exception.
the string was encoded by base64_from_binary<transform_width<std::wstring::const_iterator,6,32>
Thanks,
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Stefan Strasser
participants (2)
-
Robert Ramey
-
Stefan Strasser