Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Convert string to decimal to octal

by patt (Scribe)
on Aug 14, 2009 at 10:17 UTC ( #788583=perlquestion: print w/ replies, xml ) Need Help??
patt has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I ask for guidance in my request. I am writing a Java application that calls a Perl application. Among the parameters I'm passing to the called Perl is a String value of what is an octal number. In The Perl software I need to convert that back to octal. What I was considering doing is converting the String to decimal then to octal. Dec to oct is simple using sprintf, but how would I convert from String to decimal? Or is there a way to convert directly to octal? What I am converting is Linux File Permissions, in octal. I am using String to pass the arguments because I have a number of different arguments to pass into the Perl and a String array is the simplest, tidiest way of doing it - one array to be passed rather than several differing values. Perldocs does have a node on oct() - converting 'strings' to octal, however this appears to type 'strings' as integers rather than 'strings' as characters (which is what I will be dealing with).

Comment on Convert string to decimal to octal
Re: String to decimal to octal
by desemondo (Hermit) on Aug 14, 2009 at 10:26 UTC
    umm... I don't think you need to worry about perl converting a sting to a decimal. If I'm understanding what you want to do correctly, Perl will do that conversion autmatically - it all depends on how you use the value - Perl does its magic and silently converts it as required...

    edit
    actually I don't understand why its necessary to do any converting if you're already passing into perl an octal. perhaps you could provide an example?
      Perfect BrowserUK. I understand. Thank you...
Re: String to decimal to octal
by Anonymous Monk on Aug 14, 2009 at 10:26 UTC
    perldoc -f oct
    $ perl -le ' print oct shift ' 0700 448 $ perl -le ' print 0700 ' 448
Re: String to decimal to octal
by BrowserUk (Pope) on Aug 14, 2009 at 10:29 UTC

    You really need to clarify your definitions 'string' and 'octal' through some examples. Octal is always a 'string' representation of a number. Ie. It uses a string of ascii/unicode characters for the digit '0' to '7' to represent the actual number.

    Assuming the input received by the Perl program looks something like this:

    C:\test>perl -le" print $ARGV[ 0 ] " 0666 0666

    Then all you need to do is tell Perl to iterprete the contents of that string as octal (intuatively using the oct built-in function), and Perl will take care of the rest for you:

    C:\test>perl -le"my $n = oct( $ARGV[ 0 ] ); printf qq[decimal %d (as o +ctal: %o)\n], $n, $n " 0666 decimal 438 (as octal: 666)

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Thanks for those replies. I do need to clarify some parts of my post. I am used to coding strongly typed languages like C, C++ and Java. What I am doing in Java is constructing a String array. To do this I am converting various char, integer and an octal values to their string representation and placing these into the String array. This simplifies the passing of these arguments to the called Perl code - one argument to be passed rather than several. The octal value, now converted to its String representation, is what is confusing me. I want to convert this 'String' back to its original octal state. I am unsure how to do it. Like say 0644 convert that from String to decimal gives 644. Convert that decimal to octal gives 1204. Not what I want. I want to keep the original octal value - 644. Java provides a method in class Integer, Integer.parseInt(String, int) where String would be the octal value as a String and int is a radix giving the base to convert to, base 8 in my case. Does Perl provide this functionality?
        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/

        1. The value '0644' is a string being supplied to the perl program as a command line argument. It shows up inside the program (the bit between double quotes here: -le"..."), as $ARGV[ 0 ].
        2. $ARGV[0] is then passed to the built-in function oct, which parses that string ('0644'), and converts it to a number in perl's internal binary representation, and assigns it to the scalar variable $n.
        3. Now, when you use $n, how it will be used will depend upon the context in which you use it.

          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.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://788583]
Approved by broomduster
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (11)
As of 2014-09-17 15:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (90 votes), past polls