JiscMail Logo
Email discussion lists for the UK Education and Research communities

Help for COMP-FORTRAN-90 Archives


COMP-FORTRAN-90 Archives

COMP-FORTRAN-90 Archives


COMP-FORTRAN-90@JISCMAIL.AC.UK


View:

Message:

[

First

|

Previous

|

Next

|

Last

]

By Topic:

[

First

|

Previous

|

Next

|

Last

]

By Author:

[

First

|

Previous

|

Next

|

Last

]

Font:

Proportional Font

LISTSERV Archives

LISTSERV Archives

COMP-FORTRAN-90 Home

COMP-FORTRAN-90 Home

COMP-FORTRAN-90  2000

COMP-FORTRAN-90 2000

Options

Subscribe or Unsubscribe

Subscribe or Unsubscribe

Log In

Log In

Get Password

Get Password

Subject:

Re: binary to ASCII

From:

"Craig T. Dedo" <[log in to unmask]>

Reply-To:

Craig T. Dedo

Date:

Tue, 11 Apr 2000 10:19:43 -0500

Content-Type:

text/plain

Parts/Attachments:

Parts/Attachments

text/plain (116 lines)

Dear Peter and AGP:

Peter Shenkin wrote:

> Hi,
>
> First, Fortran does not have an unsigned type.  Thus, in general,
> you can't do this in Fortran, at least in any straightforward
> way.  The good news is that any unsigned that overflows an int
> will come out as a negative number when read into a signed int,
> so you can at least know whether you have this problem;  if you
> do, you can probably handle it with difficulty by understanding
> the bit pattern.
>
> Also, I suppose that your F90 compiler could have a signed int
> KIND large enough to hold the positive values of the unsigned
> ints that form the file.
>
> Second, the Fortran standard does not provide for reading a
> "raw" binary file, though most compilers provide extensions
> for this.  Fortran unformatted IO is usually expected to
> read/write some format peculiar to the compiler and/or the OS.
>
> Bottom line:  never say never, but here's a case where I think
> the right answer really is, "Do it in C."
>
>         -P.
>

    I disagree.  I believe that a programmer familiar with F90 and F95 can do
this fairly well in Fortran, although it will require a fair amount of hacking
and most likely the use of some non-standard extensions.  See discussion below.

>
> On Mon, 10 Apr 2000, AGP wrote:
>
> > I have been programming in F90 and F95 on a win98 machine
> > and I have been given the task of extracting data from
> > a binary file into ASCII. I consider myself a good programmer
> > but this is a bit above my head. The binary file is described
> > as "data as a binary integer array (PC byte-ordered) with
> > 10800 columns (e-w) by 6000 or 4800 rows (n-s)....the data
> > are also unsigned integers".
> > How would I use FORTRAN to extract the binary into an ASCII text file.
> > Any guidance is appreciated.
> > tia
> > AGP

    There is some important information missing.
    1.  What is the size of each binary datum?  Is it one byte?  2 bytes?  4
bytes?  Or, some weird size, e.g., 12 or 24 bits?
    2.  What, if anything, is the record structure?  Does each row have a
record delimiter?  If so, what is(are) the delimiter(s)?  If not, is it what is
commonly called a stream file, i.e., one large mass of binary data?
    3.  Are all records in the file the same length?  If so, what is the record
length?  If not, how are changes in the record length indicated within the
file?

    You will need to investigate several possibilities for reading the data.
Which one works best will depend upon the file properties and the capabilities
of your Fortran compiler.
    1.  Use direct access read.  This may work if all of the records are the
same length and the record structure is consistent with what your compiler
expects for a direct access file.  On Wintel systems, some Fortran compilers
will correctly do a direct access read of a file with no record boundaries.
You may need to compile your program with a special compiler option in order to
get this capability.
    2.  A sequential acces read may work if each record is delimited by a CR-LF
combination.
    3.  Use stream file read.  Right now, you will need to use a non-standard
extension, which varies in syntax from one compielr to another, in order to do
this.  Stream I/O will be in F2K, but so far, no vendor has implemented the ISO
standard version of this feature.

    You will need the answer to question 1 in order to know what to do to read
