# Pack / Unpack methods

**URL:** https://forum.crystal-lang.org/t/pack-unpack-methods/1608
**Category:** Help & Support
**Created:** [January 10, 2020, 9:44pm UTC](https://forum.crystal-lang.org/t/pack-unpack-methods/1608 "2020-01-10T21:44:46Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![jwoertink](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jwoertink/32/50_2.png) [@jwoertink](https://forum.crystal-lang.org/u/jwoertink)
#### Post date: [January 10, 2020, 9:44pm UTC](https://forum.crystal-lang.org/t/pack-unpack-methods/1608/1 "2020-01-10T21:44:46Z")

</div>

I’m looking for Crystal’s equivalent of Ruby’s `Array#pack` method.

This [thread](https://github.com/crystal-lang/crystal/issues/276) explains why Crystal doesn’t have it, but I’m wondering if anything has been added since this time to maybe support doing it a different way.

```ruby
irb(main):001:0> [5].pack('e*').bytes
=> [0, 0, 160, 64]

```

---

<div class="post-metadata">

### Author: ![jwoertink](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jwoertink/32/50_2.png) [@jwoertink](https://forum.crystal-lang.org/u/jwoertink)
#### Post date: [January 15, 2020, 8:58pm UTC](https://forum.crystal-lang.org/t/pack-unpack-methods/1608/2 "2020-01-15T20:58:51Z")

</div>

Here’s the way it’s done in JavaScript. Maybe someone has an idea of how to replicate it this way in Crystal?

```javascript
const buf = Buffer.allocUnsafe(4);
buf.writeFloatLE(5, 0);
Uint8Array.from(buf);

```

---

<div class="post-metadata">

### Author: ![jwoertink](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jwoertink/32/50_2.png) [@jwoertink](https://forum.crystal-lang.org/u/jwoertink)
#### Post date: [January 15, 2020, 10:15pm UTC](https://forum.crystal-lang.org/t/pack-unpack-methods/1608/3 "2020-01-15T22:15:46Z")

</div>

Alright! Thanks to some help from @straight-shoota if anyone else comes looking for answers, here we go:

```crystal
# Unpack
numbers = [5]
 
io = IO::Memory.new
 
numbers.each do |n|
  io.write_bytes n.to_f32, IO::ByteFormat::LittleEndian
end
 
io.rewind
io.to_s.bytes
#=> [0, 0, 160, 64]

```

```crystal
# Pack
n = [65, 66, 67]

# Ruby
n.pack("ccc") #=> "ABC"

# Crystal
String.build do |io|
  n.each do |number|
    io.write_byte number.to_u8
  end
end #=> "ABC"

```

Yes, it’s a bit more verbose, but if you take a look at the closed issue on Crystal, it’ll make a little sense as to why. For me, the `pack/unpack` methods in ruby always seemed a little mysterious. Seeing that it’s just writing some bytes to some IO really clears it up for me.

---

<div class="post-metadata">

### Author: ![mydoghasfleas](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/mydoghasfleas/32/2173_2.png) [@mydoghasfleas](https://forum.crystal-lang.org/u/mydoghasfleas)
#### Post date: [September 27, 2023, 1:56pm UTC](https://forum.crystal-lang.org/t/pack-unpack-methods/1608/4 "2023-09-27T13:56:11Z")

</div>

Thanks, this helped me! I wonder if examples like these are present in the documentation?
