Connecting Tech Pros Worldwide Forums | Help | Site Map

Binary file handling

Member
 
Join Date: Jun 2007
Posts: 35
#1: Oct 17 '07
hello everybody,
i need some help in binary file handling in perl. can anybody give me some information about binary file. actully i am reading data from some text file and extracting some usefull information from there and want store in my own binary file with .vbf extension ( not like .dat file.) i am putting code here for reading text file and extract some information from there but how can i store that information in my own binary file.


Expand|Select|Wrap|Line Numbers
  1. use warnings;
  2. use strict;
  3. {
  4.  
  5. my @file_contents;
  6. open (INPUT, "sample.s37");
  7. {     
  8.  @file_contents = <INPUT>;
  9.  
  10. }
  11. close(INPUT);
  12. my $i;
  13. for($i=0;$i<@file_contents-1;$i++)
  14.   {
  15.  my $Type = substr ($file_contents[$i], 0,-41);
  16.  my $Count = substr ($file_contents[$i], 2,-38);
  17.  my $Address = substr ($file_contents[$i], 4,-35);
  18.  my $Data = substr ($file_contents[$i], 8,-3);
  19.  my $CSum = substr ($file_contents[$i], 40);
  20.  printf("\nType:$Type\nCount:$Count\nAddress:$Address\ndata  :$Data\nCheckSum:$CSum");
  21.  }
  22. }
  23.  
  24.  
i want to store that information in this format:
<2 byte type><4 byte count><4 byte address><1 to 64 byte data><2 byte checksum>

can anybody help me in this code. or give me some usefull link to solve this kind of problem.and how can i write rule for this kind of parsing.

Thanks & Regard
Lokiiiiiiii

KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#2: Oct 17 '07

re: Binary file handling


If your code works as you want it, all you have to do is open a file for writing and write the output to a file instead of printf.

open(IN, ">", 'yourfile44');
print IN "your data\n";
close IN;
numberwhun's Avatar
Site Moderator
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,572
#3: Oct 17 '07

re: Binary file handling


Quote:

Originally Posted by lokeshrajoria

hello everybody,
i need some help in binary file handling in perl. can anybody give me some information about binary file. actully i am reading data from some text file and extracting some usefull information from there and want store in my own binary file with .vbf extension ( not like .dat file.) i am putting code here for reading text file and extract some information from there but how can i store that information in my own binary file.


Expand|Select|Wrap|Line Numbers
  1. use warnings;
  2. use strict;
  3. {
  4.  
  5. my @file_contents;
  6. open (INPUT, "sample.s37");
  7. {     
  8.  @file_contents = <INPUT>;
  9.  
  10. }
  11. close(INPUT);
  12. my $i;
  13. for($i=0;$i<@file_contents-1;$i++)
  14.   {
  15.  my $Type = substr ($file_contents[$i], 0,-41);
  16.  my $Count = substr ($file_contents[$i], 2,-38);
  17.  my $Address = substr ($file_contents[$i], 4,-35);
  18.  my $Data = substr ($file_contents[$i], 8,-3);
  19.  my $CSum = substr ($file_contents[$i], 40);
  20.  printf("\nType:$Type\nCount:$Count\nAddress:$Address\ndata  :$Data\nCheckSum:$CSum");
  21.  }
  22. }
  23.  
  24.  
i want to store that information in this format:
<2 byte type><4 byte count><4 byte address><1 to 64 byte data><2 byte checksum>

can anybody help me in this code. or give me some usefull link to solve this kind of problem.and how can i write rule for this kind of parsing.

Thanks & Regard
Lokiiiiiiii

I noticed this morning that you have posted the same message (this one) twice to this forum. Please do not post the same message twice. I have taken the liberty of deleting the other thread in lieu of this one that has an answer from one of our Experts.

Regards,

Jeff
Member
 
Join Date: Jun 2007
Posts: 35
#4: Oct 18 '07

re: Binary file handling


Quote:

Originally Posted by numberwhun

I noticed this morning that you have posted the same message (this one) twice to this forum. Please do not post the same message twice. I have taken the liberty of deleting the other thread in lieu of this one that has an answer from one of our Experts.

Regards,

Jeff

hello Jeff,
sorry! actully i didn't get any respose of that posting. so i posted twice. but i'll remember now.

Thanks & Regards
Lokiii
Member
 
Join Date: Jun 2007
Posts: 35
#5: Oct 18 '07

re: Binary file handling


Quote:

Originally Posted by KevinADC

If your code works as you want it, all you have to do is open a file for writing and write the output to a file instead of printf.

open(IN, ">", 'yourfile44');
print IN "your data\n";
close IN;

Hello KevinADC,
yeah my code is working properlly but that file not coming in binary. because this is readable by notepad also. but when we read some other binary file that is not readable by notepad. why is it so.
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#6: Oct 18 '07

re: Binary file handling


Quote:

Originally Posted by lokeshrajoria

Hello KevinADC,
yeah my code is working properlly but that file not coming in binary. because this is readable by notepad also. but when we read some other binary file that is not readable by notepad. why is it so.

I'm not sure what you are wanting to do. Whats in the input file?
Member
 
