Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Converting Files from Binary to ASII

  • 06-10-2005 2:20pm
    #1
    Registered Users, Registered Users 2 Posts: 250 ✭✭


    Hi All,

    Can anyone help me with converting a binary file to a readable ASCII file...

    Firstly, is there some unix command that will do it? Or is there some free download that does it?

    Or if not, can this be done in Perl? I've downloaded a perl module that will convert binary text to ascii, but the problem is reading the binary file.
    When I read it, it reads the funny symbols rather than the actual 1's and 0's. Is there some way to read the 1's and 0's rather than the symbols.

    Thanks for any help.


Comments

  • Registered Users, Registered Users 2 Posts: 1,481 ✭✭✭satchmo


    All files are binary - ASCII is just a way of encoding text by assigning byte values (ASCII uses 7 bits, decimal 0 - 127) to a different character. Some of these characters are printable (the alphabet, punctuation, etc), some not (carraige returns, backspace, etc). See Wikipedia for a more detailed explanation.

    Those funny symbols are bytes being interpreted as ASCII values (if you opened it in a text editor like notepad), when those bytes don't actually represent ASCII values at all. What's the file you're trying to read? It's probably not a text file (are you trying to look at an .exe file or similar?), or it might be encoded in a different scheme like Unicode - generally less common than ASCII, but still widespread.


  • Registered Users, Registered Users 2 Posts: 250 ✭✭giveth


    Thanks Stachmo, that makes sense.

    The file is the output of a FORTRAN program that was written years ago by someone else. There's another program that reads the output file and displays the data graphically.

    We want to use the output file for other purposes now, so we need to be able to read the output file.

    The output should just be a list of values I think.

    I've just tried an online conversion to try to convert the text from unicode to ascii, but that doesnt work.

    Is there any other encoding that it could be?

    thanks


  • Registered Users, Registered Users 2 Posts: 6,762 ✭✭✭WizZard


    Do you have the source of the Fortran program that takes the output and converts it to a graphical representatoin? You should be able to figure out the encoding/decoding from that.


  • Registered Users, Registered Users 2 Posts: 1,481 ✭✭✭satchmo


    If it's a data file output from some program, then it probably uses its own format. ASCII/Unicode and the like is only useful if you're dealing with text.. if the file just represents numerical data, such as in your case, then converting the numbers to text and back again at the other end wouldn't make sense - the numbers would just be stored directly in their binary form.

    Is there any documentation with the program? If so, the data file format should be in there somewhere. Otherwise looking at the source of the graphical part that decodes the file would be your best bet, like WizZard said.

    Or if you've no source at all, get a hex editor to view the data file and see if you can match any of the file's contents to the values being displayed in the graphical part. If possible, change the values to all zeros or something distinctive and get the program to write the values back to the file - you could then see how it changes and might be able to figure out the structure.


  • Registered Users, Registered Users 2 Posts: 21,264 ✭✭✭✭Hobbes


    Start with creating 1 value you know the output for and then check the file for it.

    Then do 2 inputs then 4, 8, etc. Is there a pattern in size? It might be that it has a header beforehand and the output might be straight binary numbers. In which case you have to determine what format those numbers are stored in.

    Another trick it to check the first couple of bytes to see if they correspond to an existing magic number (basically a binary header that determines the file format).


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 6,571 ✭✭✭daymobrew


    A few years I found a CGI that browses a Unix file system.
    I modified it to display binary files (i.e. fail the perl -T test) in hex and ASCII format.

    Look at the attached perl script and search for CHARS_PER_ROW.
    I used unpack to convert the binary data to pairs of hex chars.
    For printing I substitute non-printable chars in the binary data with dots.

    Or you could use 'od' (octal dump) utility. For example, here are the first 20 chars of a GIF file:
    $ od -N 20 -c notext.gif
    0000000   G   I   F   8   9   a 372  \0   P  \0 367  \0  \0   f  \0  \0
    0000020   i 005 002   m
    0000024
    


  • Registered Users, Registered Users 2 Posts: 250 ✭✭giveth


    Thanks guys. I'll try this stuff out later today or tomorrow...


  • Registered Users, Registered Users 2 Posts: 21,264 ✭✭✭✭Hobbes


    If you want a good HEX editor I recommend Hexplorer

    http://www.softpedia.com/get/Programming/File-Editors/Hexplorer.shtml


Advertisement