[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

what is this syntax: \001\002 ?

7stud 7stud

9/12/2007 11:11:00 AM

Hi,

On p. 131-132 in "Programming Ruby 2d.", it talks about writing binary
data to files, but there's no explanation of the syntax:

str1 = "\001\002\003"

I looked in the index of the book, I poured over "The Ruby Language"
chapter, and I also couldn't find any escape character that was "\0".
Is that Ruby's unicode syntax? How about a ding dang explanation Mr.
Thomas?
--
Posted via http://www.ruby-....

13 Answers

Daniel Lucraft

9/12/2007 11:40:00 AM

0

7stud -- wrote:
> Hi,
>
> On p. 131-132 in "Programming Ruby 2d.", it talks about writing binary
> data to files, but there's no explanation of the syntax:
>
> str1 = "\001\002\003"
>

This is an octal escape sequence. Use it to insert an arbitrary byte
into a string.

The escape code is \n, \nn or \nnn where each n is from 0 to 7. In octal
000 is 0 and 777 is 255.

best,
Dan
--
Posted via http://www.ruby-....

F. Senault

9/12/2007 11:41:00 AM

0

Le 12 septembre à 13:10, 7stud -- a écrit :

> I looked in the index of the book, I poured over "The Ruby Language"
> chapter, and I also couldn't find any escape character that was "\0".
> Is that Ruby's unicode syntax? How about a ding dang explanation Mr.
> Thomas?

You should look in the 3rd part, chapter 22, under "The Basic Types".
Table 22.2, page 306 (in my PDF edition).

It says :

| Table 22.2. Substitutions in double-quoted strings
| --------------------------------------------------------------
| \a Bell / alert (0x07) \nnn Octal nnn
| \b Backspace (0x08) \xnn Hex nn
| \e Escape (0x1b) \cx Control-x
| \f Formfeed (0x0c) \C-x Control-x
| \n Newline (0x0a) \M-x Meta-x
| \r Return (0x0d) \M-\C-x Meta-control-x
| \s Space (0x20) \x x
| \t Tab (0x09) #{code} Value of code
| \v Vertical tab (0x0b)
| --------------------------------------------------------------

So, your example uses the octal representation.

Fred
--
If my son wants to be a pimp when he grows up, that's fine with me. I
hope he's a good one and enjoys it and doesn't get caught. I'll support
him in this. But if he wants to be a network administrator, he's out of
the house and not part of my family. (Steve Wozniak)

Luis Parravicini

9/12/2007 11:46:00 AM

0

On 9/12/07, 7stud -- <dolgun@excite.com> wrote:
> Hi,
>
> On p. 131-132 in "Programming Ruby 2d.", it talks about writing binary
> data to files, but there's no explanation of the syntax:
>
> str1 = "\001\002\003"
>
> I looked in the index of the book, I poured over "The Ruby Language"
> chapter, and I also couldn't find any escape character that was "\0".
> Is that Ruby's unicode syntax?

Hi
That's the way of expressing octal numbers.


--
Luis Parravicini
http://ktulu.co...

Daniel Lucraft

9/12/2007 11:55:00 AM

0

Daniel Lucraft wrote:In octal
> 000 is 0 and 777 is 255.

No it very much isn't. 377 is what I was going for there. Thank you for
being too polite to say...

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

7stud 7stud

9/12/2007 11:56:00 AM

0

F. Senault wrote:
> Le 12 septembre � 13:10, 7stud -- a �crit :
>
>> I looked in the index of the book, I poured over "The Ruby Language"
>> chapter, and I also couldn't find any escape character that was "\0".
>> Is that Ruby's unicode syntax? How about a ding dang explanation Mr.
>> Thomas?
>
> You should look in the 3rd part, chapter 22, under "The Basic Types".
> Table 22.2, page 306 (in my PDF edition).

Ah. I didn't realize there was a second column on the right.

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

7stud 7stud

9/12/2007 12:01:00 PM

0

Daniel Lucraft wrote:
> The escape code is \n, \nn or \nnn where each n is from 0 to 7. In octal
> 000 is 0 and 777 is 255.
>

777 is 255 ?
--
Posted via http://www.ruby-....

7stud 7stud

9/12/2007 12:24:00 PM

0

Daniel Lucraft wrote:
> Daniel Lucraft wrote:In octal
>> 000 is 0 and 777 is 255.
>
> No it very much isn't. 377 is what I was going for there. Thank you for
> being too polite to say...
>
> :(
> Dan

Try again.

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

Gaspard Bucher

9/12/2007 12:26:00 PM

0

A byte is coded on 8 bits, so the maximum is 255 hence the strange
results below :

irb> 8 * 8 * 7 + 8 * 7 + 7
=> 511
irb> "\777".inspect
=> "\377"
irb> "\777"[0]
=> 255
irb> "\377"[0]
=> 255

\777 is invalid and is truncated to \377.

Jonas Roberto de Goes Filho (sysdebug)

9/12/2007 12:46:00 PM

0

Gaspard Bucher wrote:
> A byte is coded on 8 bits, so the maximum is 255 hence the strange
> results below :
>
> irb> 8 * 8 * 7 + 8 * 7 + 7
> => 511
> irb> "\777".inspect
> => "\377"
> irb> "\777"[0]
> => 255
> irb> "\377"[0]
> => 255
>
> \777 is invalid and is truncated to \377.
>
>
>
How I represent a number eight?

sysdebug(main):024:0> "\008"[0]
=> 0

This returned 0!

Regards,

--
Jonas Roberto de Goes Filho (sysdebug)
http://g...


Matthias Wächter

9/12/2007 12:49:00 PM

0

On 12.09.2007 13:45, F. Senault wrote:
> You should look in the 3rd part, chapter 22, under "The Basic Types".
> Table 22.2, page 306 (in my PDF edition).
>
> It says :
>
> | Table 22.2. Substitutions in double-quoted strings
> | --------------------------------------------------------------
> | \a Bell / alert (0x07) \nnn Octal nnn
> | \b Backspace (0x08) \xnn Hex nn
> | \e Escape (0x1b) \cx Control-x
> | \f Formfeed (0x0c) \C-x Control-x
> | \n Newline (0x0a) \M-x Meta-x
> | \r Return (0x0d) \M-\C-x Meta-control-x
> | \s Space (0x20) \x x
> | \t Tab (0x09) #{code} Value of code
> | \v Vertical tab (0x0b)
> | --------------------------------------------------------------
>
> So, your example uses the octal representation.

Does anyone know a good reason for outputting 8 bit byte characters
as octals in String#inspect?

see string.c (1.8.6):

[...]
else {
sprintf(s, "\\%03o", c & 0377);
rb_str_buf_cat2(result, s);
}
[...]

why not make it:

sprintf(s, "\\x%02x", c & 0377);

I can't understand why it is desirable to introduce yet another base
that is rarely used outside of chmod and od (without options). We
learned decimals, we got used to binary and even hex, but why do we
need octals just for these control characters when there is no
benefit compared to the hex representation (note: both take 4 chars
to display)?

- Matthias