Join Date: Jun 2007
Posts: 35
#7: Oct 19 '07

re: Binary file handling


Quote:

Originally Posted by KevinADC

I'm not sure what you are wanting to do. Whats in the input file?

Hello KevinADC,
my input file contain this, inforamtion mention below:
S00F000053424C5F352E30352E3000008A
S315FFFFB800541A63FF090063E7050063EF0100400772
S315FFFFB81025B640070CB6400704B60162406790B6EF
S315FFFFB820400720B7400791B6406700B6805EFFFF2F
S315FFFFB830605F88B6205E90F5CB0F0000204691F53E
S315FFFFB840883F01008066FFFF2036A861483F000062

when i am opening output file in notepad information display same.
S00F000053424C5F352E30352E3000008A
S315FFFFB800541A63FF090063E7050063EF0100400772
S315FFFFB81025B640070CB6400704B60162406790B6EF
S315FFFFB820400720B7400791B6406700B6805EFFFF2F
S315FFFFB830605F88B6205E90F5CB0F0000204691F53E
S315FFFFB840883F01008066FFFF2036A861483F000062

but if i create binary file in other language same input file then i am opening that output file in notepad so information is coming like this:
Tc c c @%@ @b@g@ @@g ^`_ ^ F? f 6aH? `7`g? V.7 N Fd@O( ?``} >`?|< ?*`}  Xc c 8 ga
@ "!?ָ`} # # H Xc c g\bb- ^,W NL:V KW G @ 6 ?>F IG @? "i' -1B J`} 7a``7# # H Xc c  a
c
mean to say this is not readable by notepad...why is it so....

should it possible in perl also ...

Thanks & Regards
Lokiiii
numberwhun's Avatar
Site Moderator
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,572
#8: Oct 19 '07

re: Binary file handling


Quote:

Originally Posted by lokeshrajoria

Hello KevinADC,
my input file contain this, inforamtion mention below:
S00F000053424C5F352E30352E3000008A
S315FFFFB800541A63FF090063E7050063EF0100400772
S315FFFFB81025B640070CB6400704B60162406790B6EF
S315FFFFB820400720B7400791B6406700B6805EFFFF2F
S315FFFFB830605F88B6205E90F5CB0F0000204691F53E
S315FFFFB840883F01008066FFFF2036A861483F000062

when i am opening output file in notepad information display same.
S00F000053424C5F352E30352E3000008A
S315FFFFB800541A63FF090063E7050063EF0100400772
S315FFFFB81025B640070CB6400704B60162406790B6EF
S315FFFFB820400720B7400791B6406700B6805EFFFF2F
S315FFFFB830605F88B6205E90F5CB0F0000204691F53E
S315FFFFB840883F01008066FFFF2036A861483F000062

but if i create binary file in other language same input file then i am opening that output file in notepad so information is coming like this:
Tc c c @%@ @b@g@ @@g ^`_ ^ F? f 6aH? `7`g? V.7 N Fd@O( ?``} >`?|< ?*`}  Xc c 8 ga
@ "!?ָ`} # # H Xc c g\bb- ^,W NL:V KW G @ 6 ?>F IG @? "i' -1B J`} 7a``7# # H Xc c  a
c
mean to say this is not readable by notepad...why is it so....

should it possible in perl also ...

Thanks & Regards
Lokiiii

I have no idea why that is happening, especially since we do not know what the other program is doing when writing to the file.

Regards,

Jeff
Member
 
Join Date: Jun 2007
Posts: 35
#9: Oct 19 '07

re: Binary file handling


Quote:

Originally Posted by numberwhun

I have no idea why that is happening, especially since we do not know what the other program is doing when writing to the file.

Regards,

Jeff

hello jeff,
i think they are using encoding technique. can we encode that code :
S00F000053424C5F352E30352E3000008A
S315FFFFB800541A63FF090063E7050063EF0100400772
S315FFFFB81025B640070CB6400704B60162406790B6EF
S315FFFFB820400720B7400791B6406700B6805EFFFF2F

to

Tc c c @%@ @b@g@ @@g ^`_ ^ F? f 6aH? `7`g? V.7 N Fd@O( ?``} >`?|< ?*`}  Xc c

please give me some suggestion:

Regards
Lokiiiiii
numberwhun's Avatar
Site Moderator
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,572
#10: Oct 19 '07

re: Binary file handling


Quote:

Originally Posted by lokeshrajoria

hello jeff,
i think they are using encoding technique. can we encode that code :
S00F000053424C5F352E30352E3000008A
S315FFFFB800541A63FF090063E7050063EF0100400772
S315FFFFB81025B640070CB6400704B60162406790B6EF
S315FFFFB820400720B7400791B6406700B6805EFFFF2F

to

Tc c c @%@ @b@g@ @@g ^`_ ^ F? f 6aH? `7`g? V.7 N Fd@O( ?``} >`?|< ?*`}  Xc c

please give me some suggestion:

Regards
Lokiiiiii

Again, I don't know what the other program was doing. Maybe you can examine it, find out, and then search CPAN for a module that does the same encoding.

Regards,

Jeff
Reply


Similar Perl bytes