PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > Newbie question - how to write out specific bytes to an IO stream?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Newbie question - how to write out specific bytes to an IO stream?

Réponse
 
LinkBack Outils de la discussion
Vieux 07/11/2007, 00h42   #1
Collin VanDyck
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Newbie question - how to write out specific bytes to an IO stream?

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?

  Réponse avec citation
Vieux 07/11/2007, 01h31   #2
Todd Benson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Newbie question - how to write out specific bytes to an IOstream?

On 11/6/07, Collin VanDyck <gluedtomyseat@gmail.com> wrote:
> 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?


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

  Réponse avec citation
Vieux 07/11/2007, 01h50   #3
yermej
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Newbie question - how to write out specific bytes to an IO stream?

On Nov 6, 6:42 pm, Collin VanDyck <gluedtomys...@gmail.com> wrote:
> 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?


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 http://ruby-doc.org/core/ and check Array#pack for more
details on the directives.

  Réponse avec citation
Vieux 07/11/2007, 02h26   #4
Todd Benson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Newbie question - how to write out specific bytes to an IOstream?

On 11/6/07, yermej <yermej@gmail.com> wrote:
> On Nov 6, 6:42 pm, Collin VanDyck <gluedtomys...@gmail.com> wrote:
> > 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?

>
> 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 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.

  Réponse avec citation
Vieux 07/11/2007, 02h32   #5
Collin VanDyck
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Newbie question - how to write out specific bytes to an IO stream?

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.

  Réponse avec citation
Vieux 07/11/2007, 03h23   #6
Todd Benson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Newbie question - how to write out specific bytes to an IOstream?

On 11/6/07, Collin VanDyck <gluedtomyseat@gmail.com> 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

  Réponse avec citation
Vieux 07/11/2007, 10h29   #7
Alex Young
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Newbie question - how to write out specific bytes to an IOstream?

Todd Benson wrote:
> On 11/6/07, yermej <yermej@gmail.com> wrote:
>> On Nov 6, 6:42 pm, Collin VanDyck <gluedtomys...@gmail.com> wrote:
>>> 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?

>> 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 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"

--
Alex

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 17h33.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,16018 seconds with 15 queries