CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
integer literals

 



sttring
Deleted

Jan 14, 2001, 1:09 PM

Post #1 of 6 (509 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;


In Reply To
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 (496 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 (493 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 (487 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 (481 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 (479 views)
Re: integer literals [In reply to] Can't Post

Thanks for all the help. Really appreciate it.

Thanks,
sttring


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives