perl function oct
The oct()
function in Perl is used to convert a string representing an octal number into a decimal number. It takes a single argument, which is the string to be converted.
Here's an example:
referto:theitroad.commy $octal_str = '17'; my $decimal_num = oct($octal_str); print "$octal_str in octal is $decimal_num in decimal.\n";
In this example, we declare a variable $octal_str
and assign it the value '17'
, which represents the octal number 17. We then call the oct()
function with $octal_str
as the argument, and assign the result to a variable $decimal_num
. Finally, we print the original string and the converted decimal number.
The output of this program would be:
17 in octal is 15 in decimal.
The oct()
function can also handle more complex strings that include a prefix indicating the number base. For example:
my $octal_str = '0o17'; my $decimal_num = oct($octal_str); print "$octal_str in octal is $decimal_num in decimal.\n";
In this example, we use the 0o
prefix to indicate that the string represents an octal number. The output of this program would be the same as before:
0o17 in octal is 15 in decimal.
Similarly, the 0b
prefix can be used to indicate a binary number, and the 0x
prefix can be used to indicate a hexadecimal number.