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!
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!