# Trying to get pack to work with the \* template character

**URL:** <https://www.ruby-forum.com/t/trying-to-get-pack-to-work-with-the-template-character/75465>\
**Category:** Ruby\
**Created:** [November 10, 2006, 2:21am UTC](https://www.ruby-forum.com/t/trying-to-get-pack-to-work-with-the-template-character/75465 "2006-11-10T02:21:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jh100000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b2d939/32.png) [@jh100000](https://www.ruby-forum.com/u/jh100000)\
**Post date:** [November 10, 2006, 2:21am UTC](https://www.ruby-forum.com/t/trying-to-get-pack-to-work-with-the-template-character/75465/1 "2006-11-10T02:21:36Z")

</div>

Ok, here’s a snippet of my code.

@image\_array = Array.new  
@image = File.open(“test.JPG”, “rb”) do |image|  
image.each\_byte{|ch| @image\_array \<\< ch.to\_s}  
end  
@temp = @image\_array.pack(“m\*”)

when I display the value of @temp… only the first element of  
@image\_array has been packed… any idea why the \* isn’t working? I  
thought the \* meant that it would cycle through the entire array and  
convert each element… any ideas?

---

<div class="post-metadata">

**Author:** ![jh100000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b2d939/32.png) [@jh100000](https://www.ruby-forum.com/u/jh100000)\
**Post date:** [November 10, 2006, 7:47am UTC](https://www.ruby-forum.com/t/trying-to-get-pack-to-work-with-the-template-character/75465/2 "2006-11-10T07:47:24Z")

</div>

John H. wrote:

> @image\_array = Array.new  
> @image = File.open(“test.JPG”, “rb”) do |image|  
> image.each\_byte{|ch| @image\_array \<\< ch.to\_s}  
> end  
> @temp = @image\_array.pack(“m\*”)

I know very little about #pack, so I can’t help you there. However, I  
can offer a slightly more terse (faster?) way to get the same array of  
strings:

# Treat strings as one byte per char ascii

$KCODE=‘a’  
File.open( “test.JPG”, “rb” ) do |image|  
@image\_array = image.read.split(’’).map{ |ch| ch[0].to\_s }  
end

---

<div class="post-metadata">

**Author:** ![jh100000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b2d939/32.png) [@jh100000](https://www.ruby-forum.com/u/jh100000)\
**Post date:** [November 10, 2006, 4:28pm UTC](https://www.ruby-forum.com/t/trying-to-get-pack-to-work-with-the-template-character/75465/3 "2006-11-10T16:28:42Z")

</div>

On 11/9/06, John H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Ok, here’s a snippet of my code.
> 
> @image\_array = Array.new  
> @image = File.open(“test.JPG”, “rb”) do |image|  
> image.each\_byte{|ch| @image\_array \<\< ch.to\_s}  
> end  
> @temp = @image\_array.pack(“m\*”)

Correct me if I guess incorrectly here, but it looks like you want to  
base64 encode a JPG image file. Ruby has a nice Base64 class which  
will do this for you …

require ‘base64’  
@temp = File.open(“test.JPG”, “rb”) {|image| Base64.encode64( image.read  
)}

Blessings,  
TwP

---

<div class="post-metadata">

**Author:** ![jh100000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b2d939/32.png) [@jh100000](https://www.ruby-forum.com/u/jh100000)\
**Post date:** [November 11, 2006, 11:37am UTC](https://www.ruby-forum.com/t/trying-to-get-pack-to-work-with-the-template-character/75465/4 "2006-11-11T11:37:00Z")

</div>

On Fri, 10 Nov 2006 01:21:38 -0000, John H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> thought the \* meant that it would cycle through the entire array and  
> convert each element… any ideas?

As Tim said, ruby does include a base64 library which will probably do  
what you want, but if you’re determined to use pack you need to know  
that  
‘m’ expects a binary string as it’s argument. ‘m\*’ expects one or more  
binary strings. So in this case, you just need ‘m’ like so:

```
[File.read('test.JPG')].pack('m')

```

or possibly:

```
[File.open('test.JPG','rb') { |f| f.read }].pack('m')

```
