Re: Hex Question(s)
by kyle (Abbot) on Mar 27, 2008 at 17:03 UTC
|
I think you could do what you want if you create $theHex as an object and use overload. A quick example:
package Number::HexString;
use overload '""' => sub { sprintf '0x%x', ${$_[0]} },
'0+' => sub { ${$_[0]} },
'fallback' => 1;
sub new {
my $class = shift;
my $number = 0+shift;
return bless \$number, $class;
}
package main;
my $theHex = Number::HexString->new( 1501299200 );
print "$theHex plus 1 is ", $theHex + 1, "\n";
print 'The number ', $theHex+0, " is '$theHex'\n";
__END__
0x597c0200 plus 1 is 1501299201
The number 1501299200 is '0x597c0200'
This really seems like overkill, however, and you may have to fiddle with it a while before it does what you really want. | [reply] [d/l] [select] |
Re: Hex Question(s)
by Corion (Patriarch) on Mar 27, 2008 at 16:15 UTC
|
| [reply] |
|
That's the question I guess - is there any? Doesn't a hex value normally begin with 0x? And it isn't it normally specified without quotes?
$hex = 0x1234; #prints 4660
as opposed to$hex = '0x1234'; #prints 0x1234
| [reply] [d/l] [select] |
|
What I was aiming at is, that Perl makes no distinction between the base of numbers. You may specify a number in hexadecimal notation, that is, starting with 0x, or as a plain integer, that is, in decimal notation, or in octal notation, starting with a leading zero. Internally, Perl will treat all these as numbers, because they get converted to the Perl-internal representation for numbers.
If you have a string and you want to perform arithmetic operations on it, the easiest way to do that is if the string looks_like_number, that is, is in decimal notation (at least for this discussion). Otherwise, you have to convert from the notation you have to the decimal notation. For example, the functions hex and oct do that.
But maybe, we can just leave the plane of metaphysical discussions of the representation of numbers, and the numbers themselves (and whether numbers exist without representation) and you tell us what problem you're trying to solve?
| [reply] [d/l] [select] |
|
|
Re: Hex Question(s)
by pc88mxer (Vicar) on Mar 27, 2008 at 16:20 UTC
|
Is the hex() function what you are looking for?
my $theHex = "100";
print hex($theHex), "\n"; # prints: 256
| [reply] [d/l] [select] |
|
print 0x1234+0x4321; # returns 21605
print '0x1234'+'0x1234'; returns 0 because you can't concatenate strin
+gs in this way
| [reply] [d/l] |
|
No, Perl's treating it like any other hex string in that it's not a valid number. Perl understands hex numeric literals, but they're silently converted to decimal under the hood. If you want to do manipulation of numbers then you need to use hex to parse the number to decimal and then only reformat as hex on the way out after you're done manipulating it.
Update: Ooh, better said here up above.
The cake is a lie.
The cake is a lie.
The cake is a lie.
| [reply] |
|
Are you sure that hex isn't what you're looking for?
perl -le "print hex('1234') + hex('4321')"
21845
perl -le "print hex('0x1234') + hex('0x4321')"
21845
| [reply] [d/l] |
Re: Hex Question(s)
by YuckFoo (Abbot) on Mar 27, 2008 at 16:28 UTC
|
Prepend '0x' to the string and it will maintain its numeric value.
#!/usr/bin/perl
use strict;
no warnings 'numeric';
my $this = sprintf("0x%x", 40);
my $that = sprintf("0x%x", 2);
my $other = $this + $that;
printf "$other %x\n", $other;
__END__
42 2a
Update: Added no warnings for the -w happy...
Update: This is perl, v5.6.0 built for i386-linux | [reply] [d/l] |
|
C:\>perl -w 676783.pl
Argument "0x2" isn't numeric in addition (+) at 676783.pl line 8.
Argument "0x28" isn't numeric in addition (+) at 676783.pl line 8.
0 0
This oneliner shows that this isn't the case:
>perl -le "print '0x42' + '0x10'"
0
But of course, if you leave off the single quotes, Perl will interpret the numbers as numbers (in hexadecimal notation) and add them like it does add all numbers (regardless of their notation):
>perl -le "print 0x42 + 0x10"
82
... but that's because internally, Perl does just see the numbers as numbers (without any representation to any base).
| [reply] [d/l] [select] |
|
C:\> test.pl
Argument "0x2" isn't numeric in addition (+) at C:\test.pl line 9.
Argument "0x28" isn't numeric in addition (+) at C:\test.pl line 9.
0 0
You can specify hexadecimals in literal code by appending an '0x' to the value. I suppose, if you really wanted to get to a hexadecimal from a string, you could use string eval.
my $this = sprintf("0x%x", 40);
my $that = sprintf("0x%x", 2);
my $other = eval "$this + $that";
printf "$other %x\n", $other;
I'd be inclined to go with hex, though.
| [reply] [d/l] [select] |
|
perl -w test.pl
Argument "0x2" isn't numeric in addition (+) at test.pl line 7.
Argument "0x28" isn't numeric in addition (+) at test.pl line 7.
0 0
| [reply] [d/l] |
Re: Hex Question(s)
by nikosv (Deacon) on Mar 28, 2008 at 06:55 UTC
|
the oct function converts a string into a number.
prepend 0x to your string an pass it through the oct function
print 0x1234+0x4321;#prints 21845
print '0x1234'+'0x4321';#prints 0
print oct('0x1234')+oct('0x4321); #prints 21845
or
$a='0x1234';
$b='0x4321';
print oct($a)+oct($b);
| [reply] [d/l] |