Displaying hex numbers as decimal?
am 10.10.2007 22:01:27 von 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 = ;
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!
Re: Displaying hex numbers as decimal?
am 10.10.2007 22:26:01 von Mirco Wahab
faren451@gmail.com wrote:
> 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
Re: Displaying hex numbers as decimal?
am 14.10.2007 13:31:18 von Joe Smith
faren451@gmail.com wrote:
> 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?
The built-in hex() function works just fine.
#!/usr/bin/perl
use strict; use warnings;
while () {
print " $_"; # Leading blank to make columns line up
foreach my $hnum (/[A-Fa-f0-9]+/g) {
printf "%3d ",hex($hnum);
}
print "\n\n";
}
__DATA__
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
The output is:
EB 70 07 8A 51 C5 98 1B 01 00 00 00 00 00 00 02 00 00
235 112 7 138 81 197 152 27 1 0 0 0 0 0 0 2 0 0
00 00 00 00 00 00 00 01 01 00 01 00 00 00 00 00 00
0 0 0 0 0 0 0 1 1 0 1 0 0 0 0 0 0
00 02 00 00 00 01 00 01 01 00 01 02 00 00 00 00 00
0 2 0 0 0 1 0 1 1 0 1 2 0 0 0 0 0
01 00 01 00 00 93 10 60 00
1 0 1 0 0 147 16 96 0
-Joe