Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

How to open binary file with BIG ENDIAN

milenko1976
Beginner
1,100 Views

I have one binary file(Sun Sparc) and now I want to convert it to ascii.Part of hex dump
0000000 0000 1800 0000 0000 0000 0000 0000 0000
0000010 0000 0000 0000 0000 ffff ffff 0000 1800
0000020 0000 1800 003f 0000 0000 0000 233c 0ad7
0000030 0000 0000 233c 0ad7 0000 0100 0000 1800
0000040 0000 1800 803f 0000 0000 0000 233c 0ad7
0000050 0000 0000 233c 0ad7 0000 0100 0000 1800
0000060 0000 1800 c03f 0000 0000 0000 233c 0ad7
0000070 0000 0000 233c 0ad7 0000 0100 0000 1800
0000080 0000 1800 0040 0000 0000 0000 233c 0ad7
0000090 0000 0000 233c 0ad7 0000 0100 0000 1800
00000a0 0000 1800 2040 0000 0000 0000 233c 0ad7
00000b0 0000 0000 233c 0ad7 0000 0100 0000 1800
00000c0 0000 1800 4040 0000 0000 0000 233c 0ad7
00000d0 0000 0000 233c 0ad7 0000 0100 0000 1800
00000e0 0000 1800 6040 0000 0000 0000 233c 0ad7
00000f0 0000 0000 233c 0ad7 0000 0100 0000 1800
0000100 0000 1800 8040 0000 0000 0000 233c 0ad7
0000110 0000 0000 233c 0ad7 0000 0100 0000 1800
0000120 0000 1800 9040 0000 0000 0000 233c 0ad7
0000130 0000 0000 233c 0ad7 0000 0100 0000 1800
0000140 0000 1800 a040 0000 0000 0000 233c 0ad7
0000150 0000 0000 233c 0ad7 0000 0100 0000 1800
0000160 0000 1800 b040 0000 0000 0000 233c 0ad7
0000170 0000 0000 233c 0ad7 0000 0100 0000 1800
0000180 0000 1800 c040 0000 0000 0000 233c 0ad7
0000190 0000 0000 233c 0ad7 0000 0100 0000 1800
00001a0 0000 1800 d040 0000 0000 0000 233c 0ad7
00001b0 0000 0000 233c 0ad7 0000 0100 0000 1800
00001c0 0000 1800 e040 0000 0000 0000 233c 0ad7
00001d0 0000 0000 233c 0ad7 0000 0100 0000 1800
00001e0 0000 1800 f040 0000 0000 0000 233c 0ad7
00001f0 0000 0000 233c 0ad7 0000 0100 0000 1800
0000200

 

How should I read this file?

open(10,file='rec.in',status='old',form='unformatted',CONVERT='BIG_ENDIAN')

I am confused because of this header and trailer issues.

0 Kudos
10 Replies
Steven_L_Intel1
Employee
1,100 Views

Yes, CONVERT='BIG_ENDIAN' should work. It will byte-swap the record lengths.

0 Kudos
milenko1976
Beginner
1,100 Views

program p1

implicit none
real,dimension(2400) :: v
integer ::irec

inquire (iolength = irec) v(1)
write(*,*)irec
open(10,file='rec.in',status='old',form='formatted',CONVERT='BIG_ENDIAN')
read(10,*)v

end

My code,but I got

           1
forrtl: severe (59): list-directed I/O syntax error, unit 10, file /home/milenko/Downloads/fast/2d_example_linux/rec.in
 

Why?

0 Kudos
Steven_L_Intel1
Employee
1,100 Views

You want form='unformatted' and "read(10)", not "read (10,*)". List-directed is not unformatted.

0 Kudos
milenko1976
Beginner
1,100 Views

Works fine but I got now some absurd values in my txt putput file.

If I suppose that each record contains 5 reals and one integer how should my read statement look like?

0 Kudos
Steven_L_Intel1
Employee
1,100 Views

read (10) A,B,C,D,E,J

Assuming A,B,C,D and E are declared REAL and J is declared INTEGER. Or if A is an array of 5 reals:

read (10) A,J

Maybe you want an END= or an IOSTAT= in there somewhere - I don't know what you're looking for.

0 Kudos
milenko1976
Beginner
1,100 Views

program p1

implicit none
real:: x,y,z,t,u
integer :: i,n


open(10,file='rec.in',status='old',form='unformatted', access='stream', convert='BIG_ENDIAN')
open(20,file='rec.txt',status='unknown')

100   read(10, end=999) x,y,z,t,u,i
      write(20,5) x,y,z,t,u,i
5     format(5f10.3,i3)
      n=n+1
      go to 100

999   write(6,200) n
200     format(/'number of lines in file: ',i10/)



end

 

Then part of the output

 cat rec.txt
     0.000     0.000     0.000     0.000     0.000  0
       NaN     0.000     0.000     0.500     0.000***
     0.000     0.010     0.000     0.000     0.000***
     0.000     0.010     0.000     0.010     0.000 24
     0.000     1.500     0.000     0.010     0.000***
     0.000     0.000     0.000     2.000     0.000***
     0.000     0.010     0.000     0.000     0.000***
     0.000     0.010     0.000     0.010     0.000 24
     0.000     3.000     0.000     0.010     0.000***
     0.000     0.000     0.000     3.500     0.000***
     0.000     0.010     0.000     0.000     0.000***
     0.000     0.010     0.000     0.010     0.000 24
     0.000     4.500     0.000     0.010     0.000***
     0.000     0.000     0.000     5.000     0.000***
     0.000     0.010     0.000     0.000     0.000***
 

0 Kudos
mecej4
Honored Contributor III
1,100 Views

Please attach the file rec.in, rather than providing fragments of hex-dumps of the file.

0 Kudos
milenko1976
Beginner
1,100 Views
0 Kudos
Steven_L_Intel1
Employee
1,100 Views

Take off "access="stream". It isn't a stream file.

0 Kudos
milenko1976
Beginner
1,100 Views

Thanks Steve,works perfect!

0 Kudos
Reply