RejectedSoftware Forums

Sign up

Pages: 1 2

Base64 help

Hi,

Is there a base64 encoding example? I'm trying to convert an image a person uploads into base64 so i can store it into redis.

I'm kind of new to D, so any help would be appreciated.

Thanks!

Re: Base64 help

On Wed, 02 Oct 2013 01:36:52 GMT, Mike wrote:

Hi,

Is there a base64 encoding example? I'm trying to convert an image a person uploads into base64 so i can store it into redis.

I'm kind of new to D, so any help would be appreciated.

Thanks!

You can use std.base64: http://dlang.org/phobos/std_base64.html

If you need D related help, it's probably better to ask at the D forum as it's a lot more active: http://forum.dlang.org/group/digitalmars.D.learn

Re: Base64 help

On Wed, 02 Oct 2013 01:36:52 GMT, Mike wrote:

Hi,

Is there a base64 encoding example? I'm trying to convert an image a person uploads into base64 so i can store it into redis.

I'm kind of new to D, so any help would be appreciated.

Thanks!

See the second example at the top of http://dlang.org/phobos/std_base64.html. You basically just need to put the input data as a ubyte[] array into Base64.encode and you'll get the base 64 string out. MIME encoding also requires line breaks at regular intervals, which is why the byChunk approach is used there. Translated to vibe.d primitives this would look like this (byChunk is only available for std.stdio.File):

import vibe.core.file : openFile;
import std.base64 : Base64;
import std.range : chunks;
import std.stdio : writeln;

// Create MIME Base64 with CRLF, per line 76.
auto f = openFile("./text.txt");
scope(exit) f.close();

Appender!string mime64 = appender!string;

for (ulong i = 0; i < f.size; i += 57) {
  ubyte[57] buf;
  auto chunk_size = min(f.size - i, buf.length);
  f.read(buf[0 .. chunk_size]);
  mime64.put(buf[0 .. chunk_size);
  mime64.put("\r\n");
}

writeln(mime64.data);

Re: Base64 help

Am 02.10.2013 08:48, schrieb simendsjo:

On Wed, 02 Oct 2013 01:36:52 GMT, Mike wrote:

Hi,

Is there a base64 encoding example? I'm trying to convert an image a person uploads into base64 so i can store it into redis.

I'm kind of new to D, so any help would be appreciated.

Thanks!

You can use std.base64: http://dlang.org/phobos/std_base64.html

If you need D related help, it's probably better to ask at the D forum as it's a lot more active: http://forum.dlang.org/group/digitalmars.D.learn

Right, although sometimes it may be a difficult call which is more
appropriate. At least, on the D.learn forum, if it is indeed vibe.d
related, it's very likely that I personally only see it late or never...

Re: Base64 help

Thanks for the help guys. I asked here because I saw http://vibed.org/api/vibe.stream.base64/Base64OutputStream

Re: Base64 help

On Wed, 02 Oct 2013 23:29:57 GMT, Mike wrote:

Thanks for the help guys. I asked here because I saw http://vibed.org/api/vibe.stream.base64/Base64OutputStream

Completely forgot about that one. I've implemented it now: https://github.com/rejectedsoftware/vibe.d/commit/0e86f665c24376d2f7cea5eff1a35e0c845305fb

The example then reduces to:

import vibe.core.file : openFile;
import vibe.stream.base64;
import std.stdio : writeln;

auto f = openFile("./text.txt");
scope(exit) f.close();

auto mime64 = new MemoryOutputStream;
auto encoder = new Base64OutputStream(mime64);
encoder.write(f);
writeln(mime64.data);

Re: Base64 help

Thanks for adding base64outputstream. However, I'm having trouble getting the value from Redis.

I've tried
auto test = redis.get!(char[], char)("image"~to!string(x));
but i'm not getting all the data i need to redraw the image

And these both throw errors
auto test = redis.getRange!(char[], char)("image"~to!string(x), 1, 10);
auto test = redis.getRange!(ubyte[], ubyte)("image"~to!string(x), 1, 10);

Re: Base64 help

On Tue, 15 Oct 2013 01:36:43 GMT, Mike wrote:

Thanks for adding base64outputstream. However, I'm having trouble getting the value from Redis.

I've tried
auto test = redis.get!(char[], char)("image"~to!string(x));
but i'm not getting all the data i need to redraw the image

And these both throw errors
auto test = redis.getRange!(char[], char)("image"~to!string(x), 1, 10);
auto test = redis.getRange!(ubyte[], ubyte)("image"~to!string(x), 1, 10);

That's really hard to tell from the distance (plus unfortunately I haven't personally used Redis up to now). Can you upload the source code of the project somewhere by any chance, or is it closed source?

Otherwise I'd first try to isolate the cause by checking if directly decoding the base64 encoded string results in a proper image and by checking if the data that is sent to Redis is exactly what comes out. That is, if you haven't done so already, of course.

Re: Base64 help

Yeah, I don't think the encoder is converting all the data from the image to base64. I went to a site that converts images to base64, and I compared that to what i got from mime64.data and there seems to be some null characters within it. I can show you the two files if you need to see something.

Thanks for the help so far.

Re: Base64 help

This works for me instead, if anybody is interested.

Appender!string mime64 = appender!string;


for (ulong i = 0; i < file.size; i += 57) {
  ubyte[57] buf;
  auto chunk_size = min(file.size - i, buf.length);
  file.read(buf[0 .. chunk_size]);
  mime64.put(Base64.encode(buf[0 .. chunk_size]));
}

Pages: 1 2