in reply to Re^4: ' or no?
in thread ' or no?

Thanks all gurus! I admit language is a barrier to communicate with monks,especially for a perl beginner:)
why
print oct(0xff); # 173
? because oct function require a "string" parameter,So perl convert(stealthily) 0xff(number) to 255(string),then oct convert 255(octal) to 173(decimal). right?I hopefully don't misunderstand reply being provided.
But you have to admit it's a trap to a beginner.Although I add "use warnings" and" use strict",perl don't give any useful messages.It just return 173. For me,I prefer to choose print(printf):
my $num = 0xff; print oct($num); #wrong! printf('xo',$num);# right!
p.s. maybe I can use pack,but it's another issue.

Replies are listed 'Best First'.
Re^6: ' or no?
by ikegami (Patriarch) on Dec 28, 2006 at 18:07 UTC

    oct converts *from* oct/hex/bin (oct in your case).
    Conversion from dec is done automatically when needed.
    printf converts *to* dec/oct/hex/bin.

    >perl -le "printf '%d', '255' 255 >perl -le "printf '0%o', oct '0377' 0377 >perl -le "printf '0x%X', oct '0xFF' 0xFF >perl -le "printf '0b%b', oct '0b11111111' 0b11111111