LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-07-2012, 01:44 AM   #1
Perseus
Member
 
Registered: Oct 2011
Posts: 179

Rep: Reputation: Disabled
Shell script, Perl script, command or utility to convert Binary to text


Hello to all in the forum,

I'm trying to looking for a way to convert a binary file to a readable text.

I'm not sure about the structure of the binary file, only know that the data representation is in binary, binary Coded Decimal (BCD) in ISO.

Somebody knows how can I convert this file into readable text format?

Thanks in advance for any help
 
Old 07-07-2012, 01:58 AM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
You could use "od -tu1" to print out byte sized unsigned integers or "od -tx1" for byte sized hex which in your case prints out 0-9 as A-F isn't used.
 
Old 07-07-2012, 02:06 AM   #3
Perseus
Member
 
Registered: Oct 2011
Posts: 179

Original Poster
Rep: Reputation: Disabled
Hi jschiwal, thanks for answer!!

I'm not to confident with this matter.

with od -tu1 I get this kind of output:
Code:
0057200   0   1  96   0   1  54   0   1  55   0   1 126   0   1 105   0
0057220   0 106   0   0 121   0   0 148   0   1 147   0   1  34   0   0
0057240  33   0   0   9   0   1  10   0   1  16   0   1   8   0   1  43
0057260   0   0  44   0   0  45   0   0  46   0   0  85   0   0  86   0
0057300   7  42   0   0  47   0   0  48   0   0  49   0   0 255  52   0
0057320 128   9  53 201   6   0   0 160   0   0   0 128   9  60 201   6
0057340   0   0 128   0 160   0 128   9  67 201   6   0   0 128   0 128
0057360   0   5 130   0  64   7   1   0   0   1   0 107   0   0   0   2
0057400   0   0   2   0  24   0   0   0   3   0   0   3   0  23   0   0
0057420   0   4   0   0   4   0   1   0   0   0   5   0   0  10   0   1
with od -tx1 I get this kind of output:
Code:
0057240 21 00 00 09 00 01 0a 00 01 10 00 01 08 00 01 2b
0057260 00 00 2c 00 00 2d 00 00 2e 00 00 55 00 00 56 00
0057300 07 2a 00 00 2f 00 00 30 00 00 31 00 00 ff 34 00
0057320 80 09 35 c9 06 00 00 a0 00 00 00 80 09 3c c9 06
0057340 00 00 80 00 a0 00 80 09 43 c9 06 00 00 80 00 80
0057360 00 05 82 00 40 07 01 00 00 01 00 6b 00 00 00 02
0057400 00 00 02 00 18 00 00 00 03 00 00 03 00 17 00 00
0057420 00 04 00 00 04 00 01 00 00 00 05 00 00 0a 00 01
From that output how can I get the readable text output?

Thanks for help so far
 
Old 07-07-2012, 02:48 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
I'm confused by this---all files are binary. When you READ a file, you specify (either explicitly or implicitly) how the binary data should be interpreted-----e.g. it might be read as executable code, ascii text, image data, etc.

try "hexdump -C" to see the printable characters in the file.
 
Old 07-07-2012, 02:56 AM   #5
Perseus
Member
 
Registered: Oct 2011
Posts: 179

Original Poster
Rep: Reputation: Disabled
Hello pixellany,

Thanks for your help. I would like as ascii text, readable text letters and numbers.

With hexdump -C a sample looks like this:

