Binary file handling

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lokeshrajoria
    New Member
    • Jun 2007
    • 35

    Binary file handling

    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.


    Code:
    use warnings;
    use strict;
    {
     
    my @file_contents;
    open (INPUT, "sample.s37");
    {     
     @file_contents = <INPUT>;
     
    }
    close(INPUT);
    my $i;
    for($i=0;$i<@file_contents-1;$i++)
      {
     my $Type = substr ($file_contents[$i], 0,-41);
     my $Count = substr ($file_contents[$i], 2,-38);
     my $Address = substr ($file_contents[$i], 4,-35);
     my $Data = substr ($file_contents[$i], 8,-3);
     my $CSum = substr ($file_contents[$i], 40);
     printf("\nType:$Type\nCount:$Count\nAddress:$Address\ndata  :$Data\nCheckSum:$CSum");
     }
    }
    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
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    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;

    Comment

    • numberwhun
      Recognized Expert Moderator Specialist
      • May 2007
      • 3467

      #3
      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.


      Code:
      use warnings;
      use strict;
      {
       
      my @file_contents;
      open (INPUT, "sample.s37");
      {     
       @file_contents = <INPUT>;
       
      }
      close(INPUT);
      my $i;
      for($i=0;$i<@file_contents-1;$i++)
        {
       my $Type = substr ($file_contents[$i], 0,-41);
       my $Count = substr ($file_contents[$i], 2,-38);
       my $Address = substr ($file_contents[$i], 4,-35);
       my $Data = substr ($file_contents[$i], 8,-3);
       my $CSum = substr ($file_contents[$i], 40);
       printf("\nType:$Type\nCount:$Count\nAddress:$Address\ndata  :$Data\nCheckSum:$CSum");
       }
      }
      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

      Comment

      • lokeshrajoria
        New Member
        • Jun 2007
        • 35

        #4
        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

        Comment

        • lokeshrajoria
          New Member
          • Jun 2007
          • 35

          #5
          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.

          Comment

          • KevinADC
            Recognized Expert Specialist
            • Jan 2007
            • 4092

            #6
            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?

            Comment

            • lokeshrajoria
              New Member
              • Jun 2007
              • 35

              #7
              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:
              S00F000053424C5 F352E30352E3000 008A
              S315FFFFB800541 A63FF090063E705 0063EF010040077 2
              S315FFFFB81025B 640070CB6400704 B60162406790B6E F
              S315FFFFB820400 720B7400791B640 6700B6805EFFFF2 F
              S315FFFFB830605 F88B6205E90F5CB 0F0000204691F53 E
              S315FFFFB840883 F01008066FFFF20 36A861483F00006 2

              when i am opening output file in notepad information display same.
              S00F000053424C5 F352E30352E3000 008A
              S315FFFFB800541 A63FF090063E705 0063EF010040077 2
              S315FFFFB81025B 640070CB6400704 B60162406790B6E F
              S315FFFFB820400 720B7400791B640 6700B6805EFFFF2 F
              S315FFFFB830605 F88B6205E90F5CB 0F0000204691F53 E
              S315FFFFB840883 F01008066FFFF20 36A861483F00006 2

              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ÿÿ 6¨aH? `7–õ`g˜õË? V.ñÊ7 N¬ €Fdá@OÑö( ?†¸ÿÿ€æ`á`¶ è} >è`?|¶< ?*¸ÿÿè`¶} €ÿ ÕõXcÿ cï €ÿ¦ €ÿ8 g¶àaÂ
              @ "!á?Ö¸ÿÿè` ¶} #ï #ÿ 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

              Comment

              • numberwhun
                Recognized Expert Moderator Specialist
                • May 2007
                • 3467

                #8
                Originally posted by lokeshrajoria
                Hello KevinADC,
                my input file contain this, inforamtion mention below:
                S00F000053424C5 F352E30352E3000 008A
                S315FFFFB800541 A63FF090063E705 0063EF010040077 2
                S315FFFFB81025B 640070CB6400704 B60162406790B6E F
                S315FFFFB820400 720B7400791B640 6700B6805EFFFF2 F
                S315FFFFB830605 F88B6205E90F5CB 0F0000204691F53 E
                S315FFFFB840883 F01008066FFFF20 36A861483F00006 2

                when i am opening output file in notepad information display same.
                S00F000053424C5 F352E30352E3000 008A
                S315FFFFB800541 A63FF090063E705 0063EF010040077 2
                S315FFFFB81025B 640070CB6400704 B60162406790B6E F
                S315FFFFB820400 720B7400791B640 6700B6805EFFFF2 F
                S315FFFFB830605 F88B6205E90F5CB 0F0000204691F53 E
                S315FFFFB840883 F01008066FFFF20 36A861483F00006 2

                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ÿÿ 6¨aH? `7–õ`g˜õË? V.ñÊ7 N¬ €Fdá@OÑö( ?†¸ÿÿ€æ`á`¶ è} >è`?|¶< ?*¸ÿÿè`¶} €ÿ ÕõXcÿ cï €ÿ¦ €ÿ8 g¶àaÂ
                @ "!á?Ö¸ÿÿè` ¶} #ï #ÿ 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

                Comment

                • lokeshrajoria
                  New Member
                  • Jun 2007
                  • 35

                  #9
                  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 :
                  S00F000053424C5 F352E30352E3000 008A
                  S315FFFFB800541 A63FF090063E705 0063EF010040077 2
                  S315FFFFB81025B 640070CB6400704 B60162406790B6E F
                  S315FFFFB820400 720B7400791B640 6700B6805EFFFF2 F

                  to

                  ÿÿ¸ „Tcÿ cç cï @%¶@ ¶@¶b@g¶@ ·@‘¶@g ¶€^ÿÿ`_ˆ¶ ^õË F‘õˆ? €fÿÿ 6¨aH? `7–õ`g˜õË? V.ñÊ7 N¬ €Fdá@OÑö( ?†¸ÿÿ€æ`á`¶ è} >è`?|¶< ?*¸ÿÿè`¶} €ÿ ÕõXcÿ cï

                  please give me some suggestion:

                  Regards
                  Lokiiiiii

                  Comment

                  • numberwhun
                    Recognized Expert Moderator Specialist
                    • May 2007
                    • 3467

                    #10
                    Originally posted by lokeshrajoria
                    hello jeff,
                    i think they are using encoding technique. can we encode that code :
                    S00F000053424C5 F352E30352E3000 008A
                    S315FFFFB800541 A63FF090063E705 0063EF010040077 2
                    S315FFFFB81025B 640070CB6400704 B60162406790B6E F
                    S315FFFFB820400 720B7400791B640 6700B6805EFFFF2 F

                    to

                    ÿÿ¸ „Tcÿ cç cï @%¶@ ¶@¶b@g¶@ ·@‘¶@g ¶€^ÿÿ`_ˆ¶ ^õË F‘õˆ? €fÿÿ 6¨aH? `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

                    Comment

                    Working...