CGI/Perl Guide | Learning Center | Forums | Advertise | Login Site Search: in Perl Guide PerlGuru Forums Learning Ctr
 MAIN INDEX SEARCHPOSTS WHO'S ONLINE LOG IN

Home: Perl Programming Help: Beginner:
integer literals

sttring
Deleted

Jan 14, 2001, 1:09 PM

Post #1 of 6 (520 views)
 integer literals Can't Post
I am currently learning perl from the book "Learning Perl". I am up to the section in the scalar chapter pertaining to integer literals. It says the following;

Integer Literals

Integer literals are also straightforward, as in:

12
15
-2004
3485

Don't start the number with a 0, because perl supports octal and hexadecimal (hex) literals. Octal numbers start with a leading 0, and hex numbers start with a leading 0x or 0X. The hex digits A through F (in either case) represent the conventional digit values of 10 through 15. For example:

0377 # 377 octal, same as 255 decimal
-0xff # negative FF hex, same as -255 decimal
Can someone explain this to me as the book doesn't explain it very well and I don't really understand what it means.

Thanks,
sttring

japhy
Enthusiast

Jan 15, 2001, 5:41 PM

Post #2 of 6 (507 views)
 Re: integer literals [In reply to] Can't Post
The number system we use is decimal -- deci meaning "ten", the number of digits we use (0 through 9). A number like 149 is the same as (1 * 10^2) + (4 * 10^1) + (9 * 10^0), or (1 * 100) + (4 * 10) + (9 * 1), which is 100 + 40 + 9.

Octal has eight digits, 0 through 7. Perl distinguishes octal numbers from decimal numbers by starting them with a 0. The number 22 is decimal to Perl, but the number 0225 is octal to Perl, and its equivalent in decimal is (2 * 8^2) + (2 * 8^1) + (5 * 8^0), or (2 * 64) + (2 * 8) + (5 * 1), which is 128 + 16 + 5, or 149.

Hexadecimal has 16 digits, 0 through 9, and then 'A' through 'F', which represent the decimal values 10 through 15. Perl represents these numbers with a leading 0x. The number 95 is decimal to Perl, but the number 0x95 is hexadecimal, and its equivalent in decimal is (9 * 16^1) + (5 * 16^0), or (9 * 16) + (5 * 1), which is 144 + 5, or 149.

Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author

sttring
Deleted

Jan 15, 2001, 6:37 PM

Post #3 of 6 (504 views)
 Re: integer literals [In reply to] Can't Post
I understand most of what you're saying. 2 questions though.

If I understand it correctly, the last number in the octal or hex is multiplied by 8 or 16 to the power of 0 and then the second last one to the power of 1, and the third last to the power of 2, and the fourth last to the power of 3 and so on... Is that correct?

One other question is how do you which numbers are which in some cases. For example:

Hex: 0x1515

How can you tell if this is 0x 1 5 1 5 , each number seperate or 0x 15 15 as in two 15's or could you not even write it with two 15's rather with FF? Also can you use a combination of both letters and numbers in a hex?

Thanks,
sttring

sttring
Deleted

Jan 16, 2001, 3:47 AM

Post #4 of 6 (498 views)
 Re: integer literals [In reply to] Can't Post
One other question as well. Is it possible to reverse it back? To change a decimal into a hex or octal? If so, how would you do it?

Thanks,
sttring

japhy
Enthusiast

Jan 16, 2001, 1:27 PM

Post #5 of 6 (492 views)
 Re: integer literals [In reply to] Can't Post
The hex number 0x1515 is (1 * 16^3) + (5 * 16^2) + (1 * 16^1) + (5 * 16^0). In order to get it to be like 0x(15)(15), you'd use the letter 'f' to mean 15: 0xff.

As for your other question, you can make a string that represents the octal or hexadecimal value of a number by using the sprintf() function:

 Code
`sub as_oct { sprintf "0%o", shift }   sub as_hex { sprintf "0x%x", shift }`
You can then take these strings and use the builtin functions oct() and hex() to convert FROM octal/hex TO decimal:

 Code
`\$n = as_oct(100); print "100 in octal is \$n\n";  # 0144 \$m = oct(\$n); print "\$n in decimal is \$m\n"; # 100   \$n = as_hex(100); print "100 in hex is \$n\n"; # 0x64 \$m = hex(\$n); print "\$n in decimal is \$m\n"; # 100`
Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author

sttring
Deleted

Jan 17, 2001, 6:31 AM

Post #6 of 6 (490 views)
 Re: integer literals [In reply to] Can't Post
Thanks for all the help. Really appreciate it.

Thanks,
sttring

 Announcements     PerlGuru Announcements Perl Programming Help     Frequently Asked Questions     Beginner     Intermediate     Advanced     Regular Expressions     mod_perl     DBI     Win32 Programming Help Fun With Perl     Perl Quizzes - Learn Perl the Fun Way     Perl Golf     Perl Poetry Need a Custom or Prewritten Perl Program?     I need a program that...     I Need a Programmer for Freelance Work     Throw Down The Gauntlet General Discussions     General Questions     Feedback     Tutorial/Article Suggestions for The Learning Cent     Internet Security Other Programming Languages     Javascript     PHP

 Search this forum this category all forums for All words Any words Whole Phrase (options) Powered by Gossamer Forum v.1.2.0