Solved

HEX number conversion?

Posted on 1998-05-20
8
637 Views
Last Modified: 2011-10-03
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
0
Comment
Question by:Erick37
  • 5
  • 3
8 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1206277
$num=sprintf'%X',$num;
0
 
LVL 32

Author Comment

by:Erick37
ID: 1206278
Ok, well now I feel dumb... If you want the points, ozo, post it as a proposed answer.
0
 
LVL 32

Author Comment

by:Erick37
ID: 1206279
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
 
LVL 84

Expert Comment

by:ozo
ID: 1206280
if( $temp =~ /^\x21/ ){
  ...
  if( substr($temp,0,1) eq "\xFE"  ){ #but isn't 'ù' eq "\xF9"  and 'þ' eq "\fe" ?
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 32

Author Comment

by:Erick37
ID: 1206281
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
 
LVL 32

Author Comment

by:Erick37
ID: 1206282
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
 
LVL 84

Accepted Solution

by:
ozo earned 100 total points
ID: 1206283
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
 
LVL 32

Author Comment

by:Erick37
ID: 1206284
Thanks ozo:
Both methods you provided work perfectly.

Now it's time to browse the perldocs I guess.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now