what is this syntax: \001\002 ?

7

7stud --

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?
 
D

Daniel Lucraft

7stud said:
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
 
F

F. Senault

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
 
L

Luis Parravicini

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

Daniel Lucraft

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
 
7

7stud --

F. Senault said:
Le 12 septembre � 13:10, 7stud -- a �crit :


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

7stud --

Daniel said:
Daniel Lucraft wrote:In octal

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

Gaspard Bucher

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

Jonas Roberto de Goes Filho (sysdebug)

Gaspard said:
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,
 
M

Matthias Wächter

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
 
A

Alex Young

Jonas said:
Gaspard said:
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!
8 doesn't exist in octal, so it interprets the 8 as a separate character.

irb(main):009:0> "\007".length
=> 1
irb(main):010:0> "\008".length
=> 2
 
R

Rob Biedenharn

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

This is pure history. The use of octal notation predates hex by a =20
long time. In my "The C Programming Language" [1978], 0ddd and 0xddd =20=

were both valid integer literals, but only octal \ddd was valid for =20
character constants.

In fact, there's a table of internal sizes for data types on various =20
machines which includes the Honeywell 6000 having a 9-bit char type =20
and 36-bit types for short, int, long, and float and 72-bit doubles. =20=

For that machine, octal representation of bit patterns makes perfect =20
sense.

-Rob

Rob Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 
M

Matthias Wächter

This is pure history.

Well, I understand that in history it was a popular format for
various reasons. But Ruby is now, and now we have 8 bit chars (aka
Bytes) and use hex nearly exclusively for them. Why not so in
String#inspect?

Or is the reason similar to the question why Float#to_s uses 15
instead of 17 significant bits to display? Pure convention? A (core)
developer rolling a dice somewhen? ;-)

- Matthias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top