Base64 packing question

J

John Sands

I'm packing up a file into an e-mail attachment using base64 encoding
and I'm only getting the first line of the file. I've discovered that
it's a misunderstanding I have about the pack method:

irb(main):001:0> ["a", "b", "c"].pack("m").unpack("m")
=> ["a"]

How do I get the whole array packed?
 
S

Sam Roberts

Quoteing (e-mail address removed), on Sat, Dec 04, 2004 at 08:36:11AM +0900:
I'm packing up a file into an e-mail attachment using base64 encoding
and I'm only getting the first line of the file. I've discovered that
it's a misunderstanding I have about the pack method:

irb(main):001:0> ["a", "b", "c"].pack("m").unpack("m")
=> ["a"]

How do I get the whole array packed?

Not sure why you think that pack wil concatenate the array before
base64ing it. Check out lib/base64.rb in the ruby lib directory for
an example of base64 encoding/decoding.

Sam
 
J

John Sands

Thanks. It works now.

I did it that way because The Ruby Way (page 92) has "the easiest way
to do a base64 encode/decode is the use the built-in features of Ruby.
The Array class has a pack method..." and it doesn't mention the
base64 class; so I didn't look for one.

Thanks.


Quoteing (e-mail address removed), on Sat, Dec 04, 2004 at 08:36:11AM +0900:

I'm packing up a file into an e-mail attachment using base64 encoding
and I'm only getting the first line of the file. I've discovered that
it's a misunderstanding I have about the pack method:

irb(main):001:0> ["a", "b", "c"].pack("m").unpack("m")
=> ["a"]

How do I get the whole array packed?

Not sure why you think that pack wil concatenate the array before
base64ing it. Check out lib/base64.rb in the ruby lib directory for
an example of base64 encoding/decoding.

Sam
 
E

Eric Hodel

I'm packing up a file into an e-mail attachment using base64 encoding
and I'm only getting the first line of the file. I've discovered that
it's a misunderstanding I have about the pack method:

irb(main):001:0> ["a", "b", "c"].pack("m").unpack("m")
=> ["a"]

How do I get the whole array packed?

The Array in Array#pack is like a C struct, and the argument to pack is
the format string to pack the Array into, so ["a", "b", "c"].pack "m"
only packs one of the strings you provided.

You certainly don't want to pack the strings individually (["a", "b",
"c"].pack "mmm"), instead you simply need to join the Array before
packing:

body = ["a", "b", "c"].join("\n")

[body].pack("m")
 
S

Sam Roberts

Quoteing (e-mail address removed), on Sat, Dec 04, 2004 at 10:04:55AM +0900:
Thanks. It works now.

I did it that way because The Ruby Way (page 92) has "the easiest way
to do a base64 encode/decode is the use the built-in features of Ruby.
The Array class has a pack method..." and it doesn't mention the
base64 class; so I didn't look for one.

Its got good advice, thats an odd-ball header, and I wouldn't actually
use it, but it saved me typing example code...

Cheers,
Sam
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top