string to integer

M

Mark Fenbers

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
 
T

Tony Curtis

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
 
M

Mark Fenbers

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
 
T

Tad McClellan

Mark Fenbers said:
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

:)
 
B

Bart Lateur

Mark said:
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.
 
M

Mark Fenbers

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
 
H

Helgi Briem

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

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.
 
T

Tad McClellan

Mark Fenbers said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,602
Members
45,185
Latest member
GluceaReviews

Latest Threads

Top