syntax question

R

Rosco

What is wrong with the syntax in the following bit of code:

sub convert($dec){
$hex = $dec;
if($dec==10){$hex = "A"}
else{if($dec==11){$hex = "B"}}
else{if($dec==12){$hex = "C"}}
else{if($dec==13){$hex = "D"}}
else{if($dec==14){$hex = "E"}}
else{if($dec==15){$hex = "F"}}
return $hex
};
 
G

GoMonitor/LanOnLine Incorporation

Why don't you do this:

sub convert($dec){
@hex = ('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
return @Alpha[$dec];

and save some headache
Regards

else{if($dec==11){$hex = "B"}}
else{if($dec==12){$hex = "C"}}
else{if($dec==13){$hex = "D"}}
else{if($dec==14){$hex = "E"}}
else{if($dec==15){$hex = "F"}}
return $hex
};
 
P

PapaBear

And a recode of your snippet would be:

<CODE>
sub convert {
$dec = $_;
$hex = $dec;
if($dec==10){$hex = "A"}
elsif($dec==11){$hex = "B"}
elsif($dec==12){$hex = "C"}
elsif($dec==13){$hex = "D"}
elsif($dec==14){$hex = "E"}
elsif($dec==15){$hex = "F"}
return $hex;
}
</CODE>
 
P

PapaBear

Or, why not do it the whole number in one time, instead of digit by digit?
(works for digits as well, of course)

sub d2h {
return sprintf "%lx", $_;
}
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top