This forum is now read-only. Please use our new forums! Go to forums
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
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
Answer 51adf439c2d9cdb5fb003262
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.
Answer 53c5b2b89c4e9d62d6000c3e
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.
Answer 524211a580ff3370430009e1
Popular free courses
- Free Course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner friendly,4 LessonsLanguage Fluency - Free Course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner friendly,11 LessonsLanguage Fluency - Free Course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner friendly,6 LessonsLanguage Fluency