Displaying hex numbers as decimal?

F

faren451

Hi, I have been trying to figure out how to display hex numbers as
decimals after reading in the hex from a file. Reading several posts
online, it seems like the easy way is to use printf. However, printf
does not seem to like me using a variable to make the hex number. For
example, my input files only have a 2-digit hex number, so I need to
add 0x in front to make printf work (as I found by experimenting with
the command line), but this does not work well with variables--
everything is converted to 0's. My script is below:

print "Enter filename to parse: ";
chomp($filename = <>);
$FilePath = "$filename";
sysopen(HANDLE, $FilePath, O_RDONLY) or die "Cannot open file";
@events = <HANDLE>;
close(HANDLE);
$Outfile = "out_$filename";
sysopen(OHANDLE, $Outfile, O_RDWR|O_CREAT, 0755) or die "Cannot create
file";
foreach $events (@events)
{
$fieldnum = 0;
while($events =~ /(\s.*?\s)/g)
{
$fieldnum++;

# date/time/week
if($fieldnum == 2)
{
printf OHANDLE "$1,";
}

# hex data fields
if($fieldnum > 6)
{
$tmp = $1;
$tmp =~ s/^\s+//;
$tmp =~ s/\s+$//;
$hexval = "0x$tmp";
printf OHANDLE "$hexval=>%d,", $hexval;
printf "$hexval=>%d,", $hexval;
}
}
close(OHANDLE);


Sample data fields in the file are:


EB 70 07 8A 51 C5 98 1B 01 00 00 00 00 00 00 02 00 00
00 00 00 00 00 00 00 01 01 00 01 00 00 00 00 00 00
00 02 00 00 00 01 00 01 01 00 01 02 00 00 00 00 00
01 00 01 00 00 93 10 60 00



I appreciate any help! I am a completely Perl newbie, so I have no
idea why this will not work. The built-in hex() function also does
not work, I suspect for the same reason?

Thanks!
 
M

Mirco Wahab

Hi, I have been trying to figure out how to display hex numbers as
decimals after reading in the hex from a file. Reading several posts
online, it seems like the easy way is to use printf. However, printf
does not seem to like me using a variable to make the hex number. For
example, my input files only have a 2-digit hex number, so I need to
add 0x in front to make printf work (as I found by experimenting with
the command line), but this does not work well with variables--
everything is converted to 0's. My script is below:
...
I appreciate any help! I am a completely Perl newbie, so I have no
idea why this will not work. The built-in hex() function also does
not work, I suspect for the same reason?

You have to differentiate between 'numeric literals', like 0xabc
or 123456 or 0333 and the actual perl values, which are of type
"signed integer" (IV) or "unsigned integer" (UV).

If you write them out, you choose a "representation", which might
be "binary" (%b) or "decimal" (%d) or hex (%h). The Perl function
"hex" does convert *strings* containing a hex sequence
(eg. perl -e ' print hex "10" ' ==> prints 16)

If you have hex-strings in your file, then you just
put them in the hex function and you'll get "normal
perl values" out, which might be written in any
representation later on.
Example:
==>
...
print "Enter filename to parse: ";
chomp(my $filename = <>);
my $FilePath = $filename;

open my $fh, '<', $FilePath or die "$FilePath - $!";
my @events = <$fh>;
close $fh;

my $Outfile = "out_$filename";
open $fh, '>', $Outfile or die "$Outfile - $!";

for my $events (@events) {
my $fieldnum = 0;
while( $events =~ /(\S+)/g ) {
# date/time/week
printf $fh "$1," if ++$fieldnum == 2;
# hex data fields
if($fieldnum > 6) {
my $hexval = hex $1;
printf $fh "%02X=>%d,", $hexval, $hexval;
printf "%02X=>%d,", $hexval, $hexval
}
}
}
close $fh;
...
<==

Regards

Mirco
 

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