Big Endian Little Endian

スパコンでの出力結果は、Big Endianであるため、Intelマシンで解析を行なうためにはLittle Endianに変換する必要があります。そこで、Intel FortranでのEndian問題の取扱について調べてみました。以下は備忘録。

方法は思った以上に簡単で、変換に使う装置番号をあらかじめ環境変数で指定しておき、変換(READ,WRITE)を行なうときには、その番号を指定すればよいようです。

例えば、環境変数の指定は、cshの場合、

setenv F_UFMTENDIAN 10

で、サンプルプログラムは、

      integer*4   c4
      c4 = 456

C prepare a little endian representation of data

      open(11,file='lit.tmp',form='unformatted')
      write(11) c4
      close(11)

C prepare a big endian representation of data

      open(10,file='big.tmp',form='unformatted')
      write(10) c4
      close(10)

      stop
      end

のような感じで、11番はLittle Endianで、10番のほうはBig Endianで書き出されます。

中身は、

od -t x4 lit.tmp
od -t x4 big.tmp

で比較できます。