Base64 Encoding

137 views
Skip to first unread message

tommed

unread,
Nov 13, 2009, 11:29:47 AM11/13/09
to golang-nuts
Apologies in advance if I am doing something stupid, but I am
attempting to base64 encode a string (for an http authorization
header) and am getting a:

SIGTRAP Trace/breakpoint trap 0x00003460 in ??

..error when I run the following code. Could anyone suggest what I am
doing wrong please?

import (
"flag";
"encoding/base64";
"fmt";
"strings";
"bytes";
)

var username = flag.String("u", "anonymous", "Username");
var password = flag.String("p", "password", "Password");

func main() {
flag.Parse();
fmt.Printf("HTTP Credentials = %s:%s\n", *username, *password);

// base64 encode the username and password
var src []byte = strings.Bytes(*username + ":" + *password);
var dest []byte;
var encoding = base64.NewEncoding
("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
encoding.Encode(dest, src);

// convert bytes to string
var destStr = bytes.NewBuffer(dest).String();

//fmt.Printf("Authorization: basic-%s", destStr);
}

The exception happens during encoding.Encode(dest, src)
Any Ideas?

Many Thanks in advance,
tommed
www.tommed.co.uk

tommed

unread,
Nov 13, 2009, 11:42:17 AM11/13/09
to golang-nuts
Thanks Adam,
You were spot on about the nil.

I just re-read the slice documentation and realised that a slice is
like a pointer to an array.
So I changed:

var dest []byte;
// ...
encoding.Encode(dest, src);


to:

var dest [1000]byte;
// ...
encoding.Encode(&dest, src);

As slices are polymorphic to arrays, the array pointer is considered
equal to a slice and the rest of the code works. Hurray!
Thanks again,
tommed

Adam Langley

unread,
Nov 13, 2009, 1:12:10 PM11/13/09
to tommed, golang-nuts
On Fri, Nov 13, 2009 at 8:42 AM, tommed <tom.me...@googlemail.com> wrote:
> var dest [1000]byte;
> // ...
> encoding.Encode(&dest, src);

... until src > 750 bytes and it all goes wrong...

AGL

Tom Medhurst

unread,
Nov 13, 2009, 2:16:47 PM11/13/09
to Adam Langley, golang-nuts
Well I tried setting dest with:

var dest [encoding.EncodedLen(len(src))]byte 

..but the compiler didn't like that :(

Is this where I should be using something like: 

make([]byte, encoding.EncodedLen(len(src)));

..to allocate a byte slice with the correct number of entries?

Adam Langley

unread,
Nov 13, 2009, 2:19:02 PM11/13/09
to Tom Medhurst, golang-nuts
On Fri, Nov 13, 2009 at 11:16 AM, Tom Medhurst
<tom.me...@googlemail.com> wrote:
> Is this where I should be using something like:
> make([]byte, encoding.EncodedLen(len(src)));
> ..to allocate a byte slice with the correct number of entries?

Exactly. See EncodeToString at the bottom of
http://golang.org/src/pkg/encoding/hex/hex.go


AGL

Rob 'Commander' Pike

unread,
Nov 13, 2009, 2:54:35 PM11/13/09
to Tom Medhurst, Adam Langley, golang-nuts
The size of an array (as opposed to a slice) is part of its type and statically determined.  You cannot create an array type whose size changes programmatically.  The closest you can get is a slice.

-rob

Tom Medhurst

unread,
Nov 13, 2009, 3:00:57 PM11/13/09
to Rob 'Commander' Pike, Adam Langley, golang-nuts
Thanks Rob,

I realise the array is immutable, we are attempting to set the size of the array (during initialization) at runtime.
For us to know the size we need to figure out how big the resulting encoded byte array will be (hence the "var dest := make([]byte, encoding.EncodedLen(len(src)))").

I don't want to alter the size of the array once it's created, I just wanted to know how to set the size of an array during initialization from a value which could only be determined at runtime.

I believe the make(...) command mentioned previously achieves this.
Many Thanks,
tommed
Reply all
Reply to author
Forward
0 new messages