      subroutine decod(c256,c,i,j,k,l,n)
      IMPLICIT REAL*8(A-H,O-x,z),logical*1(y)
      include 'maxorb.prm'
      common /orb/ enuc,eref,norb,noca,nocb
     *,iorb(2*maxorb),ninac,nvirt,noca0
      character*1 c(4),c256
      if(norb.lt.256) then
         i=ichar(c(1))
         j=ichar(c(2))
         k=ichar(c(3))
         if(n.eq.4)l=ichar(c(4))
         if(i.lt.0) i=i+256
         if(j.lt.0) j=j+256
         if(k.lt.0) k=k+256
         if(n.eq.4.and.l.lt.0) l=l+256
      else
         call decodi(c256,k1,k2,k3,k4,n)
         call irroc0(i,c(1),k1)
         call irroc0(j,c(2),k2)
         call irroc0(k,c(3),k3)
         if(n.ge.4)call irroc0(l,c(4),k4)
      endif
      return
      end
       subroutine decod1(c256,c1,c2,c3,c4,i,j,k,l,n)
      IMPLICIT REAL*8(A-H,O-x,z),logical*1(y)
      include 'maxorb.prm'
      character*1 c(4),c256,c1,c2,c3,c4
      c(1)=c1
      c(2)=c2
      c(3)=c3
      if(n.ge.4) c(4)=c4
      call decod(c256,c,i,j,k,l,n)
      return
      end
       subroutine encod(i,j,k,l,c1,c2,c3,c4,c256,n)
      IMPLICIT REAL*8(A-H,O-x,z),logical*1(y)
      include 'maxorb.prm'
      common /orb/ enuc,eref,norb,noca,nocb
     *,iorb(2*maxorb),ninac,nvirt,noca0
      character*1 c(4),c256,c1,c2,c3,c4
c      if(n.eq.4) then
c         if(i.le.0.or.j.le.0.or.k.le.0.or.l.le.0) then
c            print*,'i,j,k,l',i,j,k,l
c            stop 'encod'
c         endif
c      else
c         if(i.le.0.or.j.le.0.or.k.le.0) then
c            print*,'i,j,k',i,j,k
c            stop 'encod'
c         endif
c      endif
      if(norb.lt.256) then
         c1=char(i)
         c2=char(j)
         c3=char(k)
         if(n.eq.4) c4=char(l)
      else
         call icorr0(i,ii,c1,icod)
         call icorr0(j,ii,c2,jcod)
         call icorr0(k,ii,c3,kcod)
         if(n.ge.4) then
            call icorr0(l,ii,c4,lcod)
            jkl=icod+jcod*4+kcod*16+lcod*64
         else
            jkl=icod+jcod*4+kcod*16
         endif
         c256=char(jkl)
      endif
      return
      end
 
 
 
       subroutine decod2(i,c5,k)
      character*1 c5
      ii=ichar(c5)
      if(ii.eq.0) return
      if(k.eq.1) then
         k1=mod(ii,4)
      else if(k.eq.2) then
         k1=mod(ii,4)
         k1=mod((ii-k1)/4,4)
      else if(k.eq.3) then
         k1=ii/64
         k1=(ii-k1*64)/16
      else if(k.eq.4) then
         k1=ii/64
      endif
      i=i+k1*256
      return
      end
 
       subroutine decodi(c,k1,k2,k3,k4,n)
c
c----------------------------------------------------------------------
c     norb>256 ou 255  decodage de l'indice additionnel de l'adressage
c----------------------------------------------------------------------
c
      IMPLICIT REAL*8(A-H,O-x,z),logical*1(y)
      character*1 c
      i=ichar(c)
c      print*,'decodi i=ichar(c)',i
      if(i.lt.0) i=i+256
      k1=mod(i,4)
      k2=mod((i-k1)/4,4)
      if(n.ge.4) then
         k4=i/64
         k3=(i-k4*64)/16
      else
         k3=i/16
      endif
c      print*,'fin decodi',k1,k2,k3,k4
      return
      end
       subroutine irroc0(i,cnt,k)
c
c----------------------------------------------------------------------
c     norb>255 decodage des indices avec zero
c----------------------------------------------------------------------
c
      IMPLICIT REAL*8(A-H,O-x,z),logical*1(y)
      character*1 cnt
      icar=ichar(cnt)
      if(icar.lt.0) icar=icar+256
      i=icar+256*k
      return
      end
       subroutine icorr0(i,ii,cnt,k)
c
c----------------------------------------------------------------------
c     norb>255 codage des indices avec zero
c----------------------------------------------------------------------
c
      IMPLICIT REAL*8(A-H,O-x,z),logical*1(y)
      character*1 cnt
      if(i.lt.0.or.i.gt.1023) stop 'icorr0'
      ii=mod(i,256)
      cnt=char(ii)
      k=i/256
      return
      end
       subroutine decods(c256,c1,c2,c3,c4,i,j,k,l,io1,io2,io3,io4,n)
      IMPLICIT REAL*8(A-H,O-x,z),logical*1(y)
      include 'maxorb.prm'
      character*1 c(4),c256,c1,c2,c3,c4
      dimension ii(4)
      c(io1)=c1
      c(io2)=c2
      c(io3)=c3
      c(io4)=c4
      call decod(c256,c,ii(1),ii(2),ii(3),ii(4),n)
      i=ii(io1)
      j=ii(io2)
      k=ii(io3)
      l=ii(io4)
      return
      end
 
