Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Perl equivalent of JavaScript parseInt()?

by target (Beadle)
on Feb 08, 2002 at 23:58 UTC ( #144288=perlquestion: print w/replies, xml ) Need Help??

target has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to convert a javascript to perl, a silly little utility for password checking (not that I need it to work, I just want to learn how). The section of the script that I am having problems with is
y=3710 v=Math.sqrt(y) v=parseInt(v,16)
The place I'm running into trouble is the parseInt(v,16) line. I looked it up on a java script tutorial site but am having trouble finding a similar function in perl, according to the above link, "The parseInt function parses its first argument, a string, and attempts to return an integer of the specified radix (base)".

Can someone point me to where I might be able to find such a beast in perl? (i.e. a way to get 96 from 60.909769331364245)

My apologies if this question has already been asked, but I was unable to locate information about it from the search box.

Edited by dws for title descriptiveness

Replies are listed 'Best First'.
Re: JavaScript parseInt()
by jryan (Vicar) on Feb 09, 2002 at 00:41 UTC

    There isn't an actual equivalent function. However, take a look at these:

    • int - Gets the integer part of a string
    • hex - Converts a decimal to hex
    • oct - Converts a decimal to octal
    • pack - Converts stuff to all kinds of stuff
    • sprintf - Also converts stuff to all kinds of stuff
      Thanks for the answers guys. The one from dws is the correct answer, but I was really looking for something like the answer from jryan, somewhere to learn about my options.

      Thanks again, for both replies.
Re: JavaScript parseInt()
by dws (Chancellor) on Feb 09, 2002 at 00:39 UTC
    Can someone point me to where I might be able to find such a beast in perl? (i.e. a way to get 96 from 60.909769331364245) For dealing with hex strings,   print hex("60.909769331364245"); displays "96".

Re: Perl equivalent of JavaScript parseInt()?
by Anonymous Monk on Nov 18, 2019 at 19:07 UTC
    This should work like the Java ParseInt function:
    sub parseInt { my ($parse) = @_; my $new=''; my $newvalue=0; my $leng = length ($parse); $leng --; for (my $i=0; $i <=$leng; $i++) { $new = substr($parse,$i, 1); my $exp = $leng-$i; $newvalue += $new * (16**$exp); } return $newvalue; }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2022-08-17 11:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?