Code:
000000b0  c9 06 88 88 a0 00 a0 e0  ca 0e 54 00 91 06 50 41  |..........T...PA|
000000c0  13 ff ff ff ff ff 00 19  80 09 35 c9 06 00 00 a0  |..........5.....|
000000d0  00 00 00 80 19 3c c9 06  88 88 a0 00 a0 e0 ca 0e  |.....<..........|
000000e0  54 00 91 06 50 41 13 ff  ff ff ff ff 00 19 80 09  |T...PA..........|
000000f0  43 c9 06 88 88 a0 00 a0  80 05 82 00 37 06 01 00  |C...........7...|
00000100  00 01 00 0a 01 00 00 02  00 00 03 00 65 00 00 00  |............e...|
00000110  03 00 00 02 00 14 01 00  00 04 00 00 04 00 01 01  |................|
00000120  00 00 05 00 00 05 00 65  00 00 00 0a 00 ff ff 00  |.......e........|
00000130  6b 00 00 00 07 80 2e c9  18 00 91 50 49 52 69 55  |k..........PIRiU|
00000140  3f ff ff ff 00 91 50 49  52 69 55 3f ff ff 00 01  |?.....PIRiU?....|
00000150  03 ca 03 08 08 fe cb 0a  00 00 00 00 00 00 00 00  |................|
00000160  00 00 cc 01 01 81 1b c9  0b 00 91 50 49 52 69 56  |...........PIRiV|
00000170  4f ff ff ff ca 06 00 00  00 00 00 00 cb 01 03 cc  |O...............|
00000180  01 01 ff 32 00 00 02 70  80 20 01 75 70 69 8f 50  |...2...p. .upi.P|
00000190  49 47 48 00 1f ff ff 00  15 00 0c 48 00 01 5a 00  |IGH........H..Z.|
000001a0  0a 17 00 01 18 00 01 42  00 01 60 00 01 36 00 01  |.......B..`..6..|
000001b0  37 00 01 7e 00 01 69 00  00 6a 00 00 79 00 00 94  |7..~..i..j..y...|
000001c0  00 01 93 00 01 22 00 00  21 00 00 09 00 01 0a 00  |....."..!.......|
000001d0  01 10 00 01 08 00 01 2b  00 00 2c 00 00 2d 00 00  |.......+..,..-..|
000001e0  2e 00 00 55 00 00 56 00  07 2a 00 00 2f 00 00 30  |...U..V..*../..0|
000001f0  00 00 31 00 00 ff 34 00  80 09 35 c9 06 00 00 a0  |..1...4...5.....|
00000200  00 00 00 80 09 3c c9 06  00 00 80 00 80 00 80 09  |.....<..........|
00000210  43 c9 06 00 00 80 00 80  00 05 82 00 49 08 01 00  |C...........I...|
 
Old 07-07-2012, 03:07 AM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Well---what you see is what you get!!----your file does not appear to have very much ascii text.

What is the file?----Did you expect that it would be readable text?
 
Old 07-07-2012, 03:16 AM   #7
Perseus
Member
 
Registered: Oct 2011
Posts: 179

Original Poster
Rep: Reputation: Disabled
The file is a dump of a equipment where is in binary, binary Coded Decimal (BCD) in ISO and I expect that contains words and numbers I mean readable text.

Iḿ not sure if it matters but the application that generates the binary dump is not unix system.

Last edited by Perseus; 07-07-2012 at 03:17 AM.
 
Old 07-07-2012, 03:36 AM   #8
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
binary, binary Coded Decimal (BCD) in ISO
I don't know what this means.

Take a look here:
http://en.wikipedia.org/wiki/Binary-coded_decimal

One thing I noticed was that BCD can be 1 byte per number---or one byte for TWO numbers:
Quote:
compressed (or packed) BCD typically carries two numerals in a single byte by taking advantage of the fact that four bits will represent the full numeral range.
Also, look at a Google search using--eg--"BCD to ascii linux"---for example, I found one article about using dd.

Can you attach a file with a small sample of the data?
 
Old 07-07-2012, 12:00 PM   #9
Perseus
Member
 
Registered: Oct 2011
Posts: 179

Original Poster
Rep: Reputation: Disabled
Hello Pixellany,

Binary, Binary BCD ISO is the only data configuration format I know.

I've attached a sample of 20 lines, I did awk 'NR<21' file to get a sample.
And in order to be able to upload the file I put the PDF extension to the file.

You can download it and remove the extension. I think the content will be intact.

Thanks in advance for your help
Attached Files
File Type: pdf Binfile.pdf (4.6 KB, 67 views)
 
Old 07-07-2012, 12:39 PM   #10
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
first, the hexdump output--I believe the 1st 2 lines are ascii test and then some filler (ff ff ff, etc)
The next 3 lines do NOT look like ascii text, so we'll try using other methods (the rest of the file segment looks pretty much like those 3 lines.

Code:
[mherring@hering-GTWay play]$ hexdump -C bin |more
00000000  31 48 4c 52 5f 54 47 55  5f 4c 43 30 36 30 39 30  |1HLR_TGU_LC06090|
00000010  37 32 36 30 30 33 31 07  ff ff ff ff ff ff ff ff  |7260031.........|
00000020  32 00 00 01 70 80 01 01  30 08 29 0f 50 42 05 00  |2...p...0.).PB..|
00000030  01 ff ff ff 00 15 00 0a  48 00 01 33 00 01 36 00  |........H..3..6.|
00000040  01 37 00 01 66 00 01 65  00 01 77 00 01 78 00 01  |.7..f..e..w..x..|
Lines 3-5 assuming 2-byte BCD**:
Code:
[mherring@hering-GTWay play]$ od --format=u2 bin|more
0000040    50   256 32880   257  2096  3881 16976     5
0000060 65281 65535  5376  2560    72 13057   256    54
0000100 14081   256   102 25857   256   119 30721   256
And assuming 1-byte BCD:
Code:
[mherring@hering-GTWay play]$ od --format=u1 bin|more
0000040  50   0   0   1 112 128   1   1  48   8  41  15  80  66   5   0
0000060   1 255 255 255   0  21   0  10  72   0   1  51   0   1  54   0
0000100   1  55   0   1 102   0   1 101   0   1 119   0   1 120   0   1
I could be wrong, but I don't see how readable text is ever going to come out of this---especially since you said it is BCD.

You can play some more with it using the various format options in "od".


**Note the order of bytes--in each 2-byte pair the second byte is the most significant digit.
eg
Hex 32 00 = decimal 50
Hex 05 00 = decimal 5
 
Old 07-08-2012, 08:35 AM   #11
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Perhaps it would help to read my blog entry, since most of the recent articles in this thread are about what is meant by 'binary', and so on. It may also be possible to use the strings tool, which is used to find sequences of ASCII text embedded within any specified 'binary' file or stream.

--- rod.
 
Old 07-08-2012, 10:56 AM   #12
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
If I am not mistaken, the only way to reveal ascii text in the file sample supplied would be to do some bit-shifting or decoding of some kind of encryption. Otherwise, hexdump -C pretty much tells the whole story.

If we can tolerate some semantics quibbling, I'll assert that ALL computer files are binary---ie they are sequences of 1s and 0s. That said, a prevailing definition of "binary" is something that will not decode cleanly to ascii.

Another thought: could the file be using unicode? How many bytes per character in unicode?
 
Old 07-08-2012, 12:07 PM   #13
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,784

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Quote:
Originally Posted by Perseus
The file is a dump of a equipment where is in binary, binary Coded Decimal (BCD) in ISO and I expect that contains words and numbers I mean readable text.
Doesn't the equipment come with a manual that explains the dump format?

Quote:
Originally Posted by theNbomr
Perhaps it would help to read my blog entry
Uh, you don't seem to have any blog entries. I'm getting "Invalid Blog specified" from that link.

Quote:
Originally Posted by pixellany
Another thought: could the file be using unicode? How many bytes per character in unicode?
Depends on the encoding.
 
Old 07-08-2012, 04:51 PM   #14
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Quote:
Originally Posted by ntubski View Post
Uh, you don't seem to have any blog entries. I'm getting "Invalid Blog specified" from that link.
Okay, I think I've got that sorted out now. First blog entry; I didn't know how to publish it properly.
--- rod.
 
Old 07-08-2012, 07:45 PM   #15
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
If you tell us the context (eg. source of data, full file name etc) then we MAY be be able to suggest how to go about getting the file contents in a readable form.

OK
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
convert shell script to a binary executable madhi Programming 13 07-24-2012 09:36 AM
[SOLVED] shell scripting vs. perl, etc. for simple text script Jonathanius Linux - Software 6 06-27-2012 11:26 PM
How Can I convert Hex to Binary in a perl script telecom_is_me Programming 18 10-11-2010 04:34 PM
Convert from shell script to binary? Anon123 Linux - General 4 06-26-2004 05:53 AM
how to convert a text file into shell script meng_en Linux - General 9 10-15-2002 10:46 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:26 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration