Fortran read question (raw binary in input file)

Hi I recently received a binary file and am having trouble reading it. I tried both BIG_ENDIAN and LITTLE_ENDIAN. The file consists of a 300 byte character header that I can read, and 136192 bytes that should be an integer1 field. The file size 136492 is consistent with that.
(should be direct access, unformatted, recl = 136492, I’ve experimented with different settings) I think I could figure out what was there if I could just see what the actual binary sequence was. The values (according to metadata I was given) should go from 0 to 250 (which makes sense since integer
1 allows 256 possible values). However, I keep reading the file into values from -128 to 127. I suspect this is not a BIG_ENDIAN or LITTLE_ENDIAN field. I would like to be able to see the actual binary values (from 00000000 to 11111111 should be the 1 byte range) to figure out what is there in the input file. How can I do that with a fortran read?



Hi Keith,

You could probably use STREAM I/O to read in the binary values.

However, it looks to me like the problem is that since Fortran doesn’t have unsigned integers, values 0 to 255 would map to -128 to 127 in an Integer1 data type. What you may need to do is read in the integer1 value and then convert to an integer. Something like:

% cat testa.f90 
integer :: myval
integer*1 :: int1val

int1val = 250
myval = iand(int(int1val),255)
print *, int1val, myval
% pgf90 testa.f90
% a.out
    -6          250
  • Mat

based upon your reply and some digging, I now see that values over 127 were folding over. I needed to create a new integer array (with larger than integer*1). Values below -5 I added 255 and got the field to 0 to 250, as expected, in way that was also physically realistic for the data. So, for instance 0 maps to 0, and -5 maps to 250.