1

I am trying to understand if it is possible to output binary from Fortran.

Specifically I have tried:

    integer(kind=1) :: a
    a = 64
    write(unit=6,form='UNFORMATTED') a

I am trying to output this variable to the standard output so one byte of value 64 would be written.

I.E. running:

$ ./my_code | od -d

would show a single byte of value 64.

Research done:

I have seen that there is the possibility to use Binary I/O. For example for Sun Fortran 95 compiler:

https://docs.oracle.com/cd/E19059-01/stud.9/817-6694/2_io.html (section 2.3)

But this is for files, not for standard output.

--

For standard output I have seen this post, but it is related to AIX and the /proc/self/fd/1 solution is not available in FreeBSD. I am interested in FreeBSD and Linux:

Write unformatted (binary data) to stdout

My questions are:

  1. Is there something in Fortran 95 or any other modern standard to allow and provision any functionality in write for this specific case?
  2. If so, which would be the syntax or at least, where can I learn more?
14
  • 2
    Does print '(B0)', a meet your requirements, or are you trying to get the ASCII value of 64? Commented May 23, 2021 at 21:29
  • I suggest to get a real standard Fotran resource (no matter if online or a paper book) instead of obscure manuals to obsolete compiler versions full of non-standard extensions. Commented May 23, 2021 at 21:51
  • The linked question contains two approaches in the accepted answer. Why did you disqualify the first one? Why should your question not actually be a duplicate of the linked one? Please note it does not contain only the accepted answer but also another answer that might work for you. That means 3 approaches in total are offered in the linked question and the answers. Why can't they be used and how should an approach you would like differ from them? Commented May 23, 2021 at 22:00
  • 2
    Please read what SEQUENCE and BIND(C) does to derived types. There is NO guarantee on the layout of the derived type components otherwise. You can ask a detailed question about this issue. Don't forget to show the code and the results. Commented May 24, 2021 at 8:33
  • 1
    It is best to ask a special question. BIND(C) does the default C layout. The packed one will likely be achieved with SEQUENCE. However, it will no longer be interoperable. BIND(C) is Fortran 2003. See also stackoverflow.com/questions/7793511/… The implications for SEQUENCE will be similar. Commented May 24, 2021 at 8:40

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.