swapy345 has asked for the wisdom of the Perl Monks concerning the following question:

I am perl developer. please let me know how to parse binary files. The binary file is created using c code. In unpack function which templae needs to use? Hi marshall, The data looks to me like, since binary not human readable.
  9@bB  8H‹1 fi FJ@*@ d@@ .raX&\hÈ2*]G@GR,yhS,hG >4P, +];~ @Ao@W KC@( @ $zs'}1S~ \"U* +1*+ v!‹cR&M``{J#i2]0  06 ,- %/n: +H'ϗX:6 B7eii&[.pzTM\%K@0 M6Z?5B0$$ I%8 +ڂw@%)~\Y ':^_*X+qqaXQ 4 ` 2\ @ޅbT*fY+qQ 3 h@63P$ BXB2hp ]e5g d + Wp :?>dgv'}.uܐ9x ,KV b@ `w}]x -@ bB E_ܸ1Vr& ԟ %",ј~x 'mi+% `BPyy zx 8xMkj\B-YU !,XA4 [ +D-e\-qaAa/ 22 ਖ ѐA{.\-0@ cGXͺU\*N]. AޔE;{e U j͡]B. =@F# 1`B6lmPx ĵEc1 ›AɀP+D% ֽz^.Y +z"^QwCh,q× *B ąAR+ Frfk5O_B/ +gLEѻv%2 `u_yĥH4lNR>O +h J:8@zx=Uk` AtXQ0 P`7< TrI +pԀ< c ҉ B\:qX 9q3`"AxdQa-8a8h1VČȆ8x2fqǰ# +EŽ\Y/d A lXÊwLAE 9$(2y ɝ'p`@ >`bEdb ƈzR]A y Dt (A " Abr(E3# "0YD"G|B]|I x'1/b=#‹)+J ̌>A-F@c#46.]$w S#X6@  _(bP879Q%ŧ6 D A@ |S9u=8 @ (#JRhi) ,H}f EPdFAħdB2X </Ì"k +8SE*`4טG, @t6rQ8aA~tUP PV E̔BJP IFGpT 7 /.N'.8f d @E$ޥ +%54 ^Ę+v"b  lwK/SJ(6`,h<;Zj#Dw3bH@Cr + # U 7'֦':zPϼ4)$E.A@wehuKS`Gq[E& ++4W|hf"? fL39nukr2Re`r}\{‹S2G<Oz@! +2!0nƮ/0B‹4 2 Y`i)iLRF:ӣҶ=HM)(v4 |x4Rq +O'"}]‹`"ȯ-̹/Ŝ a X[MS N +]9 )nN'@ N (sjlND]y*(; ٨#A(`d q6֑"ʴ‹ +h9("Fql vH fR *jP j1,aS +‹ @+HS›ј`{Fu hIhbOFawh%u}i;{: g +Ԍ i zx 9‹ 6`s# <`DNFts)*@ SnIO +nN!#zh7È 5#A9B :&AK@k$›! p@f +8Y (A1V!Tl)h PmD3gG%` /B9{% 3r@~o{ %p + @"G+#0'0%>H "21 ,XȐ ̀&b PX W f32 X~ + T% 6 ME;؃A8 iC"H8$5$ 6Z'TxV(z`kG| mXː +[Dp5* r~R 9@ s~_#]x05% h77ISL3  +uwSB~X SuJcA7  : y㇦( +{R % ǘ>S[Q>s~0K_x%  n{Hn8P'XP&19 %ur)04#s ԏא%0 AA}Wb)\R|P %O- \ e?,+ ~IxW Pp&(- +> I‹;$I%` X1‹D`Ȏ$i%p Y x];A;XD‹hg^H cY:3>S%X:H)-1% R qePXYcJ85P 9Ģ({z#9Y   `.?09!@ ># +]y˜;P' .>Qg炖% cHFxDh7+1% A34{M3G`jIYP7W x#2)% |BSBDzI +TK7%QISy5U(4 :)1;)S!pΘ|`d&J_X%0- / zIh989P䰉\7 C ~ .a @0%1|" G`J‹' +!AY e z ft-!tqQ kZ X*rJ"`shG!‹1@q:jJ[gs\) `:w%fJ + Z`q`%a:/ 4vt;e* 6 Z ǚʬ +K$X.Ŋ (* jpsxzY:@$b`}cj + "Э\btWJz ;u1‹ \z˕`  Z~K0a^ᬏ >#R e e`M  0 X X zs @۫ E{g|: 9U_1R|R9 cĢ-  .eT4np JKuRۤ- @ +'GSUa ˮi;  @ ϪK/ʺ\9+;/; ;% ?8$ +@ noJ,@O +@%z?Ň%P1‹ &s›2{`#sؾ%`%әƂy~› +;%p[XDnɕ›_%u@W~‹ ~\逇0  +!~-\ꐀ\QʞW0w0 %v|P/D!K%hkw +J! 8_\H Jl(PAs%u ģ='$H% ctp +WNJb|' PP Byte Field Code 1 - 11 Record header 12 Type of activation (1) BIN 13 Type of message (2) BIN 14 - 19 Name of the process EBCDIC 20 - 25 Name of the command EBCDIC 26 Type of terminal (3) BIN 27 Address of terminal BIN 28 - n Complementary message EBCDIC

Replies are listed 'Best First'.
Re: how to extract original string from binary files?
by haukex (Archbishop) on May 18, 2016 at 18:57 UTC

    Hi swapy345,

    You would use the unpack templates which correspond to the data types contained within the binary file, these are documented in pack. Also, I'd recommend to open the file with the "raw" layer to make sure no translations happen, e.g. open my $fh, '<:raw', 'filename' or die $!; (see also binmode).

    Hope this helps,
    -- Hauke D

Re: how to extract original string from binary files?
by AnomalousMonk (Archbishop) on May 18, 2016 at 20:49 UTC

    Please see also perlpacktut.


    Give a man a fish:  <%-{-{-{-<

Re: how to extract original string from binary files?
by Marshall (Canon) on May 18, 2016 at 19:57 UTC
    It appears that you aware of the unpack templates, but are having trouble using them. You cannot post a binary file here, but you can post an ASCII hex string just like source code, within code blocks, say the first 60-100 bytes of your binary file? Then explain what this byte stream means to you. Folks here would understand a C struct. Show your code so far if you can.
      Hi Marshall, I got the solution. Thanks for help! Swapnil

        As you mention EBCDIC, are you aware that Perl can decode EBCDIC data very well?

        Usually decoding the conent of (PIC X) fields using

        my $printable = decode('cp37', $input);
        or
        my $printable = decode('cp1047', $input);
Re: how to extract original string from binary files?
by haukex (Archbishop) on May 19, 2016 at 08:48 UTC

    Hi swapy345,

    The data looks to me like, since binary not human readable. ...

    We're humans too, and we can't read it either ;-)

    Please use a tool like hexdump or Data::Dumper (with $Data::Dumper::Useqq=1;) if you want to show a representative sample of your data.

    Regards,
    -- Hauke D

      Any feedback on http://search.cpan.org/~mhx/Convert-Binary-C-0.76/lib/ +Convert/Binary/C.pm ? Regards, Swapnil

        Convert::Binary::C seems like an ill fit for your situation. It is most usable when you want to pack (or unpack) data according a given C structure and you want to match the alignment that the C compiler has applied to the structure.

        So far, you have not shown us that you already have the existing C structure. Because of that, I can only advise against using this module. Maybe you want to look at pack and perlpacktut instead.

      Hi Haukex,

      Could I get email id? So that I can email the binary file. It is urgent on my side to solve the issue.I thought my solution would work but it not.

      Regards,

      Swapnil

        Hi Swapnil,

        For various reasons I think it's best to just continue working via this site, that way everyone can learn from this thread. I already mentioned two ways you can convert your binary data to a format you can post here, to add more examples to that list there's A simple hex dump in pure Perl and various Windows tools available.

        However, posting your binary data here won't help much if we don't know what format the file is in - the file format is actually much more important than the binary data. I see you've tried to describe it in the root node, but it's difficult to read, try using <code> tags.

        Regards,
        -- Hauke D

Re: how to extract original string from binary files?
by hippo (Archbishop) on May 19, 2016 at 08:57 UTC
    The data looks to me like, since binary not human readable.   9@bB  8H ...

    Welcome to the Monastery. In order to make your stay here more productive for you and less troublesome for everyone else, please do take a moment to read some Writeup Formatting Tips.