# Newbie question - how to write out specific bytes to an IO stream?

**URL:** <https://www.ruby-forum.com/t/newbie-question-how-to-write-out-specific-bytes-to-an-io-stream/119445>\
**Category:** Ruby\
**Created:** [November 7, 2007, 1:59am UTC](https://www.ruby-forum.com/t/newbie-question-how-to-write-out-specific-bytes-to-an-io-stream/119445 "2007-11-07T01:59:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Collin\_VanDyck](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/839c29/32.png) [@Collin\_VanDyck](https://www.ruby-forum.com/u/Collin_VanDyck)\
**Post date:** [November 7, 2007, 1:59am UTC](https://www.ruby-forum.com/t/newbie-question-how-to-write-out-specific-bytes-to-an-io-stream/119445/1 "2007-11-07T01:59:33Z")

</div>

Hello – I’m trying to write out specific byte sequences over the  
wire / to a file / etc. Let’s say for example that I wanted to write  
out

99 111 108 108 105 110

as a simple, six byte sequence to a file. In Java, I might use the  
byte primitive, but it seems that in Ruby you have Fixnum and then a  
Float for fractions. If I simply create a Fixnum using the 99  
literal, I think that that will create more than an 8-bit sequence  
when I write out to the file.

Any ideas on how to do this?

---

<div class="post-metadata">

**Author:** ![Collin\_VanDyck](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/839c29/32.png) [@Collin\_VanDyck](https://www.ruby-forum.com/u/Collin_VanDyck)\
**Post date:** [November 7, 2007, 2:37am UTC](https://www.ruby-forum.com/t/newbie-question-how-to-write-out-specific-bytes-to-an-io-stream/119445/2 "2007-11-07T02:37:34Z")

</div>

On 11/6/07, Collin VanDyck [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> when I write out to the file.
> 
> Any ideas on how to do this?

String objects are arrays of bytes, so you could simply do…

num\_string = “99 111 108 108 105 110”  
byte\_string = num\_string.split.map! {|word| word.to\_i.chr}.join

# Now, to illustrate…

byte\_string.class

# gives String

byte[0]

# gives 99, the base 10 representation of 1100011

# note that each byte can store numbers up to 255

puts byte\_string

# gives collin

If you want to store the 1’s and 0’s as a text string, you can use the  
Integer#to\_s(2) method.

hth,  
Todd

---

<div class="post-metadata">

**Author:** ![Collin\_VanDyck](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/839c29/32.png) [@Collin\_VanDyck](https://www.ruby-forum.com/u/Collin_VanDyck)\
**Post date:** [November 7, 2007, 2:55am UTC](https://www.ruby-forum.com/t/newbie-question-how-to-write-out-specific-bytes-to-an-io-stream/119445/3 "2007-11-07T02:55:40Z")

</div>

On Nov 6, 6:42 pm, Collin VanDyck [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> when I write out to the file.
> 
> Any ideas on how to do this?

I _think_ you’ll want to use Array#pack. E.g., to write them out in  
big-endian order:

> a = [99, 111, 108, 108, 105, 110].pack ‘n\*’  
> =\> “\000c\000o\000l\000l\000i\000n”  
> file.write a

If you want little-endian, use ‘s\*’ (or ‘S\*’ for unsigned) instead of  
‘n\*’. See [RDoc Documentation](http://ruby-doc.org/core/) and check Array#pack for more  
details on the directives.

---

<div class="post-metadata">

**Author:** ![Collin\_VanDyck](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/839c29/32.png) [@Collin\_VanDyck](https://www.ruby-forum.com/u/Collin_VanDyck)\
**Post date:** [November 7, 2007, 3:39am UTC](https://www.ruby-forum.com/t/newbie-question-how-to-write-out-specific-bytes-to-an-io-stream/119445/4 "2007-11-07T03:39:34Z")

</div>

On 11/6/07, yermej [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > literal, I think that that will create more than an 8-bit sequence
> 
> If you want little-endian, use ‘s\*’ (or ‘S\*’ for unsigned) instead of  
> ‘n\*’. See [RDoc Documentation](http://ruby-doc.org/core/) and check Array#pack for more  
> details on the directives.

Hmm, I was thinking about #pack, but your example creates 12 bytes.

---

<div class="post-metadata">

**Author:** ![Collin\_VanDyck](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/839c29/32.png) [@Collin\_VanDyck](https://www.ruby-forum.com/u/Collin_VanDyck)\
**Post date:** [November 7, 2007, 3:53am UTC](https://www.ruby-forum.com/t/newbie-question-how-to-write-out-specific-bytes-to-an-io-stream/119445/5 "2007-11-07T03:53:35Z")

</div>

Thank you for your replies!

Both methods seem to work equally well, but I’m seeming to favor the  
pack method because i can just push in my random bytes (0-255) into an  
array. I decided to use pack “C\*” though, because I think that the C\*  
directive will limit the packed members to 8 bits, if I’m not mistaken.

---

<div class="post-metadata">

**Author:** ![Collin\_VanDyck](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/839c29/32.png) [@Collin\_VanDyck](https://www.ruby-forum.com/u/Collin_VanDyck)\
**Post date:** [November 7, 2007, 4:25am UTC](https://www.ruby-forum.com/t/newbie-question-how-to-write-out-specific-bytes-to-an-io-stream/119445/6 "2007-11-07T04:25:08Z")

</div>

On 11/6/07, Collin VanDyck [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thank you for your replies!
> 
> Both methods seem to work equally well, but I’m seeming to favor the  
> pack method because i can just push in my random bytes (0-255) into an  
> array. I decided to use pack “C\*” though, because I think that the C\*  
> directive will limit the packed members to 8 bits, if I’m not mistaken.

Good choice!

Todd

---

<div class="post-metadata">

**Author:** ![Collin\_VanDyck](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/839c29/32.png) [@Collin\_VanDyck](https://www.ruby-forum.com/u/Collin_VanDyck)\
**Post date:** [November 7, 2007, 11:30am UTC](https://www.ruby-forum.com/t/newbie-question-how-to-write-out-specific-bytes-to-an-io-stream/119445/7 "2007-11-07T11:30:18Z")

</div>

Todd B. wrote:

> > > Float for fractions. If I simply create a Fixnum using the 99  
> > > If you want little-endian, use ‘s\*’ (or ‘S\*’ for unsigned) instead of  
> > > ‘n\*’. See [RDoc Documentation](http://ruby-doc.org/core/) and check Array#pack for more  
> > > details on the directives.
> 
> Hmm, I was thinking about #pack, but your example creates 12 bytes.

That’s because n isn’t the code for a byte - you want c or C, for signed  
or unsigned char:

irb(main):004:0\> a = [1, 2, 3, 4, 5, 6].pack ‘C\*’  
=\> “\001\002\003\004\005\006”
