www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Base64 - Encoding and decoding

reply Nrgyzer <nrgyzer gmail.com> writes:
Hey guys,

I want encode and decode binary files like images, documents and similar file
types to use it in an binary xml. For those I created a
simple command line tool which encode or decode an file.

The encoding of files will be processed by these lines:

...
ubyte[] buffer;
buffer.length = source.available();
source.read(buffer);

string content;

for (uint i = 0; i < buffer.length; i++) {
content ~= to!(char)(buffer[i]);
}

destination.write(encode(content));
...

This seems to work correctly, but when I want decode the file, I always get
"std.base64.Base64CharException: Invalid base64 character".
I'm currently using the following to decode the file:

char[] content;

for (uint i = 0; i < source.available(); i++) {
content ~= source.getc();
}

decode(to!(string)(content));

But... when I directly decode the encoded (without read the encoded content
from external file), the decoded file is valid.

I hope anyone know where's my mistake :)

Thanks!
Nov 27 2010
parent reply Kagamin <spam here.lot> writes:
Nrgyzer Wrote:

 decode(to!(string)(content));
check out, what you send to decode function. the code... /3_=
Nov 28 2010
parent Nrgyzer <nrgyzer gmail.com> writes:
I solved the problem by replacing destination.write() to
destination.writeString() in the encoding function.

But... thanks for providing help :)
Nov 29 2010