CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Add two hexadecimal value

 



csudess
Novice

Apr 12, 2010, 3:12 PM

Post #1 of 4 (2718 views)
Add two hexadecimal value Can't Post

How could i add two hexadecimal number? thank u for ur answer


toolic
User

Apr 12, 2010, 5:08 PM

Post #2 of 4 (2717 views)
Re: [csudess] Add two hexadecimal value [In reply to] Can't Post


Code
use strict; 
use warnings;

my $n1 = 'a';
my $n2 = '9';
my $sum = hex($n1) + hex($n2);
print $sum;

__END__

19



csudess
Novice

Apr 12, 2010, 5:47 PM

Post #3 of 4 (2714 views)
Re: [toolic] Add two hexadecimal value [In reply to] Can't Post

Its good but how could i increment my hexadecimal number like a string i mean here is my number

$numb1="\x64";

and i would like to add one hexadecimal number to the $numb1, but i cant!

i tryed this

$numb1="\x64";
$numb2="\x01";

$value=$numb1+$numb2;
print "$value";

but it didnt work!:(


7stud
Enthusiast

Apr 12, 2010, 8:00 PM

Post #4 of 4 (2706 views)
Re: [csudess] Add two hexadecimal value [In reply to] Can't Post


In Reply To
i mean here is my number

$numb1="\x64";


What do you think the double quote operator does in perl? Does it create numbers?

Let's try a simple test:

Code
use strict; 
use warnings;
use 5.010;

my $numb1 = "\x64";

say $numb1;

--output:--
d


So there is some empirical evidence, right there, that perl's double quote operator returns strings--not numbers. And in fact, the hex escape format \xNN, is used to insert characters into a double quoted *string* using their ascii codes.

perl provides another hexidecimal format when you want to create *numbers* using hex format in your source code:


Code
use strict; 
use warnings;
use 5.010;

my $total = 0x01 + 0x03;
say $total;

--output:--
4





In addition:

1) Search google for 'code tags'. Read the first 10 results and come back and post what you have learned.

2) Start all your programs with the following lines:


Code
use strict; 
use warnings;
use 5.010; #if using perl 5.10+


3) Declare all variables with my(), e.g.:


Code
my $num; 
my @numbers;
my($num1, $num2) = (3, 4);
my %phone_number_for;



4) Use the 3-arg form of open(), and use a variable instead of a bareword filehandle:


Code
open my $INFILE, '<', 'data.txt' 
or die "Couldn't open data.txt: $!";



(This post was edited by 7stud on Apr 12, 2010, 11:37 PM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives