in reply to Re^2: String to decimal to octal
in thread Convert string to decimal to octal
Does Perl provide this functionality?
Yes. As I demonstrated above. And here again:
c:\test>perl -le" my $n = oct( $ARGV[ 0 ] ); printf qq[As decimal: %d As octal: %o \n], $n, $n; " 0644 As decimal: 420 As octal: 644
Let me try and explain what is going on above/
That is to say, if you use $n as a string--for example by concatenating it to another string:
c:\test>perl -le"my $n = oct( $ARGV[ 0 ] ); print 'As a string:' . $n; + " 0644 As a string:420
It will be converted from that internal binary representation automatically, and used as a (decimal) formatted ascii string.
However, if you use it in a numeric context--by adding it to another number--then it will be used as a number:
c:\test>perl -le"my $n = oct( $ARGV[ 0 ] ); print 1 + $n; " 0644 421
Does that clarify things for you?
I realise that if you are used to having to convert between string and numeric representations explicitly, that this automation seems unintuative and leaves you thinking you need to do more, but trust me it works.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: String to decimal to octal
by patt (Scribe) on Aug 15, 2009 at 12:43 UTC |