Convert binary to text Perl script

Hello everyone,

I have a binary file with a structure unknown. I have found 2 perl scripts that it seems to do the convertion but I get sintactic errors when I run them, may somebody test these 2 scripts please and see if really work?

One if from here
http://www.people.vcu.edu/~elhaij/IntroBioinf/Programs/context/binary6.html

and the other is this one:
unix commands & unix scripts @ coolcommands.com - the search engine for unix sysadmins :: Solaris, AIX, Linux, HP/UX, BSD, SCO

If you know a perl or shell script that do the job please share it :frowning:

Thanks in advance

@Ophiuchus
The second script looks like working fine! Can you tell us the errors that you get?

Hello PikK45,

Thanks for answer.

I'm not sure if some library is missing for me or if I'm running the script in wrong way.

I get this:

$ . conv.pl -t -f binaryfile
Order not found �use�, maybe wanted to say:
the order �nse� of packet �ns2� (universe)
the order �muse� of packet �muse� (universe)
the order  �uae� of packet �uae� (multiverse)
use: order not found
my: order not found
my: order not found
bash: conv.pl: line 7: sintax error near unexpected element `('
bash: conv.pl: line 7: `sub tobinary() {'

Thanks in advance for your help.

I hope you are executing the script in a wrong way.

try this

 ./conv.pl -t -f binaryfile 

This works on all unix based platforms :slight_smile:

Now I get access denied when I run it in that way :frowning: but if
I send it doing

$ perl conv.pl -t -f binaryfile

it gives me an output but with many wrong characters

l$ih A/
a_zDm$ih A/
a_z@u&M&4ZLB }B`"&L(ID:$ih A/
a_zDE$ihIDWW[L@IDKz l@I4%6&i0CEI4%$ihIDWW[ i-I4%$ih"    xW+
-$ih"    xW+
"m$iCE"    xW+W$ih"    xW+
"$ihIDWW$ih A/
a_zDu$ih A/
a_z@ML(IDz iQhI4%$ihI0^
�*zl4JIDzI$hIDz$ih"    xW+
$ih A/
aWzDM$ih"    xW+
l$ih A/
a_zDEl4JID:L(ID:$ihIDWWY$ih A/
a_zDe i,I4%$i    EI0^
�*zL(IDz$`@MIDz$ih

How could convert completely to text? :frowning:

first try to run the program to convert from text to binary and verify whether the program works the way that it is supposed to!!

If I convert some text file to binary with this script the output is 0s and 1s and If I convert the generated binary to text, the conversion is correct.

The issue is the file I want to convert, with this script doesn't show correctly converted to text.

Do you know another script or command to get this?

Thanks again

@Ophiuchus
This script is designed in a way that it converts all the characters to either binary or to text. By looking into the code, it uses functions such as

 pack & unpack 

You can look into PACK & UNPACK.

Thanks

I suspect you're confused about what those scripts do. What those scripts refer to as "binary" is actually an ascii text string of ones and zeroes. They're simply converting ascii text input into different ascii text output. Initially, each byte is an ascii character (perhaps a letter, a digit, some punctuation). The output converts each of those ascii bytes into 8 ascii bytes, where in the output the only possible value for a byte is 48 (ascii code in decimal for the digit 0) and 49 (ascii code in decimal value for the digit 1).

If you have a binary file (a file which may contain any byte value in any sequence it sees fit), and you don't know the format, what are you trying to achieve with a conversion? What is your goal? You can't expect some random set of bytes in a binary file to be convertible to meaningful text. Are you simply trying to extract embedded strings from an executable? If so, try the strings command.

Regards,
Alister

od is another good command for examining the contents of binary files. try -x for hex representation -c for character and -d for decimal.

$ od -x /usr/bin/ls | head
0000000 5a4d 0090 0003 0000 0004 0000 ffff 0000
0000020 00b8 0000 0000 0000 0040 0000 0000 0000
0000040 0000 0000 0000 0000 0000 0000 0000 0000
0000060 0000 0000 0000 0000 0000 0000 0080 0000
0000100 1f0e 0eba b400 cd09 b821 4c01 21cd 6854
0000120 7369 7020 6f72 7267 6d61 6320 6e61 6f6e
0000140 2074 6562 7220 6e75 6920 206e 4f44 2053
0000160 6f6d 6564 0d2e 0a0d 0024 0000 0000 0000
0000200 4550 0000 014c 0005 dcb8 4f2f 8e00 0001
0000220 0000 0000 00e0 032f 010b 1602 3000 0001
$ od -c /usr/bin/ls | head
0000000   M   Z 220  \0 003  \0  \0  \0 004  \0  \0  \0 377 377  \0  \0
0000020 270  \0  \0  \0  \0  \0  \0  \0   @  \0  \0  \0  \0  \0  \0  \0
0000040  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0
0000060  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0 200  \0  \0  \0
0000100 016 037 272 016  \0 264  \t 315   ! 270 001   L 315   !   T   h
0000120   i   s       p   r   o   g   r   a   m       c   a   n   n   o
0000140   t       b   e       r   u   n       i   n       D   O   S    
0000160   m   o   d   e   .  \r  \r  \n   $  \0  \0  \0  \0  \0  \0  \0
0000200   P   E  \0  \0   L 001 005  \0 270 334   /   O  \0 216 001  \0
0000220  \0  \0  \0  \0 340  \0   / 003  \v 001 002 026  \0   0 001  \0

Hello to all,

Thank you fo your comments I'll try to check my data with your suggestions and try to understand the decoded part.

Thanks for help so far really.