HEX number conversion?

I am trying to convert a scalar variable into a HEX number without success. For example:
$num = 254.6; #Some number
$num = sprintf("%.0f",$num); #Round off the number
... # Now how do I get $num == "FF" ?

Thanks in advance - Eric
LVL 32
Erick37Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
$num=sprintf'%X',$num;
0
Erick37Author Commented:
Ok, well now I feel dumb... If you want the points, ozo, post it as a proposed answer.
0
Erick37Author Commented:
Another HEX question:
How do you test raw binary data read from a file without resorting to crytic characters as in the example below?  I would like my code more readable and not have to look up the character codes for the HEX numbers.

#Look for Graphic Control Extension 0x21
if ($temp =~ /^!/) {
    ...
    read(FILE, $temp, 1);  #See what kind of extension it is.
    if ($temp =~ /^ù/) {  #Graphic Control label 0xFE
        ...
    }
    ...
}
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

ozoCommented:
if( $temp =~ /^\x21/ ){
  ...
  if( substr($temp,0,1) eq "\xFE"  ){ #but isn't 'ù' eq "\xF9"  and 'þ' eq "\fe" ?
0
Erick37Author Commented:
Thanks again ozo... geez my books are bad!
And you are right 0xFE is 'þ', the error was in my comment, I was testing for 0xF9 (now you can see why I need to use the hex value directly!)

Cheers.
0
Erick37Author Commented:
OK - here's another one: (I'm tryin' hard to give away these points)
I plugged in the code you suggested:
$num=sprintf'%X',$num; #convert a number to hex
and it works fine except I need a byte returned, when $num is less than 16.5 I get "F" when I want "0F"

Other than testing for this condition in an 'if' block, can sprintf return formatted hex numbers of a specific length?
Suppose I want "000F" for example.
0
ozoCommented:
Even if your books are bad you should have
perldoc perlfunc
and
perldoc perlre
etc. installed with your perl

$num=sprintf'%04X',$num;
#another method:
$num = uc unpack'H2',pack'C',sprintf"%.f",$num;
$num = uc unpack'H4',pack'n',sprintf"%.f",$num;


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Erick37Author Commented:
Thanks ozo:
Both methods you provided work perfectly.

Now it's time to browse the perldocs I guess.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.