This forum is now read-only. Please use our new forums! Go to forums

banner
Close banner
0 points
Submitted by mustak yaqub
about 11 years

Octal to decimal

Just a note. In the code:

my_machine = Machine.new("eric", 01234)

the password 01234 is treated as an octal value and is converted to decimal 668 .

Answer 516e1927a47ed760a7003694

4 votes

Permalink

When I saw it I was like wouldn’t it trim the 0 anyways? Then I ran it and it came out as a weird number, then I stuck a zero on the other password, and it said this cannot be an octal number. Then I came to the faq, and saw your post :D. A password should really be a string in this example

points
Submitted by Joshua Fisk
about 11 years

Answer 51adf439c2d9cdb5fb003262

3 votes

Permalink

I was curious as to why the number is automatically converted to an octal in Ruby. Here is an interesting link where this issue is discussed, the author considers this to be a weakness in Ruby: http://wordaligned.org/articles/octal-literals

And I agree with Joshua, the password should be a string.

points
Submitted by Jonathan Toms
almost 11 years

Answer 53c5b2b89c4e9d62d6000c3e

3 votes

Permalink

Integer literals— constants such as 3, 42, 1024, etc.— are interpreted specially when they start with a 0 (zero). If the number starts with 0x or 0X, it is interpreted to be a hexadecimal number consisting of hex digits 0-9A-F. if it begins with 0b, it is interpreted to by a binary number consisting of binary digits 0 and 1. If it begins with 0 without any subsequent letter, it is interpreted as an octal constant consisting of octal digits 0-7.

    x = [ 0377, 0b11111111, 0xff, 017, 0b1111, 0x0f ] 
    x.each { |n| print "#{n} "}

results in “255 255 255 15 15 15 “

This is common in many programming languages and is, in fact, mandated by many language standards bodies. The origins are likely with AT&T’s early C specification, from which so many languages borrowed a page or more.

points
Submitted by creed.erickson
almost 10 years

Answer 524211a580ff3370430009e1

2 votes

Permalink

Awesome. For a beginner like me it’s nice when someone explains stuff like that that’s not directly related to the answer but still makes you wonder. Thanks!

points
Submitted by alynn826
over 10 years

Answer 513848af9a391431b80017c9

1 vote

Permalink

Good point!

points
Submitted by Alex J
about 11 years

Answer 52a898eb548c35f2fe0001ff

1 vote

Permalink

I wanted to also express my appreciation for this observation as a beginner! These kinds of little side issues are what usually mess me up the most.

points
Submitted by S-M
over 10 years