the data into variables and transform it to proper character representation.
Here are some thoughts.
    1.  You could read each row into a CHARACTER variable using the "A"
format.  This will work if your compiler's CHARACTER data type will accept any
bit pattern, the size of the binary datum is small enough (e.g., 1 or 2 bytes),
and the maximum length of your compiler's CHARACTER variables large enough.
(On Wintel systems, Compaq Visual Fortran has a maximum length for CHARACTER
variables of 2**31-1, which is certainly large enough.  Other Fortran compilers
may have much lower limits for the maximum length of a CHARACTER variable.)
Once you have the binary data in the CHARACTER variable, you can use an
internal READ statement or the TRANSFER function to move the bits into suitably
sized INTEGER variables or array elements.
    2.  You could read the data into a signed integer of the proper size and
then copy it into a signed integer of a size large enough to properly represent
the unsigned data by using the TRANSFER function, one of the bit intrinsic
functions, or the MVBITS subroutine.
    3.  Once you have the data properly represented in INTEGER variables or
array elements, you can write it out in character representation.

    If you have any other questions, please feel free to contact me.

--
----------
Sincerely,
Craig T. Dedo                                          Internet:
[log in to unmask]
Elmbrook Computer Services                 Voice Phone:  (262) 783-5869
17130 W. Burleigh Place                        Fax Phone:     (262) 783-5928
Brookfield, WI   53005-2759                   Disclaimer:     These opinions
are mine alone.
USA                                                         They do NOT
represent any organization.

"They that can give up essential liberty to obtain a little temporary
    safety deserve neither liberty nor safety."  -- Benjamin Franklin (1759)




%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Top of Message | Previous Page | Permalink

JiscMail Tools


RSS Feeds and Sharing


Advanced Options


Archives

December 2023
February 2023
November 2022
September 2022
February 2022
January 2022
June 2021
November 2020
September 2020
June 2020
May 2020
April 2020
December 2019
October 2019
September 2019
March 2019
February 2019
January 2019
November 2018
October 2018
September 2018
August 2018
July 2018
May 2018
April 2018
March 2018
February 2018
January 2018
December 2017
November 2017
October 2017
September 2017
August 2017
July 2017
June 2017
May 2017
April 2017
March 2017
January 2017
December 2016
November 2016
October 2016
September 2016
August 2016
July 2016
June 2016
May 2016
April 2016
March 2016
February 2016
December 2015
November 2015
October 2015
September 2015
August 2015
June 2015
April 2015
March 2015
January 2015
December 2014
November 2014
October 2014
August 2014
July 2014
May 2014
April 2014
March 2014
February 2014
January 2014
December 2013
November 2013
July 2013
June 2013
May 2013
April 2013
February 2013
January 2013
December 2012
November 2012
October 2012
September 2012
August 2012
July 2012
June 2012
April 2012
March 2012
February 2012
January 2012
December 2011
November 2011
October 2011
September 2011
August 2011
July 2011
June 2011
May 2011
April 2011
March 2011
February 2011
January 2011
December 2010
November 2010
October 2010
August 2010
July 2010
June 2010
March 2010
February 2010
January 2010
December 2009
October 2009
August 2009
July 2009
June 2009
March 2009
February 2009
January 2009
December 2008
November 2008
October 2008
September 2008
August 2008
July 2008
June 2008
May 2008
April 2008
March 2008
February 2008
December 2007
November 2007
October 2007
September 2007
August 2007
July 2007
June 2007
May 2007
April 2007
March 2007
January 2007
2006
2005
2004
2003
2002
2001
2000
1999
1998
1997


JiscMail is a Jisc service.

View our service policies at https://www.jiscmail.ac.uk/policyandsecurity/ and Jisc's privacy policy at https://www.jisc.ac.uk/website/privacy-notice

For help and support help@jisc.ac.uk

Secured by F-Secure Anti-Virus CataList Email List Search Powered by the LISTSERV Email List Manager