F
faren451
Hi, ok, trying this again, since my first post seems to be non-
existent...
I am reading in 2-digit hex values from a file and would like to
output them as decimals. Through online surfing, it seems like the
easiest way to do this is to use printf. However, when I played
around with printf, it seems like all hex values need to have 0x in
front to make it work. So I tried using variables to essentially
concatenate 0x to my 2-digit hex values--but they all print as 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)
{
while($events =~ /(\s.*?\s)/g)
{
# hex data fields
$tmp = $1;
$tmp =~ s/^\s+//;
$tmp =~ s/\s+$//;
$hexval = "0x$tmp";
printf OHANDLE "$hexval=>%d,", $hexval;
printf "$hexval=>%d,", $hexval;
}
close(OHANDLE);
Sample input file is:
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 and all help! I am a complete Perl newbie and have
no idea why this does not work as advertised. I also tried the built-
in hex() function, but it has the same problem.
Thanks!
existent...
I am reading in 2-digit hex values from a file and would like to
output them as decimals. Through online surfing, it seems like the
easiest way to do this is to use printf. However, when I played
around with printf, it seems like all hex values need to have 0x in
front to make it work. So I tried using variables to essentially
concatenate 0x to my 2-digit hex values--but they all print as 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)
{
while($events =~ /(\s.*?\s)/g)
{
# hex data fields
$tmp = $1;
$tmp =~ s/^\s+//;
$tmp =~ s/\s+$//;
$hexval = "0x$tmp";
printf OHANDLE "$hexval=>%d,", $hexval;
printf "$hexval=>%d,", $hexval;
}
close(OHANDLE);
Sample input file is:
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 and all help! I am a complete Perl newbie and have
no idea why this does not work as advertised. I also tried the built-
in hex() function, but it has the same problem.
Thanks!