string to integer

Discussion in 'Perl Misc' started by Mark Fenbers, Sep 17, 2003.

  1. Mark Fenbers

    Mark Fenbers Guest

    I have an interesting problem...

    Perl seems to convert hex numbers which are explicit constants in the code just
    fine. For example,

    my $val = 0xFF;

    is no different from

    my $val = 255;

    However, if $val is assigned from reading 0xFF from a file or user input, this
    natural conversion seems not to occur. $val is treated like a string instead of
    a number. I am required to "use strict;" and so I get warnings about this when
    I use $val as an argument to a third party (COTS) subroutine that expects a
    number instead of a string, e.g., 'Argument "0xFF" isn't numeric in subroutine
    entry at (eval 2) line 10.'

    I tried something like this:

    int($val)

    but get a similar error:

    'Argument "0xFF" isn't numeric in int at myscript.pl line 98.'

    The bottom line is how can I convert a string into an integer or a float??

    Mark
     
    Mark Fenbers, Sep 17, 2003
    #1
    1. Advertising

  2. Mark Fenbers

    Tony Curtis Guest

    >> On Wed, 17 Sep 2003 10:06:00 -0400,
    >> Mark Fenbers <> said:


    > I have an interesting problem... Perl seems to convert
    > hex numbers which are explicit constants in the code
    > just fine. For example,


    > my $val = 0xFF;


    > is no different from


    > my $val = 255;


    > However, if $val is assigned from reading 0xFF from a
    > file or user input, this natural conversion seems not to
    > occur. $val is treated like a string instead of a


    It *is* a string, consisting of the characters

    '0' 'x' 'F' 'F'

    Actually, you'll be mortified to know you've answered
    your own question:

    perldoc -f hex

    and more generally

    perldoc -q convert

    hth
    t
     
    Tony Curtis, Sep 17, 2003
    #2
    1. Advertising

  3. Mark Fenbers

    Mark Fenbers Guest

    Ah ha!!

    hex($val) instead of int($val)...

    This solves my problem partially, but I was actually hoping for a solution to
    accept either hex or decimal formats (e.g., 0xff or 255). If there is none, I
    can add a few lines of code to test for '0x' and use hex() if found...

    Mark

    Tony Curtis wrote:

    > >> On Wed, 17 Sep 2003 10:06:00 -0400,
    > >> Mark Fenbers <> said:

    >
    > > I have an interesting problem... Perl seems to convert
    > > hex numbers which are explicit constants in the code
    > > just fine. For example,

    >
    > > my $val = 0xFF;

    >
    > > is no different from

    >
    > > my $val = 255;

    >
    > > However, if $val is assigned from reading 0xFF from a
    > > file or user input, this natural conversion seems not to
    > > occur. $val is treated like a string instead of a

    >
    > It *is* a string, consisting of the characters
    >
    > '0' 'x' 'F' 'F'
    >
    > Actually, you'll be mortified to know you've answered
    > your own question:
    >
    > perldoc -f hex
    >
    > and more generally
    >
    > perldoc -q convert
    >
    > hth
    > t
     
    Mark Fenbers, Sep 17, 2003
    #3
  4. Mark Fenbers <> wrote:

    > This is a multi-part message in MIME format.



    Why is this is a multi-part message in MIME format?

    Usenet is a plain text medium.

    Using MIME means that less people will see your posts.


    > I have an interesting problem...



    No, you have discovered a very fundamental aspect of computer science,
    the difference between "code" and "data".


    > Perl seems to convert hex numbers which are explicit constants in the code



    Because then it is "code".


    > if $val is assigned from reading 0xFF from a file or user input,



    Because then it is "data".


    > $val is treated like a string instead of
    > a number.



    Good, since it *is* a string instead of a number.


    > I am required to "use strict;" and so I get warnings



    "use strict" does NOT issue warnings.

    "use warnings" issues warnings.


    > The bottom line is how can I convert a string into an integer or a float??



    You can't.

    You can, however, convert a hex string into the corresponding
    numerical value. The answer to that question ends up sounding
    like you are joking with us...

    perldoc -f hex

    :)


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Sep 17, 2003
    #4
  5. Mark Fenbers

    Bart Lateur Guest

    Mark Fenbers wrote:

    >hex($val) instead of int($val)...
    >
    >This solves my problem partially, but I was actually hoping for a solution to
    >accept either hex or decimal formats (e.g., 0xff or 255). If there is none, I
    >can add a few lines of code to test for '0x' and use hex() if found...


    I'd also like to point towards oct(). By contrast, oct() is a little
    smarter, it can do not just octal, but binary and hex conversions too,
    with the proper letter flag. So my test would be:

    $int = $val =~ /^0/ ? oct($val) : 0+$val;

    You can try it with "123", "0177" (=127), "0xFF" (=255) and "0b1101"
    (=13), for example:

    foreach my $val (qw(123 0177 0xFF 0b1101)) {
    printf "'%s' is %d\n", $val, $val =~ /^0/ ? oct($val) : 0+$val;
    }

    This simple test will only work for integers, though: "0.123" isn't
    supposed to be treated as octal.

    --
    Bart.
     
    Bart Lateur, Sep 18, 2003
    #5
  6. Mark Fenbers

    Mark Fenbers Guest

    I don't post very often to newsgroups because there always has to be some
    condescending know-it-all to chime in his two cents. Someone always finds a way
    to be rude... Tad, your comments were critical, not helpful. *sigh* I guess I
    expected better etiquette from a professional...

    Mark

    Tad McClellan wrote:

    > Mark Fenbers <> wrote:
    >
    > > This is a multi-part message in MIME format.

    >
    > Why is this is a multi-part message in MIME format?
    >
    > Usenet is a plain text medium.
    >
    > Using MIME means that less people will see your posts.
    >
    > > I have an interesting problem...

    >
    > No, you have discovered a very fundamental aspect of computer science,
    > the difference between "code" and "data".
    >
    > > Perl seems to convert hex numbers which are explicit constants in the code

    >
    > Because then it is "code".
    >
    > > if $val is assigned from reading 0xFF from a file or user input,

    >
    > Because then it is "data".
    >
    > > $val is treated like a string instead of
    > > a number.

    >
    > Good, since it *is* a string instead of a number.
    >
    > > I am required to "use strict;" and so I get warnings

    >
    > "use strict" does NOT issue warnings.
    >
    > "use warnings" issues warnings.
    >
    > > The bottom line is how can I convert a string into an integer or a float??

    >
    > You can't.
    >
    > You can, however, convert a hex string into the corresponding
    > numerical value. The answer to that question ends up sounding
    > like you are joking with us...
    >
    > perldoc -f hex
    >
    > :)
    >
    > --
    > Tad McClellan SGML consulting
    > Perl programming
    > Fort Worth, Texas
     
    Mark Fenbers, Sep 18, 2003
    #6
  7. Mark Fenbers

    Helgi Briem Guest

    On Thu, 18 Sep 2003 07:28:59 -0400, Mark Fenbers
    <> wrote:

    Don't top-post. Top-posting is rude and severely
    damages your chances of having your posts
    read by a competent professional. If you do it
    again you will be killfiled by everybody left worth
    listening to. Many have already done so.

    For posting guidelines ot comp.lang.perl.misc,
    read the regular Posting Guidelines:

    http://mail.augustmail.com/~tadmc/clpmisc.shtml

    For more information about netiquette in general,
    see the "Netiquette Guidelines" at:

    http://andrew2.andrew.cmu.edu/rfc/rfc1855.html

    >This is a multi-part message in MIME format.


    Don't do that. Usenet is a text-only medium.
    Posting in MIME is rude and severely
    damages your chances of having your posts
    read by a competent professional. If you do it
    again you will be killfiled by everybody left worth
    listening to. Many have already done so.

    >I don't post very often to newsgroups because there
    >always has to be some condescending know-it-all to
    >chime in his two cents.


    Yes, that describes you perfectly.

    >Someone always finds a way to be rude...


    Indeed.

    >Tad, your comments were critical, not helpful. *sigh* I guess I
    >expected better etiquette from a professional...


    Tad's comments are invariably helpful and professional.
    If you bothered to read them and follow them you would
    find out exactly how helpful they are. Instead you
    choose to whine and complain and derive no benefit
    at all.

    I could find not a single comment in his post which is
    not 100% accurate and if followed, would help
    you solve your problem quickly. Now go and
    read perldoc -f hex and stop whining.

    And don't top post in MIME again.
    --
    Helgi Briem hbriem AT simnet DOT is

    Excuse the munged address. My last
    e-mail address was killed by spammers.
     
    Helgi Briem, Sep 18, 2003
    #7
  8. Mark Fenbers <> wrote:

    > I don't post very often to newsgroups



    Then isn't it possible that there are aspects of Usenet that
    you are unaware of?

    I post to newsgroups rather often, so I _am_ aware of them.

    I thought I'd share what I have learned with you, but you seem
    resistant to learning how to get the most out of Usenet...


    > because there always has to be some
    > condescending know-it-all to chime in his two cents.



    Some folks will be able to forgive you for that, others will
    just ignore every post that you make in the future.


    > Someone always finds a way
    > to be rude...



    Posting MIME is seen as rude on Usenet.

    Top-posting is seen as rude on Usenet.

    Asking about hex values without even bothering to see if there is a
    function named hex() is seen as rude as well.

    People that _do_ use newsgroups often will recognize who is
    being rude here...


    > Tad, your comments were critical, not helpful.



    You needed the hex() function. I told you about the hex() function.
    How is that not helpful?

    Your posts are being ignored by the people most qualified to answer
    your questions, I told you how to avoid being ignored. That seems
    "helpful" to me too...


    > *sigh* I guess I
    > expected better etiquette from a professional...



    You can expect rudeness to be repaid in kind.

    If you hope to avoid rudeness, then avoid being rude yourself
    in the first place.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Sep 18, 2003
    #8
  9. Mark Fenbers

    Bart Lateur Guest

    Helgi Briem wrote:

    >For posting guidelines ot comp.lang.perl.misc,
    >read the regular Posting Guidelines:
    >
    >http://mail.augustmail.com/~tadmc/clpmisc.shtml


    Note to the OP: even though this page is written by and posted on the
    website of Tad McClellan, here also present, the regulars here have
    generally approved it. These aren't Tad's rules, these are OUR rules.

    --
    Bart.
     
    Bart Lateur, Sep 18, 2003
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Sm9l?=

    CType(x,Integer) vs. Integer.Parse(x)

    =?Utf-8?B?Sm9l?=, Feb 6, 2006, in forum: ASP .Net
    Replies:
    7
    Views:
    5,958
    =?Utf-8?B?RGF2aWQgQW50b24=?=
    Feb 7, 2006
  2. =?ISO-8859-1?Q?Thomas_Gagn=E9?=

    No Math.min(Integer, Integer)?

    =?ISO-8859-1?Q?Thomas_Gagn=E9?=, Jul 29, 2003, in forum: Java
    Replies:
    0
    Views:
    513
    =?ISO-8859-1?Q?Thomas_Gagn=E9?=
    Jul 29, 2003
  3. Sebastian Stelzer

    How do I add an Integer to another Integer?

    Sebastian Stelzer, Oct 14, 2004, in forum: Java
    Replies:
    2
    Views:
    501
    Yu SONG
    Oct 15, 2004
  4. Sebastian Stelzer

    How do I add an Integer to another Integer?

    Sebastian Stelzer, Oct 14, 2004, in forum: Java
    Replies:
    6
    Views:
    45,464
    JavaBean2010
    Apr 7, 2010
  5. Randy Kramer
    Replies:
    12
    Views:
    378
    Robert Klemme
    Oct 25, 2007
Loading...

Share This Page