perl8rookie
Programmer
I am using following code to read a value out of a binary file and convert it to hex, read another value from an excel file. The two values are different but when I compare these values in perl the result is a match which does not make sense.
I have attached a link to download the excel, binary and script files for you to try it out. What changes can I make in script to have the both values in the same format so that the comparison is correct?
I would really appreciate if someone can point out what I am doing wrong. The command to run the script is:
> perl new.pl new.bin
# Code:
#---------------------------------------------------------
use Win32::OLE qw(in with);
use Win32::OLE::Const 'Microsoft Excel';
$Win32::OLE::Warn = 3; # die on errors...
my $Excel = Win32::OLE->GetActiveObject('Excel.Application')
|| Win32::OLE->new('Excel.Application', 'Quit');
if(open(FILE, "<".$ARGV[0]))
{
# Converts bin file to hex string
binmode(FILE);
while(!eof(FILE))
{
$hex_String .= sprintf("\%02X", ord(getc(FILE)));
}
$hex_value = substr($hex_String,$index,4);
$hex_value =~ s/^0+//;
print "\nhex value from bin file: $hex_value";
close(FILE);
# Gets the value in (1,1) and saves it to excel_value
my $readBook = $Excel->Workbooks->Open("new.xls");
my $readSheet = $readBook->Worksheets(1);
$excel_value = $readSheet->Cells($readRow,1)->{'Value'};
print "\nA value read from excel file: $excel_value";
if ($hex_value == $excel_value)
{
print "\nComparison Failure, values match";
}
else
{
print "\nComparison Success, values don't match";
}
}
# All files are attached. Please help
I have attached a link to download the excel, binary and script files for you to try it out. What changes can I make in script to have the both values in the same format so that the comparison is correct?
I would really appreciate if someone can point out what I am doing wrong. The command to run the script is:
> perl new.pl new.bin
# Code:
#---------------------------------------------------------
use Win32::OLE qw(in with);
use Win32::OLE::Const 'Microsoft Excel';
$Win32::OLE::Warn = 3; # die on errors...
my $Excel = Win32::OLE->GetActiveObject('Excel.Application')
|| Win32::OLE->new('Excel.Application', 'Quit');
if(open(FILE, "<".$ARGV[0]))
{
# Converts bin file to hex string
binmode(FILE);
while(!eof(FILE))
{
$hex_String .= sprintf("\%02X", ord(getc(FILE)));
}
$hex_value = substr($hex_String,$index,4);
$hex_value =~ s/^0+//;
print "\nhex value from bin file: $hex_value";
close(FILE);
# Gets the value in (1,1) and saves it to excel_value
my $readBook = $Excel->Workbooks->Open("new.xls");
my $readSheet = $readBook->Worksheets(1);
$excel_value = $readSheet->Cells($readRow,1)->{'Value'};
print "\nA value read from excel file: $excel_value";
if ($hex_value == $excel_value)
{
print "\nComparison Failure, values match";
}
else
{
print "\nComparison Success, values don't match";
}
}
# All files are attached. Please help