[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

healp reading / writing binary strings.

warhero

6/18/2007 1:52:00 AM

Hey, I'm looking for some help with reading / writing binary string.
Just need some help understanding what exactly is going on..

if I have a string like:
str = "\t\005\001\006\afoo\006\abar"

Or if I have something like:
str =
"\t\025\001\004\001\004\002\004\003\004\004\004\005\004\006\004\a\004\b\004\t\004\n"

What exactly kind of format is that? How do I parse it / read binary
data from it? And how can I write the same type of string from say an
array? ["foo","bar"]?

thanks
Melissa

--
Posted via http://www.ruby-....

6 Answers

Robert Klemme

6/18/2007 6:52:00 AM

0

On 18.06.2007 03:52, Melissa Silas wrote:
> Hey, I'm looking for some help with reading / writing binary string.
> Just need some help understanding what exactly is going on..
>
> if I have a string like:
> str = "\t\005\001\006\afoo\006\abar"
>
> Or if I have something like:
> str =
> "\t\025\001\004\001\004\002\004\003\004\004\004\005\004\006\004\a\004\b\004\t\004\n"
>
> What exactly kind of format is that?

Um, how can we know what format this is? I mean, you came up with that
string. The definition above just uses octal escapes. You can as well
have hex codes:

irb(main):013:0> "\x21\x20\x40"
=> "! @"

> How do I parse it / read binary
> data from it?

Depends on what you want to do with it. If you want to access bytes you
can simply do this

irb(main):014:0> s = "\t\001"
=> "\t\001"
irb(main):015:0> s[0]
=> 9
irb(main):016:0> s[1]
=> 1

If you want bits you can do

irb(main):017:0> s.unpack "b*"
=> ["1001000010000000"]
irb(main):018:0> s.unpack "b8b8"
=> ["10010000", "10000000"]
irb(main):019:0> s.unpack "b4*"
=> ["1001"]
irb(main):020:0> s.unpack "b4b4b4b4"
=> ["1001", "1000", "", ""]
irb(main):021:0> s.unpack "b4b4b"
=> ["1001", "1000", ""]
irb(main):022:0> s.unpack "b4b4"
=> ["1001", "1000"]

Please see #unpack docs for more info.

> And how can I write the same type of string from say an
> array? ["foo","bar"]?

"Write from an array"? I don't understand what you mean. If you want to
put it into an array, you can simply do

irb(main):023:0> ["foo", "\tbar\n"]
=> ["foo", "\tbar\n"]

Kind regards

robert

Dejan Dimic

6/18/2007 6:54:00 AM

0

The string that you can't understand contains the unprintable
characters.
It is probably that you got if from some type of serialization.
You can say that is in a binary format but it's not so good definition
because everything is in binary format - some we can read and some we
can not.

For the second question there is lot of strategies how to store the
array in a file and it most depend what do you want to do latter with
that stored information.
Nowadays is custom to use xml serialization, but you can use YAML or
JSON. They are all human readable.

On Jun 18, 3:52 am, Melissa Silas <beingthexemplaryli...@gmail.com>
wrote:
> Hey, I'm looking for some help with reading / writing binary string.
> Just need some help understanding what exactly is going on..
>
> if I have a string like:
> str = "\t\005\001\006\afoo\006\abar"
>
> Or if I have something like:
> str =
> "\t\025\001\004\001\004\002\004\003\004\004\004\005\004\006\004\a\004\b\004\t\004\n"
>
> What exactly kind of format is that? How do I parse it / read binary
> data from it? And how can I write the same type of string from say an
> array? ["foo","bar"]?
>
> thanks
> Melissa
>
> --
> Posted viahttp://www.ruby-....


warhero

6/18/2007 3:18:00 PM

0

Robert Klemme wrote:
> On 18.06.2007 03:52, Melissa Silas wrote:
>> What exactly kind of format is that?
> Um, how can we know what format this is? I mean, you came up with that
> string. The definition above just uses octal escapes. You can as well
> have hex codes:
>
> irb(main):013:0> "\x21\x20\x40"
> => "! @"
>
> > How do I parse it / read binary
>> data from it?
>
> Depends on what you want to do with it. If you want to access bytes you
> can simply do this
>
> irb(main):014:0> s = "\t\001"
> => "\t\001"
> irb(main):015:0> s[0]
> => 9
> irb(main):016:0> s[1]
> => 1
>
> If you want bits you can do
>
> irb(main):017:0> s.unpack "b*"
> => ["1001000010000000"]
> irb(main):018:0> s.unpack "b8b8"
> => ["10010000", "10000000"]
> irb(main):019:0> s.unpack "b4*"
> => ["1001"]
> irb(main):020:0> s.unpack "b4b4b4b4"
> => ["1001", "1000", "", ""]
> irb(main):021:0> s.unpack "b4b4b"
> => ["1001", "1000", ""]
> irb(main):022:0> s.unpack "b4b4"
> => ["1001", "1000"]
>
> Please see #unpack docs for more info.
>
> > And how can I write the same type of string from say an
>> array? ["foo","bar"]?
>
> "Write from an array"? I don't understand what you mean. If you want to
> put it into an array, you can simply do
>
> irb(main):023:0> ["foo", "\tbar\n"]
> => ["foo", "\tbar\n"]
>
> Kind regards
>
> robert


Thanks that helps.. Couple more questions..

for this example:
s = "\t\001"
puts s[0] -> 9
How does \t come out as 9? Is that the tab char which in turn is the
number 9 in ascii table?

for this example:
s = "\001"
Ok so now I know this is octal, does the octal value I supply correspond
to the ascii table as well?

for this example:
s = "\x01"
for hex, does the hex value I supply correspond to the ascii table as
well?

Thanks for the help. This is helping me understand this a bunch..

-Melissa

--
Posted via http://www.ruby-....

Todd Benson

6/18/2007 4:43:00 PM

0

On 6/18/07, Melissa Silas <beingthexemplarylists@gmail.com> wrote:
> Thanks that helps.. Couple more questions..
>
> for this example:
> s = "\t\001"
> puts s[0] -> 9
> How does \t come out as 9? Is that the tab char which in turn is the
> number 9 in ascii table?

It's just the number 9, but yes you can look at it that way.
irb(main):001:0> 9.chr
=> "\t"

>
> for this example:
> s = "\001"
> Ok so now I know this is octal, does the octal value I supply correspond
> to the ascii table as well?

irb(main):002:0> 1.chr
=> "\001"
irb(main):003:0> 255.chr
=> "\377"
irb(main):004:0> 256.chr
RangeError: 256 out of char range
from (irb):24:in 'chr'
from (irb):24
from :0

(I like that last "from" line :)

> for this example:
> s = "\x01"
> for hex, does the hex value I supply correspond to the ascii table as
> well?

irb(main):005:0> "\x61"
=> "a"
irb(main):006:0> ?a
=> 97

Your bytes are unprintable and so ruby simply tells you the byte
value. What you do with that value is up to you.

fformby

1/18/2010 8:34:00 PM

0

On Mon, 18 Jan 2010 18:59:34 +0000 (UTC), The ForeskinPeeler
<peelingtheforeskin@Use-Author-Supplied-Address.invalid> wrote:

>On Mon, 18 Jan 2010 16:14:54 GMT, The Revd wrote:
>
><snip the usual insane garbage by the nithing>

"Nithing"??? The illiterate Grik pig has picked up a 'word' from
mex/spic/gook Ejerkito's 'vocabulary'! LOL

>So, where are the sources for your insane statements, you lying Nazi swine?

WE ask the questions, Grik cocksucker.

<Bugger Grease>

The Peeler

1/18/2010 10:51:00 PM

0

On Mon, 18 Jan 2010 20:34:19 GMT, The Retd wrote:

> On Mon, 18 Jan 2010 18:59:34 +0000 (UTC), The ForeskinPeeler
> <peelingtheforeskin@Use-Author-Supplied-Address.invalid> wrote:
>
>>On Mon, 18 Jan 2010 16:14:54 GMT, The Revd wrote:
>>
>><snip the usual insane garbage by the nithing>
>
> "Nithing"??? The illiterate Grik pig has picked up a 'word' from
> mex/spic/gook Ejerkito's 'vocabulary'! LOL

Well, the term describes you to a "t". From the Oxford Dictionary:

"nithing - A vile coward; an abject or contemptible person; a villain.
Formerly also, a mean or miserly person; a niggard."

>>So, where are the sources for your insane statements, you lying Nazi swine?
>
> WE ask the questions, Grik cocksucker.
>
> <Bugger Grease>

<BG> YOU will only get your bleeding psychopathic ass clobbered here, time
